Code covered by the BSD License  

Highlights from
cutpolygon

4.0

4.0 | 1 rating Rate this file 9 Downloads (last 30 days) File Size: 5.39 KB File ID: #24449
image thumbnail

cutpolygon

by

 

16 Jun 2009 (Updated )

Cut alias slice alias split a 2D surface polygon by a line, and remove the specified part.

| Watch this File

File Information
Description

CUTPOLYGON - Split a 2D polygon by a line, and remove one of the sides

Use CUTPOLYGON to cut alias intersect alias split alias slice a polygon P
(being a series of connected X,Y coordinates) with a line L (defined by
two points), removing a specified side s. L can serve as a bottom limit
('B'), top limit ('T'), left limit ('L'), or right limit ('R').

Syntax:
  Pc = CUTPOLYGON(P, L, s, doSplit, doPlot, doTable)

Demo (cut random regular polygon with random line):
  CUTPOLYGON demo

Inputs:
  P Polygon coordinates [X, Y]
  L Line defined by two coordinates [x1, y1; x2, y2]
  s What side to remove, character or integer

Optional switches:
  doSplit Add intermediate NaN entries if the polygon is split into non-connected parts (default false)
  doPlot Plot original and cut polygon plus line (default false)
  doTable Tabulate intersection and validity per polygon segment (default false)

Side options:
  1 / B = bottom remove parts Y < intersection
  2 / T = top remove parts Y > intersection
  3 / L = left remove parts X < intersection
  4 / R = right remove parts X > intersection

Finding the intersection:
  http://en.wikipedia.org/wiki/Line-line_intersection

Output:
  - Pc is the polygon post-cut [X, Y]
  - Pc can be shorter than P (points are removed)
  - Pc can contain intermediate NaN entries if doSplit is true

Version history (recent to ancient):
  Jan 2010, Dominik Brands, fixed pure horizontal/vertical limit bug
  Apr 2009, Jasper Menger , creation

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
07 Jan 2010 Dominik Brands

Sorry, I made a mistake during the copy/paste of the code
The second part must be:
elseif xx3 > xx1
% modified/add lines
% compute intersection point
X3(i) = xx1; xp = xx1;
yp = interp1([xx3, xx4], [yy3, yy4], xx1, 'linear', 'extrap');
Y3(i) = yp;
% Original code
% X3(i) = xx1; xp = xx1; yp = yy3;
elseif xx4 > xx1
% modified/add lines
% compute intersection point
X4(i) = xx1; xp = xx1;
yp = interp1([xx3, xx4], [yy3, yy4], xx1, 'linear', 'extrap');
Y4(i) = yp;
% Original code
% X4(i) = xx1; xp = xx1; yp = yy4;

I think, a similar code must be add to the part for horizontal cut-lines.

06 Jan 2010 Dominik Brands

Hi,
cutpolygon is a very helpful tool,
but I found one thing I do not understand.
If I cut a polygon by a vertical line, the intersection points
will not be computed correct. The value for the y-coordinate
will be taken from the first point which will be cut-out.

I modify your code at the following lines:
elseif xx3 < xx1
% modified/add lines
% compute intersection point
X3(i) = xx1; xp = xx1;
yp = interp1([xx3, xx4], [yy3, yy4], xx1, 'linear', 'extrap');
Y3(i) = yp;
%r Original code
% X3(i) = xx1; xp = xx1; yp = yy3;
elseif xx4 < xx1
% modified/add lines
% compute intersection point
X4(i) = xx1; xp = xx1;
yp = interp1([xx3, xx4], [yy3, yy4], xx1, 'linear', 'extrap');
Y4(i) = yp;
% Original code
% X4(i) = xx1; xp = xx1; yp = yy4;

and:
elseif xx3 > xx1
% modified/add lines
% compute intersection point
xp = interp1([yy3, yy4], [xx3, xx4], yy1, 'linear', 'extrap');
X3(i) = xp; yp = yy3;
% Original code
% X3(i) = xx1; xp = xx1; yp = yy3;
elseif xx4 > xx1
% modified/add lines
% compute intersection point
xp = interp1([yy3, yy4], [xx3, xx4], yy1, 'linear', 'extrap');
X4(i) = xp; yp = yy4;
% Original code
% X4(i) = xx1; xp = xx1; yp = yy4;

I hope that this would be a helpful comment for you.

Updates
07 Jan 2010

Fixed bug for pure horizontal/vertical limits, as suggested by Dominik Brands.

Contact us