假设我在笛卡尔坐标平面上有两个点A
和B
,其x
和y
坐标是双精度float
s 。如何找到点C
的位置,它是它们之间距离的任意百分比?
换句话说,用以下方法代替“//Do magic to C
”的是什么?请注意,A
和B
每个都包含两个double
,代表各自的x
和y
坐标。
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;
}
答案 0 :(得分:4)
这应该有效:
double px = x1 + (x2-x1)*position;
double py = y1 + (y2-y1)*position;
DoublePoint C = new DoublePoint(px, py);
px
是x
与x1
之间的x2
坐标,与x1
的距离与position
的值成正比; py
是相应的y
坐标。
答案 1 :(得分:1)
DoublePoint C = new DoublePoint( position * (A.x + B.x), position * (A.y + B.y) );