## Clipping

Since we have a separation between the models and the image created from those models, there can be parts of the model that do not appear in the current view when they are rendered.pixels outside the clip rectangle are clipped, and are not displayed.

can clip analytically - knowing where the clip rectangle is clipping can be done before scan-line converting a graphics primitive (point, line, polygon) by altering the graphics primitive so the new version lies entirely within the clip rectangle

can clip by brute force (scissoring) - scan convert the entire primitive but only display those pixels within the clip rectangle by checking each pixel to see if it is visible.

- clipping a point against a rectangle -> nothing or single point
- clipping a line against a rectangle -> nothing or single line segment
- clipping a rectangle against a rectangle -> nothing or
single rectangle
- ( Assuming the rectangle is aligned. Otherwise treat as convex polygon. )

- clipping a convex polygon against a rectangle -> nothing or single single convex polygon
- clipping a concave polygon against a rectangle -> nothing or 1 or more concave polygons

### Point Clipping

point (X,Y)clipping rectangle with corners (Xmin,Ymin) (Xmax,Ymax)

point is within the clip rectangle if:

- Xmin <= X<= Xmax

Ymin <= Y<= Ymax

### Cohen-Sutherland Line Clipping ( Foley 3.12.3 )

given a line segment, repeatedly:- check for trivial acceptance
- both endpoints within clip rectangle

- check for trivial rejection
- both endpoints outside clip rectangle IS NOT ENOUGH

both endpoints off the same side of clip rectangle IS ENOUGH - divide segment in two where one part can be trivially rejected

each region is defined by a unique 4-bit string

- left bit = 1: above top edge (Y > Ymax)
- left bit = sign bit of (Ymax - Y)

- 2nd bit = 1: below bottom edge (Y < Ymin)
- 2nd bit = sign bit of (Y - Ymin)

- 3rd bit = 1: right of right edge (X > Xmax)
- 3rd bit = sign bit of (Xmax - X)

- right bit = 1: left of left edge (X < Xmin)
- right bit = sign bit of (X - Xmin)

The frame buffer itself, in the center, has code 0000.

1001 | 1000 | 1010 -----+------+----- 0001 | 0000 | 0010 -----+------+----- 0101 | 0100 | 0110For each line segment:

- each end point is given the 4-bit code of its region
- repeat until acceptance or rejection
- if both codes are 0000 -> trivial acceptance
- if bitwise logical AND of codes is not 0000 -> trivial rejection
- divide line into 2 segments using edge of clip rectangle
- find an endpoint with code not equal to 0000
- move left to right across the code to find a 1 bit -> the crossed edge
- break the line segment into 2 line segments at the crossed edge
- forget about the new line segment lying completely outside the clip rectangle

The full algorithm is given (in C) in the white book as figure 3.41 on p.116.

### Sutherland-Hodgman Polygon Clipping ( Foley 3.14.1 )

Unlike line-clipping where we selectively clipped against each edge, here we sucessively clip a polygon against all four edges of the clip rectanglegiven a polygon with vertices V1, V2, ... Vn

and edges between vertices Vi and Vi+1, and from Vn to V1

for each of the four clipping edges

- repeatedly for each vertex V = Vn,
V1, V2, ... Vn
- if s and p are both inside the clip rectangle -> output p
- if s is inside and p is outside the clip rectangle -> output i (the intersection of edge sp with the clip edge)
- if s and p are both outside the clip rectangle -> output nothing
- if s is outside and p is inside the clip rectangle -> output i (the intersection of edge sp with the clip edge) and then p

- given an edge from vertex s to vertex p

assume s has already been dealt with

The full algorithm ( was? ) given (in C) in the red book ( ??? Ediiton ) as program 3.9 on p.114.

The full algorithm is given (in Pascal) in the white book as program 3.49 on p.128.

### Example of Polygon Clipping

Clipping on Ymax edge:

- 9-1 : both inside -> add 1
- 1-2 : both inside -> add 2
- 2-3 : keep 2-3' and clip 3'-3 -> add 3'
- 3-4 : both outside -> add nothing
- 4-5 : keep 5'-5 and clip 4-5' -> add 5' and 5
- 5-6 : both inside -> add 6
- 6-7 : keep 6-7' and clip 7'-7 -> add 7'
- 7-8 : both outside -> add nothing
- 8-9 : keep 9'-9 and clip 8-9' -> add 9' and 9
- 9-1 : both inside -> add 1
- returning 1,2,3',5',5,6,7',9',9 as the new vertex sequence