将CGPoint沿特定方向移动一定距离... iphone [英] moving CGPoint a certain distance along a certain heading...iphone

查看:143
本文介绍了将CGPoint沿特定方向移动一定距离... iphone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个简单的问题,但是我一直找不到答案(我的数学也不擅长).我正在尝试将UIView沿某个标题移动到新的CGPoint X距离.确定新坐标的公式是什么?

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)

类似:

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

推荐答案

如果 p 是您的点,则D是距离,θ是与X轴的航向角,

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

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

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

不过,通常不使用存储距离和角度,而是使用 vectors (无需使用sin/cos)

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

这篇关于将CGPoint沿特定方向移动一定距离... iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆