我需要使用C#使用最少量的计算来解决这个问题。
我有分数(x0,y0); (y1,y1); (x2,y2): (x1,y1); (x2,y2)定义一条线,但是一段“S”。 (x0,y0)是一个孤立的点,距离“d”较短的段是一个垂直的段,其距离“d”
我使用此公式http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html计算了“d”,并使用“两点之间的距离”计算了“r”的值
public static Double GetDistance(Point sourcePoint, Point destinyPoint)
{
Double horizontalDistance = (destinyPoint.X - sourcePoint.X);
Double verticalDistance = (destinyPoint.Y - sourcePoint.Y);
Double distance = System.Math.Sqrt((horizontalDistance * horizontalDistance) + (verticalDistance * verticalDistance));
return distance;
}
实际上我需要找到红点的坐标。
答案 0 :(得分:2)
首先,您可以使用
轻松找到s
double s = Math.Sqrt(r*r - d*d);
然后找到point1和point2之间的距离
double distance = GetDistance(point1, point2);
现在你可以解决红点的位置了。
double redX = (x2-x1) / distance * s;
double redY = (y2-y1) / distance * s;
简易方法
如果你可以使用向量(如System.Numerics.Vectors中的Vector2),那将是一个非常简单的orthogonal projection问题。
Vector2 p1 = new Vector2(point1.X, point1.Y);
Vector2 p2 = new Vector2(point2.X, point2.Y);
Vector2 p0 = new Vector2(point0.X, point0.Y);
Vector2 s = p2 - p1;
Vector2 v = p0 - p1;
double len = Vector2.Dot(v, s) / Vector2.Dot(s, s);
Vector2 projection = len * s;
// Finally get the position of your red dot
Point pointRedDot = point1 + new Point(projection.X, projection.Y);
Console.WriteLine(pointRedDot);