4

this seems like such a simple problem but I have been unable to find an answer (and im no good at math). I am trying to move a UIView to a new CGPoint X distance away along a certain heading. What is the formula for determining the new coordinates?

(i do no want this to be animated, just an instantaneous move)

something like:

x = 100; (current x value)
y = 150; (current y value)
d = 25; (distance to move the point)
h = 90; (west)

\\\ insert formula to determine new x,y coords

self.car.center =  (CGPointMake ([newX],[newY]);
Brodie
  • 3,526
  • 8
  • 42
  • 61
  • What does "certain heading" mean? Is that a direction? Any assumptions about this? Some datatypes and code snippets would help. – Eiko Jun 03 '10 at 19:55
  • sorry i dont even have an code yet, im just kind of getting started on this. what i mean by heading is a course along a cardinal direction. I have a UIView on a map that needs to be moved as if it were following a heading (360 degrees). – Brodie Jun 03 '10 at 19:59

1 Answers1

9

If p is your point, D is the distance, and θ is the heading-angle from the X-axis,

pnew.x = pold.x + D * cos(θ)
pnew.y = pold.y + D * sin(θ)

Rather than storing distances and angles, though, this is usually done using vectors (which removes the need for the sin/cos)

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283