Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moving CGPoint a certain distance along a certain heading...iphone

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]);
like image 282
Brodie Avatar asked Aug 31 '25 04:08

Brodie


1 Answers

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)

like image 177
BlueRaja - Danny Pflughoeft Avatar answered Sep 02 '25 20:09

BlueRaja - Danny Pflughoeft