【问题标题】:Find limiting point between 2 points找到两点之间的限制点
【发布时间】:2011-10-22 15:00:26
【问题描述】:
  • 我有 A 点和另一个点(如 B、C 或 D)的坐标。
  • 我也知道 A 和另一点之间的距离。
  • 我知道 A 和另一点之间的最大允许距离(用紫色线和假想的圆圈说明)。
  • 问题:如何找到红点的坐标(B1 或 C1 或 D1)。
  • 示例:A=(-1,1), E=(3,-8),最大允许距离 = 4。E1 点的坐标是多少?

这是问题的图片:

注意: 我发现了 2 个非常相似或相等的其他问题,但我无法解决这些问题: Finding coordinates of a point between two points?

How can I find a point placed between 2 points forming a segment using only the partial length of the segment?

附:这不是家庭作业,我需要它来解决编程问题,但我忘记了我的数学......

【问题讨论】:

  • 这真的是编程问题吗?这似乎更像是一个基础数学问题
  • 那么,你想要一个方向为 A -> B/C/D 的向量,长度为 MaxDistance?
  • Xanatos:是的,这是一个编程问题,因为这是我在使用 C#/XNA 为游戏设置集结点时遇到的问题。
  • George:不,我不想要向量(像向量),但我只需要 A->B 之间线上的点坐标,例如点的确切位置在哪里LIMITED(未确定)的最大范围。

标签: c# vector xna


【解决方案1】:

假设 A 是位置向量,B 是位置向量,maxLength 是您允许的最大长度。

ABVector2 的(当您标记了这个问题 )。

// Create a vector that describes going from A to B
var AtoB = (B - A);
// Make a vector going from A to B, but only one unit in length
var AtoBUnitLength = Vector2.Normalize(AtoB);
// Make a vector in the direction of B from A, of length maxLength
var AtoB1 = AtoBUnitLength * maxLength;
// B1 is the starting point (A) + the direction vector of the
// correct length we just created.
var B1 = A + AtoB1;

// One liner:
var B1 = A + Vector2.Normalize(B - A) * maxLength;

【讨论】:

  • 是的,坐标确实是 Vector2 结构。而且这个解决方案效果很好,而且似乎也不会花费太多性能。请注意 Normalize() 返回 void 所以它仍然需要 2-3 行但它仍然有效。
  • @user653160:更正为使用静态Normalize 方法。
猜你喜欢
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多