0% found this document useful (0 votes)
56 views

Line Clipping Against Rectangles

The document describes line clipping and polygon clipping techniques. 1. Line clipping involves using the Cohen-Sutherland algorithm to clip lines against rectangles by assigning "outcodes" to line endpoints and iteratively clipping segments until they are fully inside or outside the clip region. 2. Polygon clipping uses the Sutherland-Hodgeman algorithm to clip polygons against each window edge one at a time by considering each polygon edge segment and clipping vertices as needed. 3. The number of polygon vertices may increase during clipping as intersections with window edges are found.

Uploaded by

Nithin Sudhakar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Line Clipping Against Rectangles

The document describes line clipping and polygon clipping techniques. 1. Line clipping involves using the Cohen-Sutherland algorithm to clip lines against rectangles by assigning "outcodes" to line endpoints and iteratively clipping segments until they are fully inside or outside the clip region. 2. Polygon clipping uses the Sutherland-Hodgeman algorithm to clip polygons against each window edge one at a time by considering each polygon edge segment and clipping vertices as needed. 3. The number of polygon vertices may increase during clipping as intersections with window edges are found.

Uploaded by

Nithin Sudhakar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Line Clipping

• Line clipping against rectangles


(x1, y1)

y max
(x0, y0)

y min
xmin xmax

The problem: Given a set of 2D lines or polygons and a window, clip


the lines or polygons to their regions that are inside the window.
Motivations

• Efficiency
• Display in portion of a screen
• Occlusions

Clip rectangle
Clipping is tricky!

In: 3 vertices
Clip
Out: 6 vertices

Clip In: 1 polygon


Out: 2 polygons
Simple Cases

1. If x0 < xmin and x1 < xmin

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

• Region and outcodes

First bit: above top edge y > ymax


Second bit: below bottom edge y < ymin
Third bit: to right of right edge x > xmax
Fourth bit: to left of left edge x < xmin
The C-S Line-Clipping Algorithm (cont.)

• Checking for trivial acceptance or rejection using outcodes


1). Each endpoint of a line segment is assigned an outcode;
2). If both 4-bit codes are zero, the line can be trivially accepted;
3). A logical and is performed on both outcodes;
4). If the result is nonzero, the line can be trivially rejected.
Steps for Cohen-Sutherland Algorithm

1. End-points pairs are checked for trivial acceptance or rejection using


outcode;
2. If not trivially accepted or rejected, divide the line segment into two at
a clip edge;
3. Iteratively clipped by test trivial-acceptance or trivial-rejection, and
divided into two segments until completely inside or trivial-rejection.
D
C 1000 1010
1001 B I
A H

0001 0000 G 0010

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

Bottom Clip Boundary

Top Clip Boundary Left Clip


Boundary
Sutherland-Hodgeman Algorithm

Note the difference between this strategy and the Cohen-Sutherland


algorithm for clipping a line: the polygon clipper clips against each
window edge in succession, whereas the line clipper is a recursive
algorithm.

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

Clipping edge Clipping edge Clipping edge


Solution (cont.):
Now v1v2:

v5

v4
v1 v1 v1

v2 v3 v2 v2

Clipping edge Clipping edge Clipping edge


Solution (cont.):
Now v2v3:

v5

v4
v1
v1

v2 v3 v2 v3 v2 v3

Clipping edge Clipping edge Clipping edge


Solution (cont.):
Now v3v4:

v5

v4 v4
v1
v1
i1

v2 v3 v3 v2 v3

Clipping edge Clipping edge Clipping edge


Solution (cont.):
Now v4v5:

v5 v5 v5

v4 v4 i2
v1
v1
i1

v2 v3 v2 v3

Clipping edge Clipping edge Clipping edge

After these, we have to clip the polygon against the other three edges of the
window in a similar way.

You might also like