0

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;}
Community
  • 1
  • 1
Jake
  • 132
  • 2
  • 7
  • 3
    I'd look at sine and cosine in trigonometry. – duffymo Jan 07 '13 at 10:50
  • 1
    That's what you need? - http://stackoverflow.com/questions/8968500/find-angle-of-a-point-from-center-of-circle?rq=1 – Andreas Dolk Jan 07 '13 at 10:52
  • Have a look at [this](http://stackoverflow.com/questions/12964983/rotate-image-around-character-java/12971987#12971987) for a possible solution, or at least a push in the right direction ;) – MadProgrammer Jan 07 '13 at 11:20

2 Answers2

4

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.

Bill
  • 44,502
  • 24
  • 122
  • 213
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • This was pretty much it, my oversight was that I'm in Android and the Y coords are counted from top to bottom, in the end i just had to replace a - with +. – Jake Jan 08 '13 at 04:45
0

Get the horizontal distance and the vertical difference between the center and the point, divide one by the other, and pass the result to the method Math.asin(double).

The result will be the angle in radians. When you need it in degree, you can use the method Math.toDegrees(double). Most APIs I know prefer radians, though.

Philipp
  • 67,764
  • 9
  • 118
  • 153