<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" 
               "http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd" [
  <!ENTITY mathml "http://www.w3.org/1998/Math/MathML">
]>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<link rel="stylesheet" type="text/css" href="../../../comsci/css/s011.css"/>
<link rel='SHORTCUT ICON' href='../../../comsci/images/FrameHome.ico' />
<style type="text/css">
</style>
<title> Sutherland-Hodgman Polygon Clipping Algorithm </title>
</head>

<body>


<div class="message_right">
  <a href="http://validator.w3.org/check/referer">
    <img src="http://www.w3.org/Icons/valid-xhtml11" alt="Valid
	XHTML 1.1!" height="31" width="88" />
  </a>

  <a href="http://jigsaw.w3.org/css-validator/">
    <img style="width:88px;height:31px"
       src="http://jigsaw.w3.org/css-validator/images/vcss" 
       alt="Valid CSS!" />
  </a>
  <br/>
  Created 2004-06-01  &nbsp; Modified 
<!--UPDATE_DATE_MODIFIED-->
<!--UPDATE_DATE_BEGIN-->
2007-01-01
<br/>
<a href="mailto:chelton.evans@yahoo.com">Chelton Evans</a>
<!--UPDATE_DATE_END-->
</div>

<h1> 
<a href="geom.html"> <img alt="proj" src="../../../comsci/images/compgeom.png" /></a>
Sutherland-Hodgman Polygon 
 Clipping Algorithm
<a href="../../../../index.html">
<img alt="home" src="../../../comsci/images/Frame.gif" /> </a>
</h1>



<div class="float25">

<p>
Clips a polygon against a convex
 polygon. The clipped polygon
 may fracture into other polygons.
</p>

<p>
The convexity of the clipper is
 because you are iterating over
 each cutting edge.  For the 
 cutting edges to be independent
 you need a convex cutting shape.
</p>

<p>
This simplifies the algorithm somewhat.
  All I have to do is describe the
 cutting of the polygon by one
 of the cutting edges and since its
 independent of the others the
 algorithm is repeated for them.
</p>


<p>
Cutting a polygon in 2D is done by
 a line. Either the point being
 tested is inside the clipping
 region  or outside.
</p>

<h2> Algorithm </h2>

<p class="equ">
 Have a vector <code>v</code> of points which
 describe the polygon. <br/>
 Push back the first point of 
 <code>v</code> as another point 
 terminating the polygon vector <code>v</code>. 

 For algorithm iteration purposes.
<br/>
 Let the point have an <code>bool inside</code>
 attribute. <br/>
 Select any cutting edge <code>ce</code>.
 Iterate over <code>v</code> initializing
 <code>inside</code> relative to 
 <code>ce</code>. <br/>
</p>

</div>

<div class="float25">
<p>
This sets up the data structure so that
 a linear transversal generates the
 new polygon vector.  Here is an
 example.
</p>

<table>
<tr> 
<td>i</td><td>0</td><td>1</td><td>2</td>
<td>3</td><td>4</td>
</tr>
<tr>
<td>inside</td>
<td>0</td><td>1</td><td>0</td><td>1</td>
<td>0</td>
</tr>

</table>

<img src="diagg00901.png" alt="diagg00901.png" />


<p class="equ">
 Let <code>v2</code> be the new
 polygon vector having been clipped
 by <code>ce</code>. <br/>
 Have a routine to find the intersection
 across the cutting line.  eg &nbsp; 
<code>point intersection(point p1, 
 point p2)</code> . 
</p>

<p>
 There are 4 cases to consider as
 each line is a set of points and
 a point has 2 possibilities (inside
 or out).  2 by 2 is 4 possibilities.
 For convenience I have used a 2 bit
 binary number in the algorithm where
 the first is <code>v[i-1].inside</code>
 and the second is <code>v[i].inside</code>.
</p>

<p class="equ">
<code>
for ( i=1; i&lt;v.size(); ++i ) <br/>

 &nbsp; 10 - v2.push_back(
  intersection(v[i],v[i-1]) ) <br/>
 &nbsp; 01 - v2.push_back( 
  intersection(v[i],v[i-1]) ),
    v2.push_back(v[i]). <br/>
  &nbsp; 11 -  v2.push_back(v[i]) <br/>
  &nbsp; 00 - do nothing.
</code>

</p>

</div>

<div class="spacer" />


<div class="float25">

<h2> Hand Trace </h2>

<table>
<tr>
<td>01</td> </tr>
<tr>
<td>01</td> <td>a</td> <td>1</td>
</tr>
</table>

<br/>

<table>
<tr>
<td>12</td> </tr>
<tr>
<td>10</td> <td>a</td> <td>1</td> 
<td>b</td>
</tr>
</table>

<br/>

<table>
<tr>
<td>23</td> </tr>
<tr>
<td>01</td> <td>a</td> <td>1</td> 
<td>b</td> 
<td>c</td> <td>3</td>
</tr>
</table>


<br/>

<table>
<tr>
<td>34</td> </tr>
<tr>
<td>10</td> <td>a</td> <td>1</td> 
<td>b</td>  
<td>c</td> <td>3</td>  
<td>d</td>
</tr>
</table>



<h2> Another Perspective </h2>

<p>
I have accidental written about
 this algorithm twice, here's
 my first try.
</p>

<p>
<a href="g006.xml"> Sutherland-Hodgman
 Polygon Clipping Algorithm </a>

</p>

<p>&lt;TODO&gt; - rewrite in a much better and clearer way,
 merging both docs.
</p>

</div>

</body>
</html>


