Home > matGeom > polygons2d > polygonSubcurve.m

polygonSubcurve

PURPOSE ^

Extract a portion of a polygon.

SYNOPSIS ^

function [res, inds] = polygonSubcurve(poly, t0, t1)

DESCRIPTION ^

 Extract a portion of a polygon.

   POLY2 = polygonSubcurve(POLYGON, POS0, POS1)
   Create a new polyline, by keeping vertices located between positions
   POS0 and POS1, and adding points corresponding to positions POS0 and
   POS1 if they are not already vertices.

   [POLY2, INDS] = polygonSubcurve(POLYGON, POS0, POS1)
   Also return indices of polygon vertices comprised between POS0 and
   POS1. The array INDS may be smaller than the array POLY2.

   Example
     Nv = 100;
     poly = circleToPolygon([30 20 15], Nv);
     arc1 = polygonSubcurve(poly, 15, 45);
     arc2 = polygonSubcurve(poly, 90, 10); % contains polygon endpoints
     figure; axis equal, hold on; axis([0 50 0 50]);
     drawPolyline(arc1, 'linewidth', 4, 'color', 'g');
     drawPolyline(arc2, 'linewidth', 4, 'color', 'r');
     drawPolygon(poly, 'color', 'b');

   See also
     polygons2d, polylineSubcurve, projPointOnPolygon, polygonPoint

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [res, inds] = polygonSubcurve(poly, t0, t1)
0002 % Extract a portion of a polygon.
0003 %
0004 %   POLY2 = polygonSubcurve(POLYGON, POS0, POS1)
0005 %   Create a new polyline, by keeping vertices located between positions
0006 %   POS0 and POS1, and adding points corresponding to positions POS0 and
0007 %   POS1 if they are not already vertices.
0008 %
0009 %   [POLY2, INDS] = polygonSubcurve(POLYGON, POS0, POS1)
0010 %   Also return indices of polygon vertices comprised between POS0 and
0011 %   POS1. The array INDS may be smaller than the array POLY2.
0012 %
0013 %   Example
0014 %     Nv = 100;
0015 %     poly = circleToPolygon([30 20 15], Nv);
0016 %     arc1 = polygonSubcurve(poly, 15, 45);
0017 %     arc2 = polygonSubcurve(poly, 90, 10); % contains polygon endpoints
0018 %     figure; axis equal, hold on; axis([0 50 0 50]);
0019 %     drawPolyline(arc1, 'linewidth', 4, 'color', 'g');
0020 %     drawPolyline(arc2, 'linewidth', 4, 'color', 'r');
0021 %     drawPolygon(poly, 'color', 'b');
0022 %
0023 %   See also
0024 %     polygons2d, polylineSubcurve, projPointOnPolygon, polygonPoint
0025 %
0026 
0027 % ------
0028 % Author: David Legland
0029 % e-mail: david.legland@inrae.fr
0030 % Created: 2009-04-30,    using Matlab 7.7.0.471 (R2008b)
0031 % Copyright 2009 INRAE - Cepia Software Platform.
0032 
0033 % number of vertices
0034 Nv = size(poly, 1);
0035 
0036 if t0 < t1
0037     % format positions
0038     t0 = max(t0, 0);
0039     t1 = min(t1, Nv);
0040 end
0041 
0042 % indices of extreme vertices inside subcurve
0043 ind0 = ceil(t0)+1;
0044 ind1 = floor(t1)+1;
0045 
0046 % get the portion of polyline between 2 extremities
0047 if t0 < t1
0048     % The result polyline does not contain the last vertex
0049     if ind1 <= Nv
0050         inds = ind0:ind1;
0051     else
0052         inds = 1;
0053     end
0054 else 
0055     % polygon contains last vertex
0056     inds = [ind0:Nv 1:ind1];
0057 end
0058 res = poly(inds, :);
0059 
0060 % add first point if it is not already a vertex
0061 if t0 ~= ind0-1
0062     res = [polygonPoint(poly, t0); res];
0063 end
0064 
0065 % add last point if it is not already a vertex
0066 if t1 ~= ind1-1
0067     res = [res; polygonPoint(poly, t1)];
0068 end
0069

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