Title: CS 455 Computer Graphics
1CS 455 Computer Graphics
2Line Clipping
- What happens when one or both endpoints of a
line segment are not inside the specified drawing
area? - Draw just the portions of a line (or object)
that fall within a given region/window/screen
(usually rectangular)
Drawing Area
3Line Clipping
- Strategies for clipping
- Check (in inner loop) if each point is inside ?
Works, but slow - Clip invalid coordinate(s) to boundary ?
Incorrect results - Find intersection of line with boundary ? Correct
if (x gt xmin x lt xmax y gt ymin y
lt ymax) drawPoint(x,y,c)
if (x lt xmin) x xmin else if (x gt xmax) x
xmax if (y lt ymin) y ymin else if (y gt
ymax) y ymax
Clip x
Output
Input
Clip y
Clip line to intersection
4Line Clipping Possible Configurations
- Both endpoints are inside the region (line AB)
- No clipping necessary
- One endpoint in, one
- out (line CD)
- Clip at intersection point
- Both endpoints outside
- the region
- No intersection (lines EF, GH)
- Line intersects the region (line IJ)
- Clip line at both intersection points
J
D
I
A
F
C
H
B
E
G
5Line Clipping Cohen-Sutherland
- Basic algorithm
- Accept (and draw) lines that have both endpoints
inside the region
- Reject (and dont draw) lines that have both
endpoints less than xmin or ymin or greater than
xmax or ymax
- Clip the remaining lines at a region boundary and
repeat steps 1 and 2 on the clipped line segments
6Cohen-Sutherland Accept/Reject Tests
- Assign 4-bit code to each endpoint corresponding
to its position relative to region - First bit (1000) if y gt ymax
- Second bit (0100) if y lt ymin
- Third bit (0010) if x gt xmax
- Fourth bit (0001) if x lt xmin
- Test
- if code0 OR code1 0000
- accept (draw)
- else if code0 AND code1 ? 0000
- reject (dont draw)
- else clip and retest
1000
1001
1010
0001
0010
0000
0100
0101
0110
7Cohen-Sutherland Accept/Reject
- Accept/reject/redo all based on bit-wise Boolean
ops.
1000
1001
1010
0001
0010
0000
0100
0101
0110
8Cohen-Sutherland Line Clipping
- Intersection algorithm
- if code0 ? 0000 then code code0
- else code code1
- dx x1 x0 dy y1 y0
- if code AND 1000 then begin // ymax
- x x0 dx (ymax y0) / dy y ymax
- end
- else if code AND 0100 then begin // ymin
- x x0 dx (ymin y0) / dy y ymin
- end
- else if code AND 0010 then begin // xmax
- y y0 dy (xmax x0) / dx x xmax
- end
- else begin // xmin
- y y0 dy (xmin x0) / dx x xmin
- end
- if code code0 then begin x0 x y0 y end
- else begin x1 x y1 y end
(x1, y1)
(x, y)
ymax
dy
(x0, y0)
dx
ymin
xmin
xmax
9Cohen-Sutherland Line Clipping Example
- Intersection algorithm
- if code0 ? 0000 then code code0
- else code code1
- dx x1 x0 dy y1 y0
- if code AND 1000 then begin // ymax
- x x0 dx (ymax y0) / dy y ymax
- end
- else if code AND 0100 then begin // ymin
- x x0 dx (ymin y0) / dy y ymin
- end
- else if code AND 0010 then begin // xmax
- y y0 dy (xmax x0) / dx x xmax
- end
- else begin // xmin
- y y0 dy (xmin x0) / dx x xmin
- end
- if code code0 then begin x0 x y0 y end
- else begin x1 x y1 y end
Code
dx
y
x
dy
10Cohen-Sutherland Line Clipping Example
- Intersection algorithm
- if code0 ? 0000 then code code0
- else code code1
- dx x1 x0 dy y1 y0
- if code AND 1000 then begin // ymax
- x x0 dx (ymax y0) / dy y ymax
- end
- else if code AND 0100 then begin // ymin
- x x0 dx (ymin y0) / dy y ymin
- end
- else if code AND 0010 then begin // xmax
- y y0 dy (xmax x0) / dx x xmax
- end
- else begin // xmin
- y y0 dy (xmin x0) / dx x xmin
- end
- if code code0 then begin x0 x y0 y end
- else begin x1 x y1 y end
Code
dx
y
x
dy
11Cohen-Sutherland Line Clipping Example
- Intersection algorithm
- if code0 ? 0000 then code code0
- else code code1
- dx x1 x0 dy y1 y0
- if code AND 1000 then begin // ymax
- x x0 dx (ymax y0) / dy y ymax
- end
- else if code AND 0100 then begin // ymin
- x x0 dx (ymin y0) / dy y ymin
- end
- else if code AND 0010 then begin // xmax
- y y0 dy (xmax x0) / dx x xmax
- end
- else begin // xmin
- y y0 dy (xmin x0) / dx x xmin
- end
- if code code0 then begin x0 x y0 y end
- else begin x1 x y1 y end
Code 1010
dx
y
x
dy
12Cohen-Sutherland Line Clipping Example
- Intersection algorithm
- if code0 ? 0000 then code code0
- else code code1
- dx x1 x0 dy y1 y0
- if code AND 1000 then begin // ymax
- x x0 dx (ymax y0) / dy y ymax
- end
- else if code AND 0100 then begin // ymin
- x x0 dx (ymin y0) / dy y ymin
- end
- else if code AND 0010 then begin // xmax
- y y0 dy (xmax x0) / dx x xmax
- end
- else begin // xmin
- y y0 dy (xmin x0) / dx x xmin
- end
- if code code0 then begin x0 x y0 y end
- else begin x1 x y1 y end
Code 1010
dx 250
y
x
dy 150
13Cohen-Sutherland Line Clipping Example
- Intersection algorithm
- if code0 ? 0000 then code code0
- else code code1
- dx x1 x0 dy y1 y0
- if code AND 1000 then begin // ymax
- x x0 dx (ymax y0) / dy y ymax
- end
- else if code AND 0100 then begin // ymin
- x x0 dx (ymin y0) / dy y ymin
- end
- else if code AND 0010 then begin // xmax
- y y0 dy (xmax x0) / dx x xmax
- end
- else begin // xmin
- y y0 dy (xmin x0) / dx x xmin
- end
- if code code0 then begin x0 x y0 y end
- else begin x1 x y1 y end
Code 1010
dx 250
y
x
dy 150
14Cohen-Sutherland Line Clipping Example
- Intersection algorithm
- if code0 ? 0000 then code code0
- else code code1
- dx x1 x0 dy y1 y0
- if code AND 1000 then begin // ymax
- x x0 dx (ymax y0) / dy y ymax
- end
- else if code AND 0100 then begin // ymin
- x x0 dx (ymin y0) / dy y ymin
- end
- else if code AND 0010 then begin // xmax
- y y0 dy (xmax x0) / dx x xmax
- end
- else begin // xmin
- y y0 dy (xmin x0) / dx x xmin
- end
- if code code0 then begin x0 x y0 y end
- else begin x1 x y1 y end
Code 1010
dx 250
y 200
x 233
dy 150
15Cohen-Sutherland Line Clipping Example
- Intersection algorithm
- if code0 ? 0000 then code code0
- else code code1
- dx x1 x0 dy y1 y0
- if code AND 1000 then begin // ymax
- x x0 dx (ymax y0) / dy y ymax
- end
- else if code AND 0100 then begin // ymin
- x x0 dx (ymin y0) / dy y ymin
- end
- else if code AND 0010 then begin // xmax
- y y0 dy (xmax x0) / dx x xmax
- end
- else begin // xmin
- y y0 dy (xmin x0) / dx x xmin
- end
- if code code0 then begin x0 x y0 y end
- else begin x1 x y1 y end
Code 1010
dx 250
y 200
x 233
dy 150
16Cohen-Sutherland Line Clipping Example
- Intersection algorithm
- if code0 ? 0000 then code code0
- else code code1
- dx x1 x0 dy y1 y0
- if code AND 1000 then begin // ymax
- x x0 dx (ymax y0) / dy y ymax
- end
- else if code AND 0100 then begin // ymin
- x x0 dx (ymin y0) / dy y ymin
- end
- else if code AND 0010 then begin // xmax
- y y0 dy (xmax x0) / dx x xmax
- end
- else begin // xmin
- y y0 dy (xmin x0) / dx x xmin
- end
- if code code0 then begin x0 x y0 y end
- else begin x1 x y1 y end
Code 1010
dx 250
y 200
x 233
dy 150
17Cohen-Sutherland Line Clipping Summary
- Choose an endpoint outside the clipping region
- Using a consistent ordering (top to bottom, left
to right) find a clipping border the line
intersects - Discard the portion of the line from the endpoint
to the intersection point - Set the new line to have as endpoints the new
intersection point and the other original
endpoint - You may need to run this several times on a
single line (e.g., a line that crosses multiple
clip boundaries)
18Cohen-Sutherland Line Clip Examples
H
1000
1001
1010
F
D
G
0001
0000
C
0010
J
A
E
I
0101
0100
0110
B
A 0001 B 0100 OR 0101 AND
0000 subdivide
C 0000 D 0010 OR 0010 AND
0000 subdivide
E 0000 F 0000 OR 0000 AND
0000 accept
G 0000 H 1010 OR 1010 AND
0000 subdivide
I 0110 J 0010 OR 0110 AND
0010 reject
19Cohen-Sutherland Line Clip Examples
H
1000
1001
1010
G
D
G
0001
0000
C
C
0010
A
A
0101
0100
0110
B
A 0001 A 0001 remove
A 0001 B 0100 OR 0101 AND
0000 subdivide
C 0000 C 0000 OR 0000 AND 0000 accept
C 0000 D 1010 remove
G 0000 G 0000 OR 0000 AND
0000 accept
G 0000 H 1010 remove
20Cohen-Sutherland Line Clip Examples
1000
1001
1010
0001
0000
0010
A
B
0101
0100
0110
B
A 0001 B 0100 remove
B 0100 B 0100 OR 0100 AND
0100 reject
21Shadow maps
From http//en.wikipedia.org/wiki/Shadow_mapping
22Project 2
Perspective
Parallel (and orthonormal)
23Polygon Clipping
- What about polygons?
- For concave polygons, the intersection
- with the clipping region may be complex
24Polygon Clipping Algorithm
- Clip polygon to ymin and ymax
- Create empty output vertex list (listout empty)
- Process input list (listin (v0, v1, , vn)
where v0 vn) in order - For each input vertex (vi where 0 ? i ? n1)
- If vi is inside region ? Add vi to end of listout
- If the line between vi and vi1 intersects
specified boundaries ? Add intersection point(s)
to end of listout - Repeat clipping to xmin and xmax
- Post-process
- Find degenerate sections where both sides of
polygon has collapsed to region boundary - Remove those sections ? Create new polygon
25Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v0
ymin
inside region no
v9
v5
v1
v2
add p0 to output list
ymax
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
(p0)
26Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v1
ymin
inside region yes
v9
v5
v1
v2
add v1 to output list
ymax
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
(p0, v1)
(p0)
27Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v2
ymin
inside region yes
v9
v5
v1
v2
add v2, p1 to output list
ymax
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
(p0, v1, v2, p1)
(p0, v1)
28Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v3
ymin
inside region no
v9
v5
v1
v2
ymax
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
(p0, v1, v2, p1)
29Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v4
ymin
inside region no
v9
v5
v1
v2
add p2 to output list
ymax
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
(p0, v1, v2, p1, p2)
(p0, v1, v2, p1)
30Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v5
ymin
inside region yes
v9
v5
v1
v2
add v5, p3 to output list
ymax
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
(p0, v1, v2, p1, p2, v5, p3)
(p0, v1, v2, p1, p2)
31Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v6
ymin
inside region no
v9
v5
v1
v2
ymax
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
(p0, v1, v2, p1, p2, v5, p3)
32Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v7
ymin
inside region no
v9
v5
v1
v2
ymax
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
(p0, v1, v2, p1, p2, v5, p3)
33Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v8
ymin
inside region no
v9
v5
v1
v2
add p4 to output list
ymax
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
(p0, v1, v2, p1, p2, v5, p3, p4)
(p0, v1, v2, p1, p2, v5, p3)
34Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v9
ymin
inside region yes
v9
v5
v1
v2
add v9, p5 to output list
ymax
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
(p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)
(p0, v1, v2, p1, p2, v5, p3, p4)
35Polygon Clipping Example
This gives us a new polygon
ymin
v9
v5
v1
v2
ymax
with vertices
(p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)
36Polygon Clipping Example (cont.)
Now clip to xmin and xmax
xmin
xmax
p9
v9
p6
v5
p7
v1
v2
p8
Input vertex list (p0, v1, v2, p1, p2, v5,
p3, p4, v9, p5) Output vertex list (p0, p6, p7,
v2, p1, p8, p9, p3, p4, v9, p5)
37Polygon Clipping Example (cont.)
Now post-process
xmin
xmax
p3
p4
p5
p0
p9
v9
p6
p7
v2
p1
v3
p8
Output vertex list (p0, p6, p7, v2,
p1, p8, p9, p3, p4, v9, p5) Post-process (p0,
p6, p9, p3,) and (p7, v2, p1, p8) and (v4, v9, p5)