Line Clipping Against Rectangles
Line Clipping Against Rectangles
y max
(x0, y0)
y min
xmin xmax
• Efficiency
• Display in portion of a screen
• Occlusions
Clip rectangle
Clipping is tricky!
In: 3 vertices
Clip
Out: 6 vertices
y max
or x0 > xmax and x1 > xmax
or y0 < ymin and y1 < ymin
y min
or y0 > ymax and y1 > ymax
xmin xmax
trivial rejection.
2. If xmin ≤ x ≤ xmax
y max
and ymin ≤ y ≤ ymax
y min
trivially accepted. xmin xmax
The Cohen-Sutherland Line-Clipping Algorithm
F
0101 0100 0110
E
Polygon Clipping
• Sutherland-Hodgeman algorithm (A divide-and-conquer
strategy)
• Polygons can be clipped against each edge of the
window one at a time. Edge intersections, if any, are
easy to find since the X or Y coordinates are already
known.
• Vertices which are kept after clipping against one
window edge are saved for clipping against the
remaining edges.
• Note that the number of vertices usually changes and
will often increases.
Clipping A Polygon Step by Step
Right Clip
Boundary
Given a polygon with n vertices, v1, v2,…, vn, the algorithm clips the
polygon against a single, infinite clip edge and outputs another series
of vertices defining the clipped polygon. In the next pass, the partially
clipped polygon is then clipped against the second clip edge, and so
on. Let’s considering the polygon edge from vertex vi to vertex vi+1.
Assume that start point vi has been dealt with in the previous iteration,
four cases will appear.
Sutherland-Hodgeman Algorithm(cont.)
Inside Outside Inside Outside
vi
Polygon Polygon
been vi+1
been
clipped Clip clipped
vi i: output
Boundary
vi+1 : output
Case 1 Case 2
i: first
output
Inside Outside
vi+1 vi
vi+1 : second
Polygon vi output
been
clipped Inside Outside
Case 3 Case 4
(no output)
An Example for the Polygon Clipping
v5
v4
v1
v2 v3
Solution:
As we said, the Sutherland-Hodgeman algorithm clip the polygon against one
at a time. We start with the right edge of the clip rectangle. In order to clip the
polygon against the line, each edge of the polygon have to be considered.
Starting with the edge, represented by a pair of vertices, v5v1:
v5 v5
v4
v1
v1 v1
v2 v3
v5
v4
v1 v1 v1
v2 v3 v2 v2
v5
v4
v1
v1
v2 v3 v2 v3 v2 v3
v5
v4 v4
v1
v1
i1
v2 v3 v3 v2 v3
v5 v5 v5
v4 v4 i2
v1
v1
i1
v2 v3 v2 v3
After these, we have to clip the polygon against the other three edges of the
window in a similar way.