2DClipping 1x2
2DClipping 1x2
Part IV
2D Clipping
Polygon clipping
The Sutherland-Hodgman algorithm
The Weiler-Atherton algorithm
Outline
Polygon clipping
The Sutherland-Hodgman algorithm
The Weiler-Atherton algorithm
Clipping
Introduction to Computer Graphics
Line segment clipping
Clipping (contd)
{left} {right}
0001 0000 0010
A=
B
B=
AB =
accept
A
Introduction to Computer Graphics
Line segment clipping
The Cohen-Sutherland algorithm
F E = {left}
F = {top}
E F 6=
E F =
clip
E
C =
D
D = {top}
C D 6=
C D =
clip
C
Introduction to Computer Graphics
Line segment clipping
The Cohen-Sutherland algorithm
I = {bottom}
J = {right}
I J 6=
I J =
J clip
K = {bottom, right}
L = {right}
L
K L 6=
K L 6=
reject
K
Introduction to Computer Graphics
Line segment clipping
The Cohen-Sutherland algorithm
The rules for accepting (or rejecting) a line segment into the
clipping algorithm are:
1. If P1 P2 = then accept. The line segment is completely
inside the clipping region.
2. If P1 P2 6= then reject. The line segment is completely
outside the clipping region.
3. If P1 P2 = then clip. The line segment should be clipped.
P2
P1 = {bottom, left}
P2
P2 P2 = {top, right}
P1 P2 = {bottom, top, left, right}
P1 P2 = {}
do clipping
P1
P1 P2 .y P1 .y
m :=
P2 .x P1 .x
P1
Introduction to Computer Graphics
Line segment clipping
The Cohen-Sutherland algorithm
P2
P2 Is {left} subset of P1 ?
P2
Yes
P10 .y =
(winLeft P1 .x) m + P1 .y
P1
P1 P10 .x = winLeft
P1
P2
P2 Is {bottom} subset of P1 ?
P2
Yes
P100 .x :=
(winBottom P10 .y )/m + P10 .x
P1
P1 P100 .y = winBottom
P1
Introduction to Computer Graphics
Line segment clipping
The Cohen-Sutherland algorithm
Similarly
P2 P2
P2 P2
P2 P2
P1 P1
P1 P1
P1 P1
P2
P2
P2
P1
P1
P1
Introduction to Computer Graphics
Line segment clipping
The Cohen-Sutherland algorithm
x = x0 + (x1 x0 ) = x0 + x
y = y0 + (y1 y0 ) = y0 + y
with 0 < 1.
Introduction to Computer Graphics
Line segment clipping
The Liang-Barsky algorithm
xmin x0 + x xmax
ymin y0 + y ymax
pk qk k = 1, 2, 3, 4
where
p1 = x q1 = x0 xmin
p2 = x q2 = xmax x0
p3 = y q3 = y0 ymin
p4 = y q4 = ymax y0
P2 = (280; 160)
150
70 230
60
P1 = (30; 20)
P2 = (280, 160)
u3 = 0.8 u4 = 0.928
u2 = 0.28
u1 = 0.16
P1 = (30, 20)
Introduction to Computer Graphics
Line segment clipping
The Liang-Barsky algorithm
Polygon clipping
I Most of the graphics package support only fill area that are
polygons
I Sometimes only convex polygons are accepted
I A standard line clipping (e.g. Cohen-Sutherland) will produce
a set of disjoint lines
I One need an algorithm which returns a closed polygon or a
set of closed polygons.
Outline
Polygon clipping
The Sutherland-Hodgman algorithm
The Weiler-Atherton algorithm
Polygon clipping
Polygon clipping
Polygon clipping
Introduction to Computer Graphics
Polygon clipping
Polygon clipping
P1 P1 P1 P1
P3 P3
P3 P3
P3 P3
P2 P2 P2 P2
P2 P2
V3
V2 V2
V1
V1
V3
V3
V4 V4
V1
2
2
3
2
1
3
1
{1,2} {2}
{2,3} {2} {2,2}{2}
{3,1}{3,1} {2,3} {3} {2,3} {2}
{3,1} {1} {3,1} {}
{1,2} {2} {1,2} {1,2} {2,1} {1}
{2,2} {2} {1,2}{2}
{2,2} {2}
{2,2} {2}
Introduction to Computer Graphics
Polygon clipping
The Sutherland-Hodgman algorithm
Outline
Polygon clipping
The Sutherland-Hodgman algorithm
The Weiler-Atherton algorithm