Home > matGeom > geom2d > projPointOnLine.m

projPointOnLine

PURPOSE ^

PROJPOINTONLINE Project of a point orthogonally onto a line.

SYNOPSIS ^

function point = projPointOnLine(point, line)

DESCRIPTION ^

PROJPOINTONLINE Project of a point orthogonally onto a line.

   PT2 = projPointOnLine(PT, LINE).
   Computes the (orthogonal) projection of point PT onto the line LINE.
   
   Function works also for multiple points and lines. In this case, it
   returns multiple points.
   Point PT1 is a [N*2] array, and LINE is a [N*4] array (see createLine
   for details). Result PT2 is a [N*2] array, containing coordinates of
   orthogonal projections of PT1 onto lines LINE.

   Example
     line = [0 2  2 1];
     projPointOnLine([3 1], line)
     ans = 
          2   3

   See also:
   lines2d, points2d, isPointOnLine, linePosition

   ---------
   author : David Legland
   INRA - TPV URPOI - BIA IMASTE
   created the 07/04/2005.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function point = projPointOnLine(point, line)
0002 %PROJPOINTONLINE Project of a point orthogonally onto a line.
0003 %
0004 %   PT2 = projPointOnLine(PT, LINE).
0005 %   Computes the (orthogonal) projection of point PT onto the line LINE.
0006 %
0007 %   Function works also for multiple points and lines. In this case, it
0008 %   returns multiple points.
0009 %   Point PT1 is a [N*2] array, and LINE is a [N*4] array (see createLine
0010 %   for details). Result PT2 is a [N*2] array, containing coordinates of
0011 %   orthogonal projections of PT1 onto lines LINE.
0012 %
0013 %   Example
0014 %     line = [0 2  2 1];
0015 %     projPointOnLine([3 1], line)
0016 %     ans =
0017 %          2   3
0018 %
0019 %   See also:
0020 %   lines2d, points2d, isPointOnLine, linePosition
0021 %
0022 %   ---------
0023 %   author : David Legland
0024 %   INRA - TPV URPOI - BIA IMASTE
0025 %   created the 07/04/2005.
0026 %
0027 
0028 %   HISTORY
0029 %   2005-08-06 correct bug when several points were passed as param.
0030 %   2012-08-23 remove repmats
0031 
0032 % direction vector of the line
0033 vx = line(:, 3);
0034 vy = line(:, 4);
0035 
0036 % difference of point with line origin
0037 dx = point(:,1) - line(:,1);
0038 dy = point(:,2) - line(:,2);
0039 
0040 % Position of projection on line, using dot product
0041 tp = (dx .* vx + dy .* vy ) ./ (vx .* vx + vy .* vy);
0042 
0043 % convert position on line to cartesian coordinates
0044 point = [line(:,1) + tp .* vx, line(:,2) + tp .* vy];

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019