【问题标题】:How can I find the location of a point a certain distance between two others?如何在其他两个点之间找到某个点的位置?
【发布时间】:2012-11-15 00:49:35
【问题描述】:

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

换句话说,以下方法中的内容是什么而不是“//Do magic to C”?请记住,AB 分别由两个 doubles 组成,它们代表各自的 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;
}

【问题讨论】:

  • 您希望点 C 位于 直线上 段 A-B,还是希望 any 点位于 dist(A,C) = R * 分布(B,C)?这将是一个夸张,IIRC。
  • 任意百分比,你确定吗?如果该百分比 .5,则有两个这样的独特点......在这种情况下,我相信你需要一些三角函数来找出确切的点...
  • @wildplasser 实际上,所有这些点的轨迹都是一个圆。
  • 我公式中的 R 不是半径,而是两个距离之间的比率。
  • @wildplasser 我希望它在线段 AB

标签: algorithm math point


【解决方案1】:

这应该可行:

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

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

【讨论】:

    【解决方案2】:
    DoublePoint C = new DoublePoint( position * (A.x + B.x), position * (A.y + B.y) );
    

    【讨论】:

    • 我不是反对者,但这真的适用于position 不是0.5 的情况吗?例如,当position 为零时,公式看起来会返回DoublePoint(0, 0),但它应该返回DoublePoint(A.x, B.x)position1 也是如此...
    • 我试过这个,起初......当position1.0 时,它会急剧超调。例如,给定A=(2.0,3.0)B=(4.0,5.0),这将给出C=(1.0*(2.0+4.0),1.0*(3.0+5.0))=(6.0,8.0) 而不是预期的C=(4.0,5.0)。此外,当position0.0 时,它总是返回C=(0.0,0.0) 而不是预期的C=(2.0,3.0)
    • 如果 pos = 1 那么 a.x + b.x - a.x 正好是 b.x
    • @ZdravkoDanev 确实如此,我看不出这与任何事情有什么关系。同样值得注意的是,鉴于a.x + b.x - a.xa.x 的任何值都将无效。
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2020-02-03
    • 2021-10-16
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多