Home > matGeom > geom2d > projPointOnLine.m

projPointOnLine

PURPOSE ^

PROJPOINTONLINE Project a point orthogonally onto a line.

SYNOPSIS ^

function point = projPointOnLine(point, line)

DESCRIPTION ^

PROJPOINTONLINE Project 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-by-2 array, and LINE is a N-by-4 array (see the
   createLine function for details). Result PT2 is a N-by-2 array,
   containing the coordinates of orthogonal projections of points PT1 onto
   lines LINE.

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

   See also
     lines2d, points2d, isPointOnLine, linePosition, projPointOnEllipse

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function point = projPointOnLine(point, line)
0002 %PROJPOINTONLINE Project 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-by-2 array, and LINE is a N-by-4 array (see the
0010 %   createLine function for details). Result PT2 is a N-by-2 array,
0011 %   containing the coordinates of orthogonal projections of points PT1 onto
0012 %   lines LINE.
0013 %
0014 %   Example
0015 %     line = [0 2  2 1];
0016 %     projPointOnLine([3 1], line)
0017 %     ans =
0018 %          2   3
0019 %
0020 %   See also
0021 %     lines2d, points2d, isPointOnLine, linePosition, projPointOnEllipse
0022 %
0023 
0024 % ------
0025 % Author: David Legland
0026 % E-mail: david.legland@inrae.fr
0027 % Created: 2005-07-04
0028 % Copyright 2005-2024 INRAE - BIA Research Unit - BIBS Platform (Nantes)
0029 
0030 % parse input arguments
0031 p = inputParser;
0032 addRequired(p, 'point', @(x)validateattributes(x,{'numeric'},...
0033     {'size',[nan, 2],'nonnan','real','finite'}))
0034 addRequired(p, 'line', @(x)validateattributes(x,{'numeric'},...
0035     {'size',[nan, 4],'nonnan','real','finite'}))
0036 parse(p, point, line)
0037 
0038 % direction vector of the line
0039 vx = line(:, 3);
0040 vy = line(:, 4);
0041 
0042 % difference of point with line origin
0043 dx = point(:,1) - line(:,1);
0044 dy = point(:,2) - line(:,2);
0045 
0046 % Position of projection on line, using dot product
0047 tp = (dx .* vx + dy .* vy ) ./ (vx .* vx + vy .* vy);
0048 
0049 % convert position on line to cartesian coordinates
0050 point = [line(:,1) + tp .* vx, line(:,2) + tp .* vy];

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022