Perpendicular on a line from a given point

前端 未结 14 1542
名媛妹妹
名媛妹妹 2020-11-29 18:34

How can I draw a perpendicular on a line segment from a given point? My line segment is defined as (x1, y1), (x2, y2), If I draw a perpendicular from a point (x3,y3) and it

相关标签:
14条回答
  • 2020-11-29 19:12

    From wiki:

    In algebra, for any linear equation y=mx + b, the perpendiculars will all have a slope of (-1/m), the opposite reciprocal of the original slope. It is helpful to memorize the slogan "to find the slope of the perpendicular line, flip the fraction and change the sign." Recall that any whole number a is itself over one, and can be written as (a/1)

    To find the perpendicular of a given line which also passes through a particular point (x, y), solve the equation y = (-1/m)x + b, substituting in the known values of m, x, and y to solve for b.

    The slope of the line, m, through (x1, y1) and (x2, y2) is m = (y1 - y2) / (x1 - x2)

    0 讨论(0)
  • 2020-11-29 19:12

    Compute the slope of the line joining points (x1,y1) and (x2,y2) as m=(y2-y1)/(x2-x1)

    Equation of the line joining (x1,y1) and (x2,y2) using point-slope form of line equation, would be y-y2 = m(x-x2)

    Slope of the line joining (x3,y3) and (x4,y4) would be -(1/m)

    Again, equation of the line joining (x3,y3) and (x4,y4) using point-slope form of line equation, would be y-y3 = -(1/m)(x-x3)

    Solve these two line equations as you solve a linear equation in two variables and the values of x and y you get would be your (x4,y4)

    I hope this helps.

    cheers

    0 讨论(0)
  • 2020-11-29 19:15

    Just for the sake of completeness, here is a solution using homogeneous coordinates.

    1. The homogeneous points are:

      p1 = (x1,y1,1), p2 = (x2,y2,1), p3 = (x3,y3,1)

    2. a line through two points is their cross-product

      l_12 := p1 x p2 = (y1-y2, x2-x1, x1*y2 - x2*y1)

    3. The (signed) distance of a point to a line is their dot product.

      d := l_12 * p3 = x3*(y1-y2) + y3*(x2-x1) + x1*y2 - x2*y1

    4. The vector from p4 to p3 is d times the normal vector of l_12 divided by the squared length of the normal vector.

      n2 := (y1-y2)^2 + (x2-x1)^2

      p4 := p3 + d/n2*(y1-y2, x2-x1, 0)

    Note: if you divide l_12 by the length of the normal vector

    l_12 := l_12 / sqrt((y1-y2)^2 + (x2-x1)^2)

    the distance d will be the euclidean distance.

    0 讨论(0)
  • 2020-11-29 19:19

    This is a vectorized Matlab function for finding pairwise projections of m points onto n line segments. Here xp and yp are m by 1 vectors holding coordinates of m different points, and x1, y1, x2 and y2 are n by 1 vectors holding coordinates of start and end points of n different line segments. It returns m by n matrices, x and y, where x(i, j) and y(i, j) are coordinates of projection of i-th point onto j-th line.

    The actual work is done in first few lines and the rest of the function runs a self-test demo, just in case where it is called with no parameters. It's relatively fast, I managed to find projections of 2k points onto 2k line segments in less than 0.05s.

    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
    
    0 讨论(0)
  • 2020-11-29 19:25

    You will often find that using vectors makes the solution clearer...

    Here is a routine from my own library:

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

    }

    You will have to implement Real2 (a point) and Vector2 and dotProduct() but these should be simple:

    The code then looks something like:

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

    The library (org.xmlcml.euclid) is at: http://sourceforge.net/projects/cml/

    and there are unit tests which will exercise this method and show you how to use it.

    @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);
    }
    
    0 讨论(0)
  • 2020-11-29 19:26

    I solved the equations for you:

    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)
    

    Where ^2 means squared

    0 讨论(0)
提交回复
热议问题