2

I found the equations to convert an X,Y into radius and angle:

x = r*Cos(Q)
y = r*Sin(Q)

r= sqrt(x*x + y*y)
Q = tan^-1(y/x)

My problem is that I dont remember what is the syntax for the tan^-1 in objective C. I do not want to get things wrong.

This got me thinking: Why build this from scratch? Is there a built-in way to convert a given CGPoint in Objective C from polar to rectangular coordinates? Maybe there's some CAAnimation class that can help me with this?

Macmade
  • 52,708
  • 13
  • 106
  • 123
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • 2
    http://developer.apple.com/library/ios/#documentation/System/Conceptual/ManPages_iPhoneOS/man3/atan2.3.html (and http://en.wikipedia.org/wiki/Polar_coordinate_system#Converting_between_polar_and_Cartesian_coordinates) – Hot Licks Nov 24 '11 at 16:37
  • great, I don't even have to type in the /sign if I use atan2(y,x) – Alex Stone Nov 24 '11 at 16:44
  • 1
    Right, and it handles 360 degrees without `if` logic. – Hot Licks Nov 24 '11 at 17:03

1 Answers1

5

To get the angle use:

Q = atan2( y, x );

This function will work out the quadrant to give the correct sign of the angle.

drmatt
  • 156
  • 6