SlideShare a Scribd company logo
1 of 35
CSC 406: Applied
Computer Graphics
LECTURE 8:
Clipping
Lecture 8:
 Scope:
 Clipping
 Clipping algorithms
Clipping
 We have talked about 2D scan conversion of
line-segments and polygons
 What if endpoints of line segments or vertices
of polygons lie outside the visible device
region?
 Need clipping!
 Clipping .pdf notes
Clipping
 Clipping: Remove points outside a region of interest – clip
window.
 Want to discard everything that’s outside of our window...
 Primitives: point, line-segment, and polygon.
 Point clipping: Remove points outside window.
 A point is either entirely inside the region or not.
 Line-segment clipping: Remove portion of line segment outside
window.
 Line segments can straddle the region boundary.
 Polygon clipping: Remove portion of polygon outside window
Review: Parametric
representation of line
or equivalently
 A and B are non-coincident endpoints.
 For , L(t) defines an infinite line.
 For , L(t) defines a line segment from A to B.
 Good for generating points on a line.
 Not so good for testing if a given point is on a line.
A
B
Review: Implicit line Equation
representation of line:
•P is a point on the line.
• is a vector perpendicular to the line.
• gives us the signed distance from any point Q to the line.
•The sign of tells us if Q is on the left or right of the line, relative to the
direction of .
•If is zero, then Q is on the line.
•Use same form for the implicit representation of a half space.
Clipping a point to a half space
 Represent window edges implicitly.
 Use the implicit form of a line to classify a point Q.
 Must choose a convention for the normal:
points to the inside.
Check the sign of :
 If > 0, then Q is inside.
 Otherwise clip (discard) Q: It is on the edge or outside.
Clipping a line segment to a
half space
 There are three cases:
 The line segment is entirely inside: Keep it.
 The line segment is entirely outside: Discard it.
 The line segment is partially inside and partially outside:
Generate new line to represent part inside.
Input Specification
•Window edge: implicit,
•Line segment: parametric, L(t) = A + t(B-A).
Do the easy stuff first
 Do the easy stuff first:

We can devise easy (and fast!) tests for the first two cases:
 AND Outside
 AND Inside
 Need to decide whether “on the boundary” is inside or
outside?
 Trivial tests are important in computer graphics:
 Particularly if the trivial case is the most common one.
 Particularly if we can reuse the computation for the non-
trivial case
Do the hard stuff only if we
have to
If line segment partially inside and partially outside, need to clip it:
•Line segment from A to B in parametric form:
•When t=0, L(t)=A. When t=1, L(t)=B.
•We now have the following:
Find the Intersection
•We want t such that :
•Solving for t gives us
•NOTE:
The values we use for our simple test can be reused to compute t:
Clipping a line segment to a
window
 Just clip to each of four half spaces in turn.
given: line segment (A, B), clip in-place:
for each edge (P,n)
wecA = (A-P) . n
wecB = (B-P) . n
if ( wecA < 0 AND wecB < 0 ) then reject
if ( wecA > 0 AND wecB > 0 ) then next
t = wecA / (wecA - wecB)
if ( wecA < 0 ) then
A = A + t*(B-A)
else
B = A + t*(B-A)
endif
endfor
 Note:
 This Algorithm can clip lines to any convex window.
 Optimizations can be made for the special case of horizontal and vertical
window edges
Optimization (Cohen-
Sutherland)
 Q: Solving for the intersection point is expensive.
Can we avoid evaluation of a clip on one edge if we
know the line segment is out on another?
 A: Yes.
 Do all trivial tests first.
 Combine results using Boolean operations to determine
 Trivial accept on window: all edges have trivial accept
 Trivial reject on window: any edge has trivial reject
 Do full clip only if no trivial accept/reject on window
Cohen-Sutherland Algorithm
0
1
2 3
0000
00010101 1001
0100
1000
00100110 1010
(x1, y1)
(x2, y2)
(x2, y1)
(x1, y2)
Half space code (x < x2) | (x > x1) | (y > y1) | (y < y2)
Outcodes
Cohen-Sutherland Algorithm
 Computing the outcodes for a point is trivial
 Just use comparison
 Trivial rejection is performed using the logical
and of the two endpoints
 A line segment is rejected if the result of and
operation > 0. Why?
Using Outcodes
 Consider the 5 cases below
 AB: outcode(A) = outcode(B) = 0
 Accept line segment
Using Outcodes
 CD: outcode (C) = 0, outcode(D) ≠ 0
 Compute intersection
 Location of 1 in outcode(D) determines which
edge to intersect with
 Note if there were a segment from A to a point in
a region with 2 ones in outcode, we might have to
do two interesections
Using Outcodes
 EF: outcode(E) logically ANDed with
outcode(F) (bitwise) ≠ 0
 Both outcodes have a 1 bit in the same place
 Line segment is outside of corresponding side of
clipping window
 reject
Using Outcodes
 GH and IJ: same outcodes, neither zero but
logical AND yields zero
 Shorten line segment by intersecting with one
of sides of window
 Compute outcode of intersection (new
endpoint of shortened line segment)
 Re-execute algorithm
Cohen-Sutherland Algorithm
 Now we can efficiently reject lines completely
to the left, right, top, or bottom of the
rectangle.
 If the line cannot be trivially rejected (what
cases?), the line is split in half at a clip line.
 Not that about one half of the line can be
rejected trivially
 This method is efficient for large or small
windows.
Cohen-Sutherland Algorithm
clip (int Ax, int Ay, int Bx, int By)
{
int cA = code(Ax, Ay);
int cB = code(Bx, By);
while (cA | cB) { // not completely inside the window
if(cA & cB) return; // rejected
if(cA) {
update Ax, Ay to the clip line depending
on which outer region the point is in
cA = code(Ax, Ay);
} else {
update Bx, By to the clip line depending
on which outer region the point is in
cB = code(Bx, By);
}
}
drawLine(Ax, Ay, Bx, By);
}
3D Line-clip Algorithm
• Half-space now lies on one side of a plane.
• Implicit formula for plane in 3D is same as that for line in 2D.
• Parametric formula for line to be clipped is unchanged.
– Use 6-bit outcodes
– When needed, clip line segment against planes
Polygon Clipping
 Not as simple as line segment clipping
 Clipping a line segment yields at most one line
segment
 Clipping a polygon can yield multiple polygons
 However, clipping a convex polygon can yield
at most one other polygon
Tessellation and Convexity
 One strategy is to replace nonconvex (concave)
polygons with a set of triangular polygons (a
tessellation)
 Also makes fill easier
 Tessellation code in GLU library
Pipeline Clipping of Polygons
 Three dimensions: add front and back clippers
 Polygon Clipping (Sutherland-Hodgman):
 Window must be a convex polygon
 Polygon to be clipped can be convex or not
 Approach:
 Polygon to be clipped is given as a sequence of vertices
 Each polygon edge is a pair
 Don't forget wrap around; is also an edge
 Process all polygon edges in succession against a window edge
 Polygon in - polygon out
 Repeat on resulting polygon with next sequential window edge
 Sutherland-Hodgman algorithm deals with concave polygons efficiently
 Built upon efficient line segment clipping
Sutherland-Hodgman Polygon
Clipping Algorithm
Four Cases
 There are four cases to consider
 Some notations:
 s = vj is the polygon edge starting vertex
 p = vj+1 is the polygon edge ending vertex
 i is a polygon-edge/window-edge intersection point
 wj is the next polygon vertex to be output
Case 1
Polygon edge is entirely inside the window edge
•p is next vertex of resulting polygon
Case 2
Polygon edge crosses window edge going out
• Intersection point i is next vertex of resulting polygon
•
Case 3
Polygon edge is entirely outside the window edge
•No output
Case 4
Polygon edge crosses window edge going in
• Intersection point i and p are next two vertices of resulting polygon
•
Summary of cases
An Example with a non-convex
polygon
Summary of Clipping
 Point Clipping
 Simple: in or out
 Line Clipping:
 Based on point clipping
 Compute the line segment inside the window
 Need to compute intersections between the line segment and window edges.
 Consider trivial cases first to reduce computation.
 outcodes algorithm
 Polygon Clipping:
 More complex. A convex polygon may be clipped into multiple polygons.
 Based on line clipping
 Clip the polygon against window edges in a sequence

More Related Content

What's hot

Unit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdfUnit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdfAmol Gaikwad
 
Line drawing algo.
Line drawing algo.Line drawing algo.
Line drawing algo.Mohd Arif
 
Clipping Algorithm In Computer Graphics
Clipping Algorithm In Computer GraphicsClipping Algorithm In Computer Graphics
Clipping Algorithm In Computer Graphicsstudent(MCA)
 
Ellipses drawing algo.
Ellipses drawing algo.Ellipses drawing algo.
Ellipses drawing algo.Mohd Arif
 
2D Transformation
2D Transformation2D Transformation
2D TransformationShahDhruv21
 
Raster Scan Graphics, Line Drawing Algorithm and Circle Drawing Algorithm
Raster Scan Graphics, Line Drawing Algorithm and Circle Drawing Algorithm Raster Scan Graphics, Line Drawing Algorithm and Circle Drawing Algorithm
Raster Scan Graphics, Line Drawing Algorithm and Circle Drawing Algorithm Aparna Joshi
 
Clipping in Computer Graphics
Clipping in Computer GraphicsClipping in Computer Graphics
Clipping in Computer GraphicsLaxman Puri
 
Mid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsMid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsDrishti Bhalla
 
2d-transformation
2d-transformation2d-transformation
2d-transformationPooja Dixit
 
Chapter 3 Output Primitives
Chapter 3 Output PrimitivesChapter 3 Output Primitives
Chapter 3 Output PrimitivesPrathimaBaliga
 
COMPUTER GRAPHICS-"Projection"
COMPUTER GRAPHICS-"Projection"COMPUTER GRAPHICS-"Projection"
COMPUTER GRAPHICS-"Projection"Ankit Surti
 
Perspective projection
Perspective projectionPerspective projection
Perspective projectionPranjalDas25
 
Cohen-Sutherland Line Clipping Algorithm
Cohen-Sutherland Line Clipping AlgorithmCohen-Sutherland Line Clipping Algorithm
Cohen-Sutherland Line Clipping AlgorithmMaruf Abdullah (Rion)
 
2D viewing & clipping
2D viewing & clipping2D viewing & clipping
2D viewing & clippingMdAlAmin187
 
3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics Fundamentals3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics FundamentalsMuhammed Afsal Villan
 
Back face detection
Back face detectionBack face detection
Back face detectionPooja Dixit
 
Dilation and erosion
Dilation and erosionDilation and erosion
Dilation and erosionAswin Pv
 

What's hot (20)

Unit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdfUnit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdf
 
Line drawing algo.
Line drawing algo.Line drawing algo.
Line drawing algo.
 
Clipping Algorithm In Computer Graphics
Clipping Algorithm In Computer GraphicsClipping Algorithm In Computer Graphics
Clipping Algorithm In Computer Graphics
 
03.Scan Conversion.ppt
03.Scan Conversion.ppt03.Scan Conversion.ppt
03.Scan Conversion.ppt
 
Ellipses drawing algo.
Ellipses drawing algo.Ellipses drawing algo.
Ellipses drawing algo.
 
2D Transformation
2D Transformation2D Transformation
2D Transformation
 
Raster Scan Graphics, Line Drawing Algorithm and Circle Drawing Algorithm
Raster Scan Graphics, Line Drawing Algorithm and Circle Drawing Algorithm Raster Scan Graphics, Line Drawing Algorithm and Circle Drawing Algorithm
Raster Scan Graphics, Line Drawing Algorithm and Circle Drawing Algorithm
 
Clipping in Computer Graphics
Clipping in Computer GraphicsClipping in Computer Graphics
Clipping in Computer Graphics
 
Mid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsMid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer Graphics
 
Clipping
ClippingClipping
Clipping
 
2d-transformation
2d-transformation2d-transformation
2d-transformation
 
Chapter 3 Output Primitives
Chapter 3 Output PrimitivesChapter 3 Output Primitives
Chapter 3 Output Primitives
 
COMPUTER GRAPHICS-"Projection"
COMPUTER GRAPHICS-"Projection"COMPUTER GRAPHICS-"Projection"
COMPUTER GRAPHICS-"Projection"
 
Perspective projection
Perspective projectionPerspective projection
Perspective projection
 
Cohen-Sutherland Line Clipping Algorithm
Cohen-Sutherland Line Clipping AlgorithmCohen-Sutherland Line Clipping Algorithm
Cohen-Sutherland Line Clipping Algorithm
 
Polygon filling
Polygon fillingPolygon filling
Polygon filling
 
2D viewing & clipping
2D viewing & clipping2D viewing & clipping
2D viewing & clipping
 
3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics Fundamentals3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics Fundamentals
 
Back face detection
Back face detectionBack face detection
Back face detection
 
Dilation and erosion
Dilation and erosionDilation and erosion
Dilation and erosion
 

Viewers also liked

computer graphics
computer graphicscomputer graphics
computer graphicsashpri156
 
Introduction to Computer graphics
Introduction to Computer graphics Introduction to Computer graphics
Introduction to Computer graphics PrathimaBaliga
 

Viewers also liked (13)

Computer Graphics display technologies(Computer graphics tutorials)
Computer Graphics display technologies(Computer graphics tutorials)Computer Graphics display technologies(Computer graphics tutorials)
Computer Graphics display technologies(Computer graphics tutorials)
 
lecture3 color representation in computer graphics(Computer graphics tutorials)
lecture3 color representation in computer graphics(Computer graphics tutorials)lecture3 color representation in computer graphics(Computer graphics tutorials)
lecture3 color representation in computer graphics(Computer graphics tutorials)
 
Csc406 lecture7 device independence and normalization in Computer graphics(Co...
Csc406 lecture7 device independence and normalization in Computer graphics(Co...Csc406 lecture7 device independence and normalization in Computer graphics(Co...
Csc406 lecture7 device independence and normalization in Computer graphics(Co...
 
java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)
 
fundamentals of Computer graphics(Computer graphics tutorials)
 fundamentals of Computer graphics(Computer graphics tutorials) fundamentals of Computer graphics(Computer graphics tutorials)
fundamentals of Computer graphics(Computer graphics tutorials)
 
lecture4 raster details in computer graphics(Computer graphics tutorials)
lecture4 raster details in computer graphics(Computer graphics tutorials)lecture4 raster details in computer graphics(Computer graphics tutorials)
lecture4 raster details in computer graphics(Computer graphics tutorials)
 
An introduction to java programming language forbeginners(java programming tu...
An introduction to java programming language forbeginners(java programming tu...An introduction to java programming language forbeginners(java programming tu...
An introduction to java programming language forbeginners(java programming tu...
 
Computer Graphics display technologies(Computer graphics tutorials and tips)
Computer Graphics display technologies(Computer graphics tutorials and tips)Computer Graphics display technologies(Computer graphics tutorials and tips)
Computer Graphics display technologies(Computer graphics tutorials and tips)
 
lecture2 computer graphics graphics hardware(Computer graphics tutorials)
 lecture2  computer graphics graphics hardware(Computer graphics tutorials) lecture2  computer graphics graphics hardware(Computer graphics tutorials)
lecture2 computer graphics graphics hardware(Computer graphics tutorials)
 
bresenham circles and polygons in computer graphics(Computer graphics tutorials)
bresenham circles and polygons in computer graphics(Computer graphics tutorials)bresenham circles and polygons in computer graphics(Computer graphics tutorials)
bresenham circles and polygons in computer graphics(Computer graphics tutorials)
 
lecture1 introduction to computer graphics(Computer graphics tutorials)
lecture1 introduction to computer graphics(Computer graphics tutorials)lecture1 introduction to computer graphics(Computer graphics tutorials)
lecture1 introduction to computer graphics(Computer graphics tutorials)
 
computer graphics
computer graphicscomputer graphics
computer graphics
 
Introduction to Computer graphics
Introduction to Computer graphics Introduction to Computer graphics
Introduction to Computer graphics
 

Similar to lecture8 clipping

Similar to lecture8 clipping (20)

Clipping ( Cohen-Sutherland Algorithm )
Clipping ( Cohen-Sutherland Algorithm )Clipping ( Cohen-Sutherland Algorithm )
Clipping ( Cohen-Sutherland Algorithm )
 
Lecture 2d point,curve,text,line clipping
Lecture   2d point,curve,text,line clippingLecture   2d point,curve,text,line clipping
Lecture 2d point,curve,text,line clipping
 
99995327.ppt
99995327.ppt99995327.ppt
99995327.ppt
 
Lect 5 2d clipping
Lect 5 2d clippingLect 5 2d clipping
Lect 5 2d clipping
 
Clipping in Computer Graphics
Clipping in Computer Graphics Clipping in Computer Graphics
Clipping in Computer Graphics
 
ibuib.pptx
ibuib.pptxibuib.pptx
ibuib.pptx
 
Clipping
ClippingClipping
Clipping
 
ohu.pptx
ohu.pptxohu.pptx
ohu.pptx
 
Clipping 22
Clipping 22Clipping 22
Clipping 22
 
Clipping 22
Clipping 22Clipping 22
Clipping 22
 
kgv.pptx
kgv.pptxkgv.pptx
kgv.pptx
 
Clipping
ClippingClipping
Clipping
 
Cohen sutherland line clipping
Cohen sutherland line clippingCohen sutherland line clipping
Cohen sutherland line clipping
 
Clipping computer graphics
Clipping  computer graphicsClipping  computer graphics
Clipping computer graphics
 
line clipping
line clipping line clipping
line clipping
 
yutd65.pptx
yutd65.pptxyutd65.pptx
yutd65.pptx
 
UNIT2.pptx
UNIT2.pptxUNIT2.pptx
UNIT2.pptx
 
7-Clipping-16 (1).pdf
7-Clipping-16 (1).pdf7-Clipping-16 (1).pdf
7-Clipping-16 (1).pdf
 
Clipping
ClippingClipping
Clipping
 
Clipping
ClippingClipping
Clipping
 

More from Daroko blog(www.professionalbloggertricks.com)

More from Daroko blog(www.professionalbloggertricks.com) (14)

Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...
Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...
Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...
 
Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...
Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...
Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...
 
An Introduction to Project management(project management tutorials)
An Introduction to Project management(project management tutorials)An Introduction to Project management(project management tutorials)
An Introduction to Project management(project management tutorials)
 
The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)
 
Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)
 
advanced java programming(java programming tutorials)
 advanced java programming(java programming tutorials) advanced java programming(java programming tutorials)
advanced java programming(java programming tutorials)
 
java swing tutorial for beginners(java programming tutorials)
java swing tutorial for beginners(java programming tutorials)java swing tutorial for beginners(java programming tutorials)
java swing tutorial for beginners(java programming tutorials)
 
Displays and color system in computer graphics(Computer graphics tutorials)
Displays and color system in computer graphics(Computer graphics tutorials)Displays and color system in computer graphics(Computer graphics tutorials)
Displays and color system in computer graphics(Computer graphics tutorials)
 
Data structures graphics library in computer graphics.
Data structures  graphics library in computer graphics.Data structures  graphics library in computer graphics.
Data structures graphics library in computer graphics.
 
2d/3D transformations in computer graphics(Computer graphics Tutorials)
2d/3D transformations in computer graphics(Computer graphics Tutorials)2d/3D transformations in computer graphics(Computer graphics Tutorials)
2d/3D transformations in computer graphics(Computer graphics Tutorials)
 
Computer graphics defination(An introuction to computer garphics)
Computer graphics defination(An introuction to computer garphics)Computer graphics defination(An introuction to computer garphics)
Computer graphics defination(An introuction to computer garphics)
 
Interaction design process in human Computer Interface(Human computer interac...
Interaction design process in human Computer Interface(Human computer interac...Interaction design process in human Computer Interface(Human computer interac...
Interaction design process in human Computer Interface(Human computer interac...
 
Interaction devices in human Computer Interface(Human Computer interface tut...
 Interaction devices in human Computer Interface(Human Computer interface tut... Interaction devices in human Computer Interface(Human Computer interface tut...
Interaction devices in human Computer Interface(Human Computer interface tut...
 
interaction norman model in Human Computer Interaction(HCI)
interaction  norman model in Human Computer Interaction(HCI)interaction  norman model in Human Computer Interaction(HCI)
interaction norman model in Human Computer Interaction(HCI)
 

lecture8 clipping

  • 1. CSC 406: Applied Computer Graphics LECTURE 8: Clipping
  • 2. Lecture 8:  Scope:  Clipping  Clipping algorithms
  • 3. Clipping  We have talked about 2D scan conversion of line-segments and polygons  What if endpoints of line segments or vertices of polygons lie outside the visible device region?  Need clipping!  Clipping .pdf notes
  • 4. Clipping  Clipping: Remove points outside a region of interest – clip window.  Want to discard everything that’s outside of our window...  Primitives: point, line-segment, and polygon.  Point clipping: Remove points outside window.  A point is either entirely inside the region or not.  Line-segment clipping: Remove portion of line segment outside window.  Line segments can straddle the region boundary.  Polygon clipping: Remove portion of polygon outside window
  • 5. Review: Parametric representation of line or equivalently  A and B are non-coincident endpoints.  For , L(t) defines an infinite line.  For , L(t) defines a line segment from A to B.  Good for generating points on a line.  Not so good for testing if a given point is on a line. A B
  • 6. Review: Implicit line Equation representation of line: •P is a point on the line. • is a vector perpendicular to the line. • gives us the signed distance from any point Q to the line. •The sign of tells us if Q is on the left or right of the line, relative to the direction of . •If is zero, then Q is on the line. •Use same form for the implicit representation of a half space.
  • 7. Clipping a point to a half space  Represent window edges implicitly.  Use the implicit form of a line to classify a point Q.  Must choose a convention for the normal: points to the inside. Check the sign of :  If > 0, then Q is inside.  Otherwise clip (discard) Q: It is on the edge or outside.
  • 8. Clipping a line segment to a half space  There are three cases:  The line segment is entirely inside: Keep it.  The line segment is entirely outside: Discard it.  The line segment is partially inside and partially outside: Generate new line to represent part inside.
  • 9. Input Specification •Window edge: implicit, •Line segment: parametric, L(t) = A + t(B-A).
  • 10. Do the easy stuff first  Do the easy stuff first:  We can devise easy (and fast!) tests for the first two cases:  AND Outside  AND Inside  Need to decide whether “on the boundary” is inside or outside?  Trivial tests are important in computer graphics:  Particularly if the trivial case is the most common one.  Particularly if we can reuse the computation for the non- trivial case
  • 11. Do the hard stuff only if we have to If line segment partially inside and partially outside, need to clip it: •Line segment from A to B in parametric form: •When t=0, L(t)=A. When t=1, L(t)=B. •We now have the following:
  • 12. Find the Intersection •We want t such that : •Solving for t gives us •NOTE: The values we use for our simple test can be reused to compute t:
  • 13. Clipping a line segment to a window  Just clip to each of four half spaces in turn. given: line segment (A, B), clip in-place: for each edge (P,n) wecA = (A-P) . n wecB = (B-P) . n if ( wecA < 0 AND wecB < 0 ) then reject if ( wecA > 0 AND wecB > 0 ) then next t = wecA / (wecA - wecB) if ( wecA < 0 ) then A = A + t*(B-A) else B = A + t*(B-A) endif endfor  Note:  This Algorithm can clip lines to any convex window.  Optimizations can be made for the special case of horizontal and vertical window edges
  • 14. Optimization (Cohen- Sutherland)  Q: Solving for the intersection point is expensive. Can we avoid evaluation of a clip on one edge if we know the line segment is out on another?  A: Yes.  Do all trivial tests first.  Combine results using Boolean operations to determine  Trivial accept on window: all edges have trivial accept  Trivial reject on window: any edge has trivial reject  Do full clip only if no trivial accept/reject on window
  • 15. Cohen-Sutherland Algorithm 0 1 2 3 0000 00010101 1001 0100 1000 00100110 1010 (x1, y1) (x2, y2) (x2, y1) (x1, y2) Half space code (x < x2) | (x > x1) | (y > y1) | (y < y2) Outcodes
  • 16. Cohen-Sutherland Algorithm  Computing the outcodes for a point is trivial  Just use comparison  Trivial rejection is performed using the logical and of the two endpoints  A line segment is rejected if the result of and operation > 0. Why?
  • 17. Using Outcodes  Consider the 5 cases below  AB: outcode(A) = outcode(B) = 0  Accept line segment
  • 18. Using Outcodes  CD: outcode (C) = 0, outcode(D) ≠ 0  Compute intersection  Location of 1 in outcode(D) determines which edge to intersect with  Note if there were a segment from A to a point in a region with 2 ones in outcode, we might have to do two interesections
  • 19. Using Outcodes  EF: outcode(E) logically ANDed with outcode(F) (bitwise) ≠ 0  Both outcodes have a 1 bit in the same place  Line segment is outside of corresponding side of clipping window  reject
  • 20. Using Outcodes  GH and IJ: same outcodes, neither zero but logical AND yields zero  Shorten line segment by intersecting with one of sides of window  Compute outcode of intersection (new endpoint of shortened line segment)  Re-execute algorithm
  • 21. Cohen-Sutherland Algorithm  Now we can efficiently reject lines completely to the left, right, top, or bottom of the rectangle.  If the line cannot be trivially rejected (what cases?), the line is split in half at a clip line.  Not that about one half of the line can be rejected trivially  This method is efficient for large or small windows.
  • 22. Cohen-Sutherland Algorithm clip (int Ax, int Ay, int Bx, int By) { int cA = code(Ax, Ay); int cB = code(Bx, By); while (cA | cB) { // not completely inside the window if(cA & cB) return; // rejected if(cA) { update Ax, Ay to the clip line depending on which outer region the point is in cA = code(Ax, Ay); } else { update Bx, By to the clip line depending on which outer region the point is in cB = code(Bx, By); } } drawLine(Ax, Ay, Bx, By); }
  • 23. 3D Line-clip Algorithm • Half-space now lies on one side of a plane. • Implicit formula for plane in 3D is same as that for line in 2D. • Parametric formula for line to be clipped is unchanged. – Use 6-bit outcodes – When needed, clip line segment against planes
  • 24. Polygon Clipping  Not as simple as line segment clipping  Clipping a line segment yields at most one line segment  Clipping a polygon can yield multiple polygons  However, clipping a convex polygon can yield at most one other polygon
  • 25. Tessellation and Convexity  One strategy is to replace nonconvex (concave) polygons with a set of triangular polygons (a tessellation)  Also makes fill easier  Tessellation code in GLU library
  • 26. Pipeline Clipping of Polygons  Three dimensions: add front and back clippers
  • 27.  Polygon Clipping (Sutherland-Hodgman):  Window must be a convex polygon  Polygon to be clipped can be convex or not  Approach:  Polygon to be clipped is given as a sequence of vertices  Each polygon edge is a pair  Don't forget wrap around; is also an edge  Process all polygon edges in succession against a window edge  Polygon in - polygon out  Repeat on resulting polygon with next sequential window edge  Sutherland-Hodgman algorithm deals with concave polygons efficiently  Built upon efficient line segment clipping Sutherland-Hodgman Polygon Clipping Algorithm
  • 28. Four Cases  There are four cases to consider  Some notations:  s = vj is the polygon edge starting vertex  p = vj+1 is the polygon edge ending vertex  i is a polygon-edge/window-edge intersection point  wj is the next polygon vertex to be output
  • 29. Case 1 Polygon edge is entirely inside the window edge •p is next vertex of resulting polygon
  • 30. Case 2 Polygon edge crosses window edge going out • Intersection point i is next vertex of resulting polygon •
  • 31. Case 3 Polygon edge is entirely outside the window edge •No output
  • 32. Case 4 Polygon edge crosses window edge going in • Intersection point i and p are next two vertices of resulting polygon •
  • 34. An Example with a non-convex polygon
  • 35. Summary of Clipping  Point Clipping  Simple: in or out  Line Clipping:  Based on point clipping  Compute the line segment inside the window  Need to compute intersections between the line segment and window edges.  Consider trivial cases first to reduce computation.  outcodes algorithm  Polygon Clipping:  More complex. A convex polygon may be clipped into multiple polygons.  Based on line clipping  Clip the polygon against window edges in a sequence