2011-10-22 65 views
4
  • 我有坐标点A和另一点(如B,C或D)。
  • 我也有A和另一点之间的距离。
  • 我知道A和另一点之间的最大允许距离(用紫色线和虚圆表示)。
  • 问题:如何查找红点(B1或C1或D1)的坐标。
  • 示例:A =( - 1,1),E =(3,-8),最大允许距离= 4.点E1的坐标是什么?

这是问题的一个形象: problem查找2点之间的限制点

注: 我发现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?

PS这不是我需要编程问题的作业,但我忘了我的数学...

+0

这真的是一个编程问题?它似乎更多是一个基数学问题 – xanatos

+0

所以,你需要一个长度MaxDistance的方向A - > B/C/D的向量? –

+0

Xanatos:是的,这是一个编程问题,因为这是我在C#/ XNA中设置游戏集结点的问题。 – Napoleon

回答

6

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

AB都是Vector2的(如您对此问题标记为)。

// 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; 
+0

+1,优雅的方式来做到这一点。 –

+0

是的坐标确实是Vector2结构。而且这个解决方案完美地工作,似乎也没有花费太多的性能。请注意,Normalize()返回无效,所以它仍然需要2-3行,但仍然有效。 – Napoleon

+0

@ user653160:更正为使用静态的“Normalize”方法。 –