I have a set of boxes which create a sort of path, they don't intersect very smoothy and to help that process I'm trying ot find a way to get an angle between them that can make it easier to generate another box between the intersection points.

Here's a pic of what I mean, the crudely drawn yellow arrows are basiclaly the angles I'm trying to find to put boxes there:

enter image description here

  • 3
    \$\begingroup\$ How are you storing the boxes? Are they point, rotation, width, height or 4 points? \$\endgroup\$ – Tetrad Sep 10 '11 at 6:33

That vector can be obtained simply by summing up the normals of the boxes faces and normalize. The normal vector even point outside the box so the sum vector points toward the direction where the boxes exit from the intersection. along with the vector you need the intersection segment too (if you still don't have it)


you can use atan2. for example:

float radians = Math.atan2(object2.y - object1.y, object2.x - object1.x);

and then if you want the degrees its like this:

float degrees = radians * 180 / PI

Since I can't comment yet, I'll just put it as an answer:

As stas said, use Atan2, but be careful. Atan2 returns 0 to PI like a normal angle, BUT it returns 0 to -PI for the lower half of the 'circle'.

And remember that in the computer, angles start at 3 o'clock for 0, 12 noon for (PI/2), 9 for PI, and 6 for (3PI/2) or -PI in this case. You'll need to convert anything that you receive as a negative.

To convert: if(radians < 0) { radians = (2 * PI) - radians; }

From there, convert to degrees if you so desire.

I'm doing this from memory, but I had 'fun' learning this on my own a couple years ago. If you need any more help, just ask.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.