Search code examples
javaandroidmathgeometryangle

Finding angle from center point on circle


Possible Duplicate:
Find angle of a point from center of circle

Imagine a circle, the center point is fixed, as is the point at the top of the circle. What i want is to give a third point anywhere around the circle (or outside the circle) and get the degrees from the center to top point line from 0-359. (I actually graphed out a nice picture illustrating but im new and cant post it)

To give some context, the circle is a little planet and I want do place a little building on the surface with the base of the building at a tangent. I need the rotation angle for my building bitmap.

edit: Thanks for the help, i'm still struggling with this one though. I wonder could it be relevant that I'm using android and the y0 coordinate is at the top? Is it the other way around on other platforms? would that affect the calculation?

Solution: Because I am in android and the y coords are counted from top to bottom I had to change a - witha +

degrees =  Math.atan2(x - centerX, -y + centerY);
// and to make it count 0-360
if (degrees < 0) {degrees += 2 * Math.PI;}

Solution

  • Use Math.atan2() to get the angle in radians from east, and then rotate and convert as appropriate.

    Note that atan2 is defined as atan2(y, x) NOT atan2(x, y), as you might expect.