moving CGPoint a certain distance along a certain heading…iphone

倾然丶 夕夏残阳落幕 提交于 2020-01-04 02:17:13

问题


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]);

回答1:


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)



来源:https://stackoverflow.com/questions/2969181/moving-cgpoint-a-certain-distance-along-a-certain-heading-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!