Home > matGeom > polygons2d > clipPolyline.m

clipPolyline

PURPOSE ^

CLIPPOLYLINE Clip an open polyline with a rectangular box.

SYNOPSIS ^

function res = clipPolyline(poly, box)

DESCRIPTION ^

CLIPPOLYLINE Clip an open polyline with a rectangular box.

   POLY2 = clipPolyline(POLY, BOX);
   POLY is N-by-2 array of vertex coordinates.
   BOX has the form: [XMIN XMAX YMIN YMAX].
   Returns the set of polylines created by the intersection of the
   polyline POLY and the bounding box BOX. The result is a cell array with
   as many cells as the number of curve clips.


   Example
     circle = [5 5 6];
     poly = circleToPolygon(circle, 200);
     box = [0 10 0 10];
     res = clipPolyline(poly, box);
     figure;
     hold on; axis equal; axis([-2 12 -2 12]);
     drawCircle(circle, 'b:')
     drawBox(box, 'k')
     drawPolyline(res, 'linewidth', 2, 'color', 'b')
 
   See also
     polygons2d, boxes2d, clipPolygon, clipEdge

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function res = clipPolyline(poly, box)
0002 %CLIPPOLYLINE Clip an open polyline with a rectangular box.
0003 %
0004 %   POLY2 = clipPolyline(POLY, BOX);
0005 %   POLY is N-by-2 array of vertex coordinates.
0006 %   BOX has the form: [XMIN XMAX YMIN YMAX].
0007 %   Returns the set of polylines created by the intersection of the
0008 %   polyline POLY and the bounding box BOX. The result is a cell array with
0009 %   as many cells as the number of curve clips.
0010 %
0011 %
0012 %   Example
0013 %     circle = [5 5 6];
0014 %     poly = circleToPolygon(circle, 200);
0015 %     box = [0 10 0 10];
0016 %     res = clipPolyline(poly, box);
0017 %     figure;
0018 %     hold on; axis equal; axis([-2 12 -2 12]);
0019 %     drawCircle(circle, 'b:')
0020 %     drawBox(box, 'k')
0021 %     drawPolyline(res, 'linewidth', 2, 'color', 'b')
0022 %
0023 %   See also
0024 %     polygons2d, boxes2d, clipPolygon, clipEdge
0025 %
0026 
0027 % ------
0028 % Author: David Legland
0029 % E-mail: david.legland@inrae.fr
0030 % Created: 2005-05-14
0031 % Copyright 2005-2024 INRA - Cepia Software Platform
0032 
0033 % check case of polylines stored in cell array
0034 if iscell(poly)
0035     res = cell(1, length(poly));
0036     for i = 1:length(poly)
0037         res{i} = clipPolyline(poly{i}, box);
0038     end
0039     return;
0040 end
0041 
0042 % check case of empty polylines
0043 N = size(poly, 1);
0044 if N == 0
0045     res = cell(0, 0);
0046     return
0047 end
0048 
0049 % create edges array of polyline
0050 edges = [poly(1:N-1, :) poly(2:N, :)];
0051 
0052 % clip edges
0053 edges = clipEdge(edges, box);
0054 
0055 % select non empty edges, and get their vertices
0056 % find clipped edges within box
0057 inds = sum(abs(edges), 2) > 1e-14;
0058 
0059 % find list of adjacent edges within box
0060 dinds = diff(inds);
0061 inds0 = find(dinds == 1) + 1;
0062 if inds(1) == 1
0063     inds0 = [1 inds0];
0064 end
0065 inds1 = find(dinds == -1);
0066 if inds(end) == 1
0067     inds1 = [inds1 N-1];
0068 end
0069 
0070 nClips = length(inds0);
0071 res = cell(1, nClips);
0072 for iClip = 1:nClips
0073     range = inds0(iClip):inds1(iClip);
0074     poly2 = [edges(range, 1:2) ; edges(range(end), 3:4)];
0075     res{iClip} = poly2;
0076 end

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