找到2点之间的限制点

Nap*_*eon 4 c# xna vector

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

这是问题的图像: 问题

注意:我发现其他2个非常相似或相同的问题,但我无法解决这些问题: 找到两点之间点的坐标?

如何仅使用段的部分长度找到形成段的2个点之间的点?

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

Geo*_*ett 6

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

A并且BVector2(因为你标记了这个问题).

// 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;
Run Code Online (Sandbox Code Playgroud)