Juan Bonnett
Juan Bonnett

Reputation: 1863

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:

enter image description here

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

Upvotes: 1

Views: 946

Answers (1)

Eduardo Escobar
Eduardo Escobar

Reputation: 3389

The angle (red) may be calculated with:

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

Upvotes: 2

Related Questions