0
$\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
    $\begingroup$ @JuanBonnet: Is that point at the exact center? $\endgroup$
    – Moo
    Jan 1, 2016 at 20:55
  • $\begingroup$ Not in my drawing, but IT IS in the software hehe $\endgroup$ Jan 1, 2016 at 20:56
  • $\begingroup$ Let's say I'm developing a videogame, and that rectangle is just a box that can be anywhere in a X,Y space, the center is variable relative to the whole 2D space, but not variable inside the rectangle itself. $\endgroup$ Jan 1, 2016 at 20:58

1 Answer 1

1
$\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
  • $\begingroup$ It works perfect, plus you used the native Javacript functions, thank you very much sir, I hope we can meet again when I have more questions now that I'm diving into complex computing stuff! Much respect and happy new year! $\endgroup$ Jan 1, 2016 at 21:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .