0% found this document useful (0 votes)
45 views34 pages

2DClipping 1x2

The document discusses different algorithms for clipping lines and polygons to a viewing region, including the Cohen-Sutherland and Liang-Barsky line clipping algorithms as well as polygon clipping algorithms like Sutherland-Hodgman. It provides details on how each algorithm works through examples and pseudocode.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views34 pages

2DClipping 1x2

The document discusses different algorithms for clipping lines and polygons to a viewing region, including the Cohen-Sutherland and Liang-Barsky line clipping algorithms as well as polygon clipping algorithms like Sutherland-Hodgman. It provides details on how each algorithm works through examples and pseudocode.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Introduction to Computer Graphics

Part IV

2D Clipping

Introduction to Computer Graphics

Line segment clipping


The Cohen-Sutherland algorithm
The Liang-Barsky algorithm

Polygon clipping
The Sutherland-Hodgman algorithm
The Weiler-Atherton algorithm

Boolean operations on polygons


Introduction to Computer Graphics
Line segment clipping

Outline

Line segment clipping


The Cohen-Sutherland algorithm
The Liang-Barsky algorithm

Polygon clipping
The Sutherland-Hodgman algorithm
The Weiler-Atherton algorithm

Boolean operations on polygons

Introduction to Computer Graphics


Line segment clipping

Clipping

The goal of clipping is mainly to eliminate parts of the scene which


are not visible within a given viewport (e.g. window).


Introduction to Computer Graphics
Line segment clipping

Clipping (contd)

If the boundaries of the clipping rectangle are at


(xmin , ymin ); (xmax , ymax ) the the four inequalities must be satisfied
for a point (x, y ) to be inside the clipping region:
1. xmin x
2. x xmax
3. ymin y
4. y ymax

Introduction to Computer Graphics


Line segment clipping
The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm

This algorithm performs some tests on lines to determine whether


clipping computation can be avoided. These tests say that:
1. If the line segment is totally inside the clipping region do
nothing and accept the line segment
2. If the line segment is totally outside the clipping region do
nothing and reject the line segment
3. If the line segment is partially inside the clipping region clip
the line segment
Introduction to Computer Graphics
Line segment clipping
The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm (contd)

For these tests, the space is divided into 9 regions

{top,left} {top} {top, right}


1001 1000 1010

{left} {right}
0001 0000 0010

{bottom,left} {bottom} {bottom,right}


0101 0100 0110

Introduction to Computer Graphics


Line segment clipping
The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm : example 1

A=
B
B=
AB =
accept

A
Introduction to Computer Graphics
Line segment clipping
The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm : example 2

F E = {left}
F = {top}
E F 6=
E F =
clip
E

Introduction to Computer Graphics


Line segment clipping
The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm : example 3

C =
D
D = {top}
C D 6=
C D =
clip
C
Introduction to Computer Graphics
Line segment clipping
The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm : example 4

I = {bottom}
J = {right}
I J 6=
I J =
J clip

Introduction to Computer Graphics


Line segment clipping
The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm : example 5

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 Cohen-Sutherland algorithm (contd)

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.

Introduction to Computer Graphics


Line segment clipping
The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm (contd)

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

The Cohen-Sutherland algorithm (contd)

P2

P2 Is {left} subset of P1 ?
P2
Yes
P10 .y =
(winLeft P1 .x) m + P1 .y
P1
P1 P10 .x = winLeft

P1

Introduction to Computer Graphics


Line segment clipping
The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm (contd)

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

The Cohen-Sutherland algorithm (contd)

Similarly
P2 P2

P2 P2
P2 P2

P1 P1
P1 P1

P1 P1

Introduction to Computer Graphics


Line segment clipping
The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm (contd)


And finally:

P2

P2
P2

P1
P1

P1
Introduction to Computer Graphics
Line segment clipping
The Cohen-Sutherland algorithm

Pseudo-code for the Cohen-Sutherland algorithm


Require: a rectangular axis aligned clipping window
Require: a segment given by two distinct points
Ensure: a clipped segment that is totally inside the clipping window
if trivial accept then
return the segment unmodified
end if
if trivial reject then
return an empty segment
end if
for all point segmentEnd do
if point in the BOTTOM area then
clip point against BOTTOM boundary
end if
if point in the TOP area then
clip point against TOP boundary
end if
if point in the LEFT area then
clip point against LEFT boundary
end if
if point in the RIGHT area then
clip point against RIGHT boundary
end if
end for
return segment

Introduction to Computer Graphics


Line segment clipping
The Liang-Barsky algorithm

2D Liang-Barsky Clipping algorithm

The main idea of the Liang-Barsky clipping algorithm is to do as


much testing as possible before computing line intersection.
Let s1 be line segment defined by its extremities (x0 ; y0 ) and
(x1 ; y1 ). The parametric form for this line segment is given by:

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

2D Liang-Barsky Clipping algorithm (contd)


A point is in the clip window if:

xmin x0 + x xmax
ymin y0 + y ymax

which can be expressed by the 4 inequalities :

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

Introduction to Computer Graphics


Line segment clipping
The Liang-Barsky algorithm

2D Liang-Barsky Clipping algorithm (contd)


1. A line parallel to a clipping window edge has pk = 0 for that
boundary.
2. If for the same value of k, qk < 0, the line is completely
outside and can be eliminated.
3. When pk < 0 the line proceeds outside to inside the clip
window and when pk > 0 the line proceeds inside to outside.
4. For a non-zero pk , the value k = qk /pk gives the intersection
point with the window boundaries
5. For each line segment calculate 1 and 2 . For 1 look at the
boundaries for which pk < 0 and set 1 = max(0, qk /pk ). For
2 look an boundaries for which pk > 0 and set
1 = min(1, qk /pk ). If 1 > 2 the line is outside and
therefore rejected.
Introduction to Computer Graphics
Line segment clipping
The Liang-Barsky algorithm

2D Liang-Barsky Clipping : an example

P2 = (280; 160)
150

70 230

60

P1 = (30; 20)

Introduction to Computer Graphics


Line segment clipping
The Liang-Barsky algorithm

2D Liang-Barsky Clipping : an example

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.

Introduction to Computer Graphics


Polygon clipping

Outline

Line segment clipping


The Cohen-Sutherland algorithm
The Liang-Barsky algorithm

Polygon clipping
The Sutherland-Hodgman algorithm
The Weiler-Atherton algorithm

Boolean operations on polygons


Introduction to Computer Graphics
Polygon clipping

Polygon clipping using line clipping algorithm

Introduction to Computer Graphics


Polygon clipping

Polygon clipping

The correct solution should be:


Introduction to Computer Graphics
Polygon clipping

Polygon clipping

I Basically one can process using the same approach as in a line


clipping algorithm
I First one can check if a polygon can be totally saved or totally
rejected by testing it coordinates extends (bounding box).
I The simplest bounding box is the Axis Aligned Bounding Box
(AABB) which can be computed finding the minimal x
coordinate of all vertices of the polygon, the maximal x
coordinate, the minimal y coordinate and the maximal y
coordinate.

Introduction to Computer Graphics


Polygon clipping

Polygon clipping
Introduction to Computer Graphics
Polygon clipping

Polygon clipping

If the fill area is not completely inside or completely outside, one


need to locate the polygon intersection with the clipping
boundaries.
P1 P1

P1 P1 P1 P1

P3 P3

P3 P3

P3 P3
P2 P2 P2 P2

P2 P2

Introduction to Computer Graphics


Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm


The basic idea of the algorithm is :
I Consider each edge of the viewport individually
I Clip the polygon against the edge equation
I After doing all planes, the polygon is fully clipped
Introduction to Computer Graphics
Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)

Introduction to Computer Graphics


Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)


Introduction to Computer Graphics
Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)

Introduction to Computer Graphics


Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)


Introduction to Computer Graphics
Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)

Introduction to Computer Graphics


Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)

I The Sutherland-Hodgman algorithm is an efficient algorithm


to clip convex polygon
I For the non convex polygon, the result may be wrong since
the algorithm always return a single polygon
I The general strategies is to sent the pair of endpoints for each
successive polygon edges through the series of clipper (left,
right, bottom, top).
I The different clipper may work in parrallel.
Introduction to Computer Graphics
Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)

Each clipper should consider 4 situations:


1. Both the start point and the end point of the edge as inside
the clipping region
2. The start point is inside the clipping region and the end point
is outside
3. Both vertices are outside the clipping region
4. The start point is outside the clipping region and the end
point is inside

Introduction to Computer Graphics


Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)

Each of the successive clipper generates an output to the next


clipper according to:
1. If start point is outside and end point inside
output = {intersection with the border, end point}
2. If both points are inside
output = {end point}
3. If start point is inside and end point outside
output = {intersection with the border}
4. If both points are outside
output = {}
Introduction to Computer Graphics
Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)

V3

V2 V2
V1
V1

Input : {outside, inside} Input : {inside, inside}


Output : {V10 , V2 } Output : {V3 }

Introduction to Computer Graphics


Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)

V3
V3
V4 V4

V1

Input : {inside, outside} Input : {outside, outside}


Output : {V30 } Output : {}
Introduction to Computer Graphics
Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)

2
2
3

2
1

3
1

Introduction to Computer Graphics


Polygon clipping
The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (contd)

Left clipper Right clipper Bottom clipper Top clipper

{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

The Sutherland-Hodgman algorithm (contd)

For non-convex polygon, the Sutherland-Hodgman may produce


wrong results:

Introduction to Computer Graphics


Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm

1. It is a general clipping algorithm which can clip non convex


polygon against arbitrary clipping window.
2. This algorithm is also used in 3D for hidden surface removal.
Introduction to Computer Graphics
Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


Main algorithm strategy : walk along the polygon and window
edges counterclockwise to find the clipped regions

Introduction to Computer Graphics


Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


First find all intersection points between the edges of polygon and
clipping window (clipping window may be non-rectangular).
Introduction to Computer Graphics
Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


Mark points where polygon enters clipping window (green here) as
the walk is taken along the polygon

Introduction to Computer Graphics


Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


While there is still an unprocessed entering intersection : Walk
polygon/window boundary
Introduction to Computer Graphics
Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


If intersection point is out in:
I Record clipped point
I Follow polygon boundary

Introduction to Computer Graphics


Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


If intersection point is in out:
I Record clipped point
I Follow window boundary
Introduction to Computer Graphics
Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)

Introduction to Computer Graphics


Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


Introduction to Computer Graphics
Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


Repeat until the clipped-polygon is closed

Introduction to Computer Graphics


Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


Find the next unprocessed entering point
Introduction to Computer Graphics
Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


Compute the next clipped subpolygon

Introduction to Computer Graphics


Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


Introduction to Computer Graphics
Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


Repeat until there is no more unprocessed entering point

Introduction to Computer Graphics


Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


Importance of good adjacency data structure (Here we can simply
list the oriented edges)
Introduction to Computer Graphics
Polygon clipping
The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (contd)


I What if a vertex is on the boundary?
I What happens if it is almost on the boundary?
I Problem with floating point precision
I Welcome to the real world of geometry!

Introduction to Computer Graphics


Boolean operations on polygons

Outline

Line segment clipping


The Cohen-Sutherland algorithm
The Liang-Barsky algorithm

Polygon clipping
The Sutherland-Hodgman algorithm
The Weiler-Atherton algorithm

Boolean operations on polygons


Introduction to Computer Graphics
Boolean operations on polygons

Boolean operations on polygons

I Sometimes one need to perform some boolean operation on


polygon (e.g. CAD systems).
I The meaning of these operations is the same as for the
operations on sets
I The methods and techniques to choose which part of the
polygon should be considered are the same as those used for
polygon clipping.
I These algorithms expect that the input polygons are simple
(or Jordan) which mean that the edges does not cross outside
the polygon vertices.

Introduction to Computer Graphics


Boolean operations on polygons

Boolean operations on polygons (contd)

Let P1 and P2 be the two polygons:


Introduction to Computer Graphics
Boolean operations on polygons

Boolean operations on polygons (contd)

The intersection of these two polygons is defined as:

Introduction to Computer Graphics


Boolean operations on polygons

Boolean operations on polygons (contd)

The union of these two polygons is defined as:


Introduction to Computer Graphics
Boolean operations on polygons

Boolean operations on polygons (contd)

The difference of these two polygons is defined as:

Introduction to Computer Graphics


Boolean operations on polygons

Boolean operations on polygons (contd)


The symmetrical difference (XOR) of these two polygons is defined
as:

You might also like