<?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>  Cohen-Sutherland Line-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>
Cohen-Sutherland Line-Clipping Algorithm
<a href="../../../../index.html">
<img alt="home" src="../../../comsci/images/Frame.gif" /> </a>
</h1>



<p> Clips lines outside a rectangle region. Generalizes
 to a volume in 3D space. 
</p>

<p> I will simply refer to the Cohen-Sutherland line clipping
 algorithm as "the algorithm" as I am documenting it. 
 For a formal treatment refer to "Computer Graphics Principles
 and Practice" by Foly, van Dam, Feiner and Hughes second edition 
 page 113.
</p>

<p>
Firstly clipping is an end state in the sense
 that other intersection points could be first
 calculated. If the algorithm cuts a line (calculates
 an intersection point) it does so on lines generated
 from the regions edges extending infinity. 
</p>

<p>
Indeed the algorithm divides area into 9 regions by extending 
 the rectangular regions edges infinitely. 
 A point is classified into one of these regions. Points
 on the boundary belong to either region and the
 algorithm exploits this.
</p>

<div class="spacer">
</div>


<div class="float">
<img src="cs_line01.png" alt="cs_line01.png" />
</div>


<div class="float20">

<p>
Let 

<math xmlns="&mathml;">
  <mo>(</mo>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>,</mo>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>)</mo>
</math>

 and 

<math xmlns="&mathml;">
  <mo>(</mo>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>,</mo>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
</math>

 be the region's lower and upper coordinates.

 The region is classified by four comparison tests
 stored into a number.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>n</mi>
  <mo>=</mo>
  <mi>0</mi>
</math>

<br/>
<math xmlns="&mathml;">
  <mi>n</mi>
  <mo>=</mo>
  <mi>n</mi>
  <mo>+</mo>
  <mo>(</mo>
  <mi>y</mi>
  <mo>&gt;</mo>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
</math>

<br/>
<math xmlns="&mathml;">
  <mi>n</mi>
  <mo>=</mo>
  <mi>n</mi>
  <mo>+</mo>
  <mo>2</mo>
  <mo>(</mo>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>y</mi>
  <mo>)</mo>
</math>

<br/>
<math xmlns="&mathml;">
  <mi>n</mi>
  <mo>=</mo>
  <mi>n</mi>
  <mo>+</mo>
  <mo>4</mo>
  <mo>(</mo>
  <mi>x</mi>
  <mo>&gt;</mo>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>)</mo>
</math>

<br/>
<math xmlns="&mathml;">
  <mi>n</mi>
  <mo>=</mo>
  <mi>n</mi>
  <mo>+</mo>
  <mo>8</mo>
  <mo>(</mo>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>x</mi>
  <mo>)</mo>
</math>
</p>

<p> Define a function that given any 
 point returns the region number.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>reg</mi>
  <mo>(</mo>
  <mi>X</mi>
  <mo>)</mo>
</math>
</p>

</div>


<div class="spacer">
</div>

<h2> Global view </h2>

<p>
The algorithm processes lines. A line is a pair of points.
 Let there be three containers : U for the initial set
 of pairs of points, T for trashed pairs of points,
 and A for accepted pairs of points.
</p>

<p>
The algorithm processes a pair of points by considering one
 pair at a time. Three possibilities emerge.
 1. clearly reject 2. clearly accept 3. further processing .
</p>

<p> 
Label a pair of points: the first is A and the second
 is B.
</p>

<h3>
Clearly Reject Test </h3>

<p class="equ">
<math xmlns="&mathml;">
  <mi>reg</mi>
  <mo>(</mo>
  <mi>A</mi>
  <mo>)</mo>
  <mo>&amp;&amp;</mo>
  <mi>reg</mi>
  <mo>(</mo>
  <mi>B</mi>
  <mo>)</mo>
  <mo>&NotEqual;</mo>
  <mi>0</mi>
</math>

</p>

<p> See the region mapping diagram to understand why. </p>

<h3>
Clearly Accept Test </h3>

<p class="equ">
<math xmlns="&mathml;">
  <mi>reg</mi>
  <mo>(</mo>
  <mi>A</mi>
  <mo>)</mo>
  <mo>+</mo>
  <mi>reg</mi>
  <mo>(</mo>
  <mi>B</mi>
  <mo>)</mo>
  <mo>==</mo>
  <mi>0</mi>
</math>

</p>

<h3>
Undecided </h3>

<p>
Here's where it gets interesting. The line
 is divided in two. The current point is
 removed from U and trashed (goes to T).
 But from it two new lines are created and
 added to U, and the algorithm goes on.
</p>


<h3> Non-uniqueness of a Point </h3>

<p>
Ok - wheres the excitement? The algorithm divides
 the line on a region boundary say at a new point K.
 Since K is in both regions and the are two possibilities
 of K - say K- and K+ for opposite regions.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mo>(</mo>
  <mi>A</mi>
  <mo>,</mo>
  <mi>B</mi>
  <mo>)</mo>
  <mo>=</mo>
  <mo>(</mo>
  <mi>A</mi>
  <mo>,</mo>
  <mi>K-</mi>
  <mo>)</mo>
  <mo>+</mo>
  <mo>(</mo>
  <mi>K+</mi>
  <mo>,</mo>
  <mi>B</mi>
  <mo>)</mo>
</math>


</p>

<p>
Lets think of the consequences of the split. If (A,B) intersected
 a regions edge it would split the line at the edge and
 the line outside the region must be rejected on the next
 algorithm pass.
</p>

<p>
If the split line doesn't intersect a regions edge
 it intersects the boundary that it was split on.
 Since this boundary deletes a line if its on one 
 side and one of the new lines is, it will be deleted.
</p>

<p>
The above two cases are all the possibilities - excluding 
 lines parallel to the x and y axes on borders. 
</p>


<h3> Clipping Order </h3>

<p>
Cohen-Sutherland algorithm chooses an ordering on how the
 line is split. At least one of the points is outside
 the clipping square with opcode 0. Call this point p1 and
 the other p2.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>bool</mi>
  <mi>const</mi>
  <mi>isleft</mi>
  <mo>(</mo>
  <mi>point</mi>
  <mi>&amp;</mi> 
  <mi>p</mi>
  <mo>)</mo>
</math> <br/>
 &nbsp; 
 { return
<math xmlns="&mathml;">
  <mo>(</mo>
  <mi>p.opcode</mi>
  <mo>&amp;&amp;</mo>
  <mi>1</mi>
  <mo>)</mo> 
</math> &NotEqual; 0; }
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>bool</mi>
  <mi>const</mi>
  <mi>isright</mi>
  <mo>(</mo>
  <mi>point</mi>
  <mi>&amp;</mi> 
  <mi>p</mi>
  <mo>)</mo>
</math> <br/>
 &nbsp; 
  { return 
<math xmlns="&mathml;">
  <mo>(</mo>
  <mi>p.opcode</mi> 
  <mo>&amp;&amp;</mo> <mi>2</mi><mo>)</mo>
</math>
  &NotEqual; 0 }
</p>


<p class="equ">
<math xmlns="&mathml;">
  <mi>bool</mi>
  <mi>const</mi>
  <mi>isabove</mi>
  <mo>(</mo>
  <mi>point</mi>
  <mi>&amp;</mi> 
  <mi>p</mi>
  <mo>)</mo>
</math> <br/>
 &nbsp;  { return 
<math xmlns="&mathml;">
  <mo>(</mo>
  <mi>p.opcode</mi>
  <mo> &amp;&amp;</mo>
  <mi>8</mi>
  <mo>)</mo>
</math>
  &NotEqual; 0 }
</p>


<p class="equ">
<math xmlns="&mathml;">
  <mi>bool</mi>
  <mi>const</mi>
  <mi>isbelow</mi>
  <mo>(</mo>
  <mi>point</mi>
  <mi>&amp;</mi> 
  <mi>p</mi>
  <mo>)</mo>
</math> <br/>
 &nbsp; 
 { return 
<math xmlns="&mathml;">
   <mo>(</mo>
   <mi>p.opcode</mi>
   <mo>&amp;&amp;</mo> 
   <mi>4</mi>
   <mo>)</mo> 
</math> 
 &NotEqual; 0; }
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>void</mi>
  <mi>undecided</mi>
  <mo>(</mo>
  <mi>point</mi>
  <mi>&amp;</mi>
  <mi>p1</mi>
  <mi>,</mi>
  <mi>point</mi>
  <mi>const</mi>
  <mi>&amp;</mi>
  <mi>p2</mi>
  <mo>)</mo>
</math>
<br/>
<math xmlns="&mathml;">
  <mi>{</mi>
</math>
<br/>

 &nbsp; &nbsp; 
<math xmlns="&mathml;">
  <mi>if</mi>
  <mi>isleft</mi>
  <mo>(</mo>
  <mi>p1</mi>
  <mo>)</mo>
</math>
<br/>
 &nbsp; &nbsp; &nbsp; &nbsp; Chop against left edge, return.
<br/>
<br/>


 &nbsp; &nbsp; 
<math xmlns="&mathml;">
  <mi>if</mi>
  <mi>isright</mi>
  <mo>(</mo>
  <mi>p1</mi>
  <mo>)</mo>
</math>
<br/>
 &nbsp; &nbsp; &nbsp; &nbsp; Chop against right edge, return.
<br/>
<br/>


 &nbsp; &nbsp; 
<math xmlns="&mathml;">
  <mi>if</mi>
  <mi>isabove</mi>
  <mo>(</mo>
  <mi>p1</mi>
  <mo>)</mo>
</math>
<br/>
 &nbsp; &nbsp; &nbsp; &nbsp; Chop against top edge, return.
<br/>
<br/>


 &nbsp; &nbsp; 
<math xmlns="&mathml;">
  <mi>if</mi>
  <mi>isbottom</mi>
  <mo>(</mo>
  <mi>p1</mi>
  <mo>)</mo>
</math>
<br/>
 &nbsp; &nbsp; &nbsp; &nbsp; Chop against bottom edge, return.
<br/>
<br/>

<math xmlns="&mathml;">
  <mi>}</mi>
</math>

<br/>
<br/>
p1 is a point inside(on border) of clipping region. eg
 has opcode 0.
</p>










</body>
</html>


