1

假设我有 2 分(x1,y1)o----------------o(x2,y2)

我想要做的是在我沿着直线移动距离(x1,y1)的距离d时获取新坐标。然而我意识到,我离 (x1,y1) 越远,新坐标开始变得越来越不准确(即它偏离原线)。我的解决方案基于https://math.stackexchange.com/questions/25286/2d-coordinates-of-a-point-along-a-line-based-on-d-and-m-中提供的最后一个答案我在哪里搞砸。我在这里做错了什么吗?或者objective-c中是否有一个可以做同样事情的类?

谢谢!

float signu, signv;
float x, y;
float x1 = cp1.x;
float y1 = cp1.y;
float x2 = cp2.x;
float y2 = cp2.y;
float d = noOfSteps*pixelsPerStep;
float m = (y2-y1)/(x2-x1);
float u = d/ABS(sqrt(m*m+1));
float v = m*u;
if(x1-x1<=0)
    signu = -1;
if(y2-y1<=0)
    signv = -1;
x = x1 + signu*u;
y = y1 + signv*v;
4

1 回答 1

1

试试向量代数的标准方法:

Norm = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))
Assert Norm != 0

//components of direction vector
Dir_X = (x2 - x1) / Norm
Dir_Y = (y2 - y1) / Norm

x = x1 + d * Dir_X
y = y1 + d * Dir_Y
于 2012-08-18T17:38:49.453 回答