Home > matGeom > polygons2d > clipPolygonHP.m

clipPolygonHP

PURPOSE ^

CLIPPOLYGONHP Clip a polygon with a Half-plane defined by a directed line.

SYNOPSIS ^

function poly2 = clipPolygonHP(poly, line)

DESCRIPTION ^

CLIPPOLYGONHP Clip a polygon with a Half-plane defined by a directed line.

   POLY2 = clipPolygonHP(POLY, LINE)
   POLY is a [Nx2] array of points, and LINE is given as [x0 y0 dx dy].
   The result POLY2 is also an array of points, sometimes smaller than
   poly, and that can be [0x2] (empty polygon).

   See also:
   polygons2d, clipPolygon

 ---------
 author : David Legland
 created the 31/07/2005.
 Copyright 2010 INRA - Cepia Software Platform.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function poly2 = clipPolygonHP(poly, line)
0002 %CLIPPOLYGONHP Clip a polygon with a Half-plane defined by a directed line.
0003 %
0004 %   POLY2 = clipPolygonHP(POLY, LINE)
0005 %   POLY is a [Nx2] array of points, and LINE is given as [x0 y0 dx dy].
0006 %   The result POLY2 is also an array of points, sometimes smaller than
0007 %   poly, and that can be [0x2] (empty polygon).
0008 %
0009 %   See also:
0010 %   polygons2d, clipPolygon
0011 %
0012 % ---------
0013 % author : David Legland
0014 % created the 31/07/2005.
0015 % Copyright 2010 INRA - Cepia Software Platform.
0016 %
0017 
0018 %   HISTORY
0019 %   15/08/2005 add test to avoid empty polygons
0020 %   13/06/2007 deprecate
0021 %   10/10/2008 'reprecate'
0022 
0023 
0024 % avoid to process empty polygons
0025 if size(poly, 1)<3
0026     poly2 = zeros([0 2]);
0027     return;
0028 end
0029 
0030 % ensure the last point is the same as the first one
0031 if sum(poly(end, :)==poly(1,:))~=2
0032     poly = [poly; poly(1,:)];
0033 end
0034 
0035 N = size(poly, 1);
0036 edges = [poly([N 1:N-1], :) poly];
0037 
0038 b = isLeftOriented(poly, line);
0039 
0040 % case of totally clipped polygon
0041 if sum(b)==0
0042     poly2 = zeros(0, 2);
0043     return;
0044 end
0045  
0046 
0047 poly2 = zeros(0, 2);
0048 
0049 i=1;
0050 while i<=N
0051     
0052     if isLeftOriented(poly(i,:), line)
0053         % keep all points located on the right side of line
0054         poly2 = [poly2; poly(i,:)]; %#ok<AGROW>
0055     else
0056         % compute of preceeding edge with line
0057         if i>1
0058             poly2 = [poly2; intersectLineEdge(line, edges(i, :))]; %#ok<AGROW>
0059         end    
0060         
0061         % go to the next point on the left side
0062         i=i+1;
0063         while i<=N
0064             
0065             % find the next point on the right side
0066             if isLeftOriented(poly(i,:), line)
0067                 % add intersection of previous edge
0068                 poly2 = [poly2; intersectLineEdge(line, edges(i, :))]; %#ok<AGROW>
0069                 
0070                 % add current point
0071                 poly2 = [poly2; poly(i,:)]; %#ok<AGROW>
0072                 
0073                 % exit the second loop
0074                 break;
0075             end
0076             
0077             i=i+1;
0078         end
0079     end
0080     
0081     i=i+1;
0082 end
0083 
0084 % remove last point if it is the same as the first one
0085 if sum(poly2(end, :)==poly(1,:))==2
0086     poly2 = poly2(1:end-1, :);
0087 end
0088

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