简体   繁体   中英

How to find the angle from the center of a rectangle to its vertices

I'm developing a collision detection system in Javascript, and I need to find from which side of the rectangle a ball collided.

Anyway, what I need right now is to find the angle from the center of a rectangle to its vertices. Like this:

在此处输入图片说明

As you can see in the image, I want to find that angle, but also the rest of the angles to the bottom left and top left vertices.

I know this is math, but I need to code the formula in Javascript anyway.

Let's say I have this:

var box = {
   width : 200,
   height : 100
};

var boxCenter = {x : box.width / 2, y : box.height / 2 };

var angleRight = // ... ;
var angleBottom = // ... ;

And so on

角度(红色)可通过以下公式计算:

var angle = 2* Math.atan(height/width);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM