来自孤立点的直线和垂直线段,获取所有坐标

Line and Perpendicular segment from a Isolated Point, Get all Coordinates

我需要使用 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"[=31 的值=] 使用 "Distance between two points".

    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;
    }

其实我需要找到红点的坐标

首先你可以通过

轻松找到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), it will be a very simple orthogonal projection 问题中的 Vector2。

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);