Home > matGeom > polygons2d > polylinePoint.m

polylinePoint

PURPOSE ^

POLYLINEPOINT Extract a point from a 2D or 3D polyline.

SYNOPSIS ^

function point = polylinePoint(poly, pos)

DESCRIPTION ^

POLYLINEPOINT Extract a point from a 2D or 3D polyline.

   POINT = polylinePoint(POLYLINE, POS)
   POLYLINE is a N*2 or N*3 array containing coordinate of polyline vertices
   POS is comprised between 0 (first point of polyline) and Nv-1 (last
   point of the polyline).
   

   Example
   poly = [10 10;20 10;20 20;30 30];
   polylinePoint(poly, 0)
       [10 10]
   polylinePoint(poly, 3)
       [30 30]
   polylinePoint(poly, 1.4)
       [20 14]


   See also
   polygons2d

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function point = polylinePoint(poly, pos)
0002 %POLYLINEPOINT Extract a point from a 2D or 3D polyline.
0003 %
0004 %   POINT = polylinePoint(POLYLINE, POS)
0005 %   POLYLINE is a N*2 or N*3 array containing coordinate of polyline vertices
0006 %   POS is comprised between 0 (first point of polyline) and Nv-1 (last
0007 %   point of the polyline).
0008 %
0009 %
0010 %   Example
0011 %   poly = [10 10;20 10;20 20;30 30];
0012 %   polylinePoint(poly, 0)
0013 %       [10 10]
0014 %   polylinePoint(poly, 3)
0015 %       [30 30]
0016 %   polylinePoint(poly, 1.4)
0017 %       [20 14]
0018 %
0019 %
0020 %   See also
0021 %   polygons2d
0022 
0023 % ------
0024 % Author: David Legland
0025 % E-mail: david.legland@inrae.fr
0026 % Created: 2009-04-30, using Matlab 7.7.0.471 (R2008b)
0027 % Copyright 2009-2024 INRA - Cepia Software Platform
0028 
0029 % number of points to compute
0030 Np = length(pos(:));
0031 
0032 % number of vertices in polyline
0033 Nv = size(poly, 1);
0034 
0035 % allocate memory results
0036 point = zeros(Np, size(poly,2));
0037 
0038 % iterate on points
0039 for i=1:Np
0040     % compute index of edge (between 0 and Nv)
0041     ind = floor(pos(i));
0042     
0043     % special case of last point of polyline
0044     if ind==Nv-1
0045         point(i,:) = poly(end,:);
0046         continue;
0047     end
0048     
0049     % format index to ensure being on polyline
0050     ind = min(max(ind, 0), Nv-2);
0051     
0052     % position on current edge
0053     t = min(max(pos(i)-ind, 0), 1);
0054     
0055     % parameters of current edge
0056     x0 = poly(ind+1, 1);
0057     y0 = poly(ind+1, 2);
0058     dx = poly(ind+2,1)-x0;
0059     dy = poly(ind+2,2)-y0;
0060     if size(poly,2)>2
0061         z0 = poly(ind+1, 3);
0062         dz = poly(ind+2,3)-z0;
0063     end
0064     % compute position of current point
0065     if size(poly,2)>2
0066         point(i, :) = [x0+t*dx, y0+t*dy, z0+t*dz];
0067     else
0068         point(i, :) = [x0+t*dx, y0+t*dy];
0069     end
0070 end

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