如何在给定点的线段上绘制垂线?我的线段定义为(x1,y1),(x2,y2),如果我从点(x3,y3)绘制一个垂直线并且它在点(x4,y4)上遇到线。我想找出这个(x4,y4)。

有帮助吗?

解决方案

我为你解决了方程:

k = ((y2-y1) * (x3-x1) - (x2-x1) * (y3-y1)) / ((y2-y1)^2 + (x2-x1)^2)
x4 = x3 - k * (y2-y1)
y4 = y3 + k * (x2-x1)

其中^ 2表示平方

其他提示

来自 wiki

  

在代数中,对于任何线性方程   y = mx + b,垂线全部都是   斜率为(-1 / m),相反   原坡的倒数。它   有助于记住“to to”的口号   找到垂直的斜率   线,翻转分数并改变   签&QUOT。回想一下整个数字a   本身就是一个,可以写   as(a / 1)

     

找到给定的垂线   也经过一条线的线   特别点(x,y),解决了   方程y =( - 1 / m)x + b,代替   在已知的m,x和y值中   求解b。

线的斜率m,通过(x1,y1)和(x2,y2)是m =(y1-y2)/(x1-x2)

我同意peter.murray.rust,矢量使解决方案更加清晰:

// first convert line to normalized unit vector
double dx = x2 - x1;
double dy = y2 - y1;
double mag = sqrt(dx*dx + dy*dy);
dx /= mag;
dy /= mag;

// translate the point and get the dot product
double lambda = (dx * (x3 - x1)) + (dy * (y3 - y1));
x4 = (dx * lambda) + x1;
y4 = (dy * lambda) + y1;

您经常会发现使用矢量可以使解决方案更清晰......

以下是我自己图书馆的例程:

public class Line2  {

Real2 from;
Real2 to;
Vector2 vector;
Vector2 unitVector = null;


    public Real2 getNearestPointOnLine(Real2 point) {
        unitVector = to.subtract(from).getUnitVector();
        Vector2 lp = new Vector2(point.subtract(this.from));
        double lambda = unitVector.dotProduct(lp);
        Real2 vv = unitVector.multiplyBy(lambda);
        return from.plus(vv);
    }

}

你必须实现Real2(一个点)和Vector2和dotProduct(),但这些应该很简单:

然后代码看起来像:

Point2 p1 = new Point2(x1, y1);
Point2 p2 = new Point2(x2, y2);
Point2 p3 = new Point2(x3, y3);
Line2 line = new Line2(p1, p2);
Point2 p4 = getNearestPointOnLine(p3);

图书馆(org.xmlcml.euclid)位于: http://sourceforge.net/projects/cml/

并且有单元测试将运用此方法并向您展示如何使用它。

@Test
public final void testGetNearestPointOnLine() {
    Real2 p = l1112.getNearestPointOnLine(new Real2(0., 0.));
    Real2Test.assertEquals("point", new Real2(0.4, -0.2), p, 0.0000001);
}

你知道点和斜率,所以新线的等式是:

y-y3=m*(x-x3)

由于线是垂直的,因此斜率是负的倒数。你现在有两个方程式,可以求解它们的交集。

y-y3=-(1/m)*(x-x3)
y-y1=m*(x-x1)

计算连接点(x1,y1)和(x2,y2)的线的斜率为 m =(y2-y1)/(x2-x1)

使用线斜率形式的线方程连接(x1,y1)和(x2,y2)的线的方程式将是 y-y2 = m(x-x2)

连接线(x3,y3)和(x4,y4)的斜率为 - (1 / m)

同样,使用线斜率形式的线方程连接(x3,y3)和(x4,y4)的线的方程将是 y-y3 = - (1 / m)(x-x3)

在求解两个变量的线性方程时求解这两个线方程,得到的x和y的值将是你的(x4,y4)

我希望这会有所帮助。

欢呼声

  

找出两者的斜率   线,比如说斜率是m1和m2    m1 * m2 = -1 是条件   垂直度。

以下问题的Matlab函数代码

function Pr=getSpPoint(Line,Point)
% getSpPoint(): find Perpendicular on a line segment from a given point
x1=Line(1,1);
y1=Line(1,2);
x2=Line(2,1);
y2=Line(2,1);
x3=Point(1,1);
y3=Point(1,2);

px = x2-x1;
py = y2-y1;
dAB = px*px + py*py;

u = ((x3 - x1) * px + (y3 - y1) * py) / dAB;
x = x1 + u * px;
y = y1 + u * py;

Pr=[x,y];

end

Mathematica在2014年版本10中引入了函数 RegionNearest [] 。此函数可用于返回此问题的答案:

{x4,y4} = RegionNearest[Line[{{x1,y1},{x2,y2}}],{x3,y3}]

这主要是Arnkrishn答案的重复。我只想用完整的Mathematica代码片段完成他的部分:

m = (y2 - y1)/(x2 - x1)
eqn1 = y - y3 == -(1/m)*(x - x3)
eqn2 = y - y1 == m*(x - x1)
Solve[eqn1 && eqn2, {x, y}]

这是接受答案的C#实现。它也使用ArcGis返回MapPoint,因为我们正在为这个项目使用它。

        private MapPoint GenerateLinePoint(double startPointX, double startPointY, double endPointX, double endPointY, double pointX, double pointY)
        {
            double k = ((endPointY - startPointY) * (pointX - startPointX) - (endPointX - startPointX) * (pointY - startPointY)) / (Math.Pow(endPointY - startPointY, 2) 
                + Math.Pow(endPointX - startPointX, 2));
            double resultX = pointX - k * (endPointY - startPointY);
            double resultY = pointY + k * (endPointX - startPointX);

            return new MapPoint(resultX, resultY, 0, SpatialReferences.Wgs84);
        }

感谢Ray,因为这对我来说非常合适。 arcgis

这是一个矢量化的Matlab函数,用于查找 m 点到 n 线段的成对投影。这里 xp yp m by 1 向量,其中包含 m 不同点的坐标, x1 y1 x2 y2 n by 1 矢量,保存起点和终点的坐标 n 不同的细分市场。 它返回 m by n 矩阵, x y ,其中 x(i,j) y(i,j) i - 投影到 j -th line的投影坐标。

实际工作在前几行完成,函数的其余部分运行自测试演示,以防万一没有参数调用。它相对较快,我设法在不到0.05秒的时间内找到了2k点到2k线段的投影。

function [x, y] = projectPointLine(xp, yp, x1, y1, x2, y2)
if nargin > 0
        xd = (x2-x1)';
    yd = (y2-y1)';
    dAB = xd.*xd + yd.*yd;
    u = bsxfun(@rdivide, bsxfun(@times, bsxfun(@minus, xp, x1'), xd) + ...
        bsxfun(@times, bsxfun(@minus, yp, y1'), yd), dAB);
    x = bsxfun(@plus, x1', bsxfun(@times, u, xd));
    y = bsxfun(@plus, y1', bsxfun(@times, u, yd));
else
    nLine = 3;
    nPoint = 2;
    xp = rand(nPoint, 1) * 2 -1;
    yp = rand(nPoint, 1) * 2 -1;
    x1 = rand(nLine, 1) * 2 -1;
    y1 = rand(nLine, 1) * 2 -1;
    x2 = rand(nLine, 1) * 2 -1;
    y2 = rand(nLine, 1) * 2 -1;
    tic;
    [x, y] = projectPointLine(xp, yp, x1, y1, x2, y2);
    toc
    close all;
    plot([x1'; x2'], [y1'; y2'], '.-', 'linewidth', 2, 'markersize', 20);
    axis equal;
    hold on
    C = lines(nPoint + nLine);
    for i=1:nPoint
        scatter(x(i, :), y(i, :), 100, C(i+nLine, :), 'x', 'linewidth', 2);
        scatter(xp(i), yp(i), 100, C(i+nLine, :), 'x', 'linewidth', 2);
    end
    for i=1:nLine
        scatter(x(:, i)', y(:, i)', 100, C(i, :), 'o', 'linewidth', 2);
    end
end
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top