How to find the angle of a rectangle vertex

$\begingroup$

before I go further I want you to know that I'm developing a collision detection system in a programming language (Javascript).

I'm not used to math terms (it was like 10 years ago when I was in school), but I think this should be simple:

I need the formula to find this angle.

enter image description here

In the picture you can see I need the angle of the first vertex, but I also need the formula to find the other angles to the bottom right vertex, the bottom left vertex and so on.

Although the rectangle size is variable, I have access to its width and height, and also the X,Y coordinate of its center point.

Thank you beforehand!

$\endgroup$ 3

1 Answer

$\begingroup$

To find the "bearings" (angles) to the rectangle's vertices, all that is needed are the height (the vertical, Y direction: let's call it $h$) and the width (the horizontal, X direction: let's call it $w$). The coordinates of the center are not needed.

The upper-right vertex has bearing $\tan^{-1}\frac wh$ where $\tan^{-1}$ is the arctangent function, sometimes called $\operatorname{atan}$ or $\arctan$. I believe in javascript you would write

$$\operatorname{Math.atan}(w/h)$$

and the answer would be an angle in radians.

The lower-right vertex has bearing

$$\operatorname{Math.PI}-\operatorname{Math.atan}(w/h)$$

The lower-left vertex has bearing

$$\operatorname{Math.PI}+\operatorname{Math.atan}(w/h)$$

and the upper-left vertex has bearing

$$2*\operatorname{Math.PI}-\operatorname{Math.atan}(w/h)$$

Do you need further explanation of these formulas?

$\endgroup$ 1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like