如何找到两个点之间一定距离的点的位置?

时间:2012-11-15 00:49:35

标签: algorithm math point

假设我在笛卡尔坐标平面上有两个点AB,其xy坐标是双精度float s 。如何找到点C的位置,它是它们之间距离的任意百分比?

换句话说,用以下方法代替“//Do magic to C”的是什么?请注意,AB每个都包含两个double,代表各自的xy坐标。

public static findProgressPoint(DoublePoint A, DoublePoint B, double position)
{
  if (position > 1 || position < 0) //Ensure that position is between 0 and 1, inclusive
    position = position - (int)position;
  DoublePoint C = new DoublePoint(0.0, 0.0);
  //Do magic to C
  return C;
}

2 个答案:

答案 0 :(得分:4)

这应该有效:

double px = x1 + (x2-x1)*position;
double py = y1 + (y2-y1)*position;
DoublePoint C = new DoublePoint(px, py);

pxxx1之间的x2坐标,与x1的距离与position的值成正比; py是相应的y坐标。

答案 1 :(得分:1)

DoublePoint C = new DoublePoint( position * (A.x + B.x), position * (A.y + B.y) );