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
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 % created the 14/05/2005. 0030 % Copyright 2010 INRA - Cepia Software Platform. 0031 0032 % HISTORY 0033 % 2007/09/14 fix doc 0034 0035 % check case of polylines stored in cell array 0036 if iscell(poly) 0037 res = cell(1, length(poly)); 0038 for i = 1:length(poly) 0039 res{i} = clipPolyline(poly{i}, box); 0040 end 0041 return; 0042 end 0043 0044 % check case of empty polylines 0045 N = size(poly, 1); 0046 if N == 0 0047 res = cell(0, 0); 0048 return 0049 end 0050 0051 % create edges array of polyline 0052 edges = [poly(1:N-1, :) poly(2:N, :)]; 0053 0054 % clip edges 0055 edges = clipEdge(edges, box); 0056 0057 % select non empty edges, and get their vertices 0058 % find clipped edges within box 0059 inds = sum(abs(edges), 2) > 1e-14; 0060 0061 % find list of adjacent edges within box 0062 dinds = diff(inds); 0063 inds0 = find(dinds == 1) + 1; 0064 if inds(1) == 1 0065 inds0 = [1 inds0]; 0066 end 0067 inds1 = find(dinds == -1); 0068 if inds(end) == 1 0069 inds1 = [inds1 N-1]; 0070 end 0071 0072 nClips = length(inds0); 0073 res = cell(1, nClips); 0074 for iClip = 1:nClips 0075 range = inds0(iClip):inds1(iClip); 0076 poly2 = [edges(range, 1:2) ; edges(range(end), 3:4)]; 0077 res{iClip} = poly2; 0078 end