You are on page 1of 44

Computer Graphics

311321

1
CHAPTER SIX

Two-Dimensional
Clipping

2
Clipping
•For the image below consider which points should
be kept and which ones should be clipped

P4

Window P2
wymax P6
P3
P1
P7 P5

P9
P8
wymin
P10

wxmin wxmax
Point Clipping
•Easy - a point (x,y) is not clipped if:
• wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax
•otherwise it is clipped
P4 Clipped
Clipped

Window P2
wymax
Clipped
P5
P1
P7 Points Within the Window
are Not Clipped
P9 P8
wymin
Clipped P10

wxmin wxmax
Two-Dimensional Point Clipping

 For a clipping rectangle in standard position, we save a two-


dimensional point P = (x,y) for display if the following inequalities
are satisfied:

xw min  x  xw max
yw min  y  yw max (6 - 12)

 If any of these four inequalities is not satisfied, the point is clipped


(not saved for display).
 Point clipping is useful in applications when pictures are modeled
with particle systems. For example, scenes involving clouds and
smoke.

5
Point Clipping
• Exercise: P1 = (10, 20), P2 = (30, 50), P3 = (60, 90), and
P4 = (130, 150). Suppose that the coordinates of the two
opposite corners of the clip window are
(xwmin, ywmin) = (30, 30) and (xwmax, ywmax) = (130, 110).
Which of the above points will be clipped?

6
Two-Dimensional Line Clipping
Figure 6-11 illustrates possible positions for straight-line segments
in relationship to a standard clipping window.
A line-clipping algorithm processes each line in a scene through a
series of tests and intersection calculations to determine whether
the entire line or any part of it is to be saved for display.

7
Line Clipping

8
Sutherland: Line Clipping
- One of the oldest and most popular line-clipping
procedures.
- The method speeds up the processing of line
segments by performing initial tests that reduce the
number of intersections that must be calculated.
- Efficient method of accepting or rejecting lines that
don’t intersect the window edges.

9
Sutherland: Line Clipping
􀂾 Lines that cross a boundary of the window, are clipped
by computing the coordinates of the new boundary
endpoint of the line where it crosses the edge of the window.

􀂾 Assign a binary 4 bit code to each vertex by comparing


it to clip coordinates:
􀂾 Bit 1 (left): the sign bit of x – xwmin.
􀂾 Bit 2 (right): the sign bit of xwmax – x.
􀂾 Bit 3 (below): the sign bit of y - ywmin
􀂾 Bit 4 (above): the sign bit of ywmax – y.

10
Sutherland: World Division

11
Sutherland: World Division
•World space is divided into regions based
on the window boundaries
– Each region has a unique four bit region code
– Region codes indicate the position of the
regions with respect to the window

1001 1000 1010


3 2 1 0
abovebelow right left 0000
0001 0010
Window
Region Code Legend
0101 0100 0110
Sutherland: Labelling
•Every end-point is labelled with the
appropriate region code
P11 [1010]
P4 [1000]

Window
wymax P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]

wxmin wxmax
Sutherland: Lines In The Window
Lines completely contained within the window
boundaries have region code [0000] for both
end-points so are not clipped
P11 [1010]
P4 [1000]

Window
wymax P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]

wxmin wxmax
Sutherland: Lines Outside The Window
Any lines with a common set bit in the region
codes of both end-points can be clipped
– The AND operation can efficiently check this
P11 [1010]
P4 [1000]

Window
wymax P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]

wxmin wxmax
Sutherland: Other Lines
•Lines that cannot be identified as
completely inside or outside the window may
or may not cross the window interior
•These lines are processed as follows:
– Compare an end-point outside the window to a
boundary (choose any order in which to
consider boundaries e.g. left, right, bottom, top)
and determine how much can be discarded
– If the remainder of the line is entirely inside or
outside the window, retain it or clip it
respectively
Sutherland: Other Lines (cont…)

– Otherwise, compare the remainder of the line


against the other window boundaries
– Continue until the line is either discarded or a
segment inside the window is found
•We can use the region codes to determine
which window boundaries should be
considered for intersection
– To check if a line crosses a particular
boundary we compare the appropriate bits in
the region codes of its end-points
– If one of these is a 1 and the other is a 0 then
the line crosses the boundary
Sutherland Examples
•Consider the line P9 to P10 below
– Start at P10 Window
wymax
– From the region codes
of the two end-points we
know the line doesn’t P [0000]
9

cross the left or right wymin P ’ [0000]


10

boundary P [0100] 10

– Calculate the intersection


wxmin wxmax
of the line with the bottom
boundary to generate point P10’
– The line P9 to P10’ is completely inside the
window so is retained
Sutherland Examples (cont…)

•Consider the line P3 to P4 below


P4 [1000]
– Start at P4 P4’ [1001]
Window
wymax
– From the region codes P [0001]
3

of the two end-points


we know the line
crosses the left wymin
boundary so calculate
the intersection point to wxmin wxmax
generate P4’
– The line P3 to P4’ is completely outside the
window so is clipped
Sutherland Examples (cont…)

•Consider the line P7 to P8 below


– Start at P7
Window
wymax
– From the two region
codes of the two P ’ [0000]
7

end-points we know P [0001] 7


P ’ [0000]
8
P [0010]
8

the line crosses the wymin


left boundary so
calculate the
wxmin wxmax
intersection point to
generate P7’
Sutherland Examples (cont…)

•Consider the line P7’ to P8


– Start at P8
Window
wymax
– Calculate the
intersection with the P ’ [0000]
7

right boundary to P [0001]7


P ’ [0000]
8
P [0010]
8

generate P8’ wymin


– P7’ to P8’ is inside
the window so is wxmin wxmax
retained
Sutherland: Line Clipping
• Compute the end point Outcodes of A and B;
• if (A == 0000 && B == 0000)
– the whole line is visible (accept it);
• Else if (A AND B != 0000)
– the whole line is outside of window (reject it);
• else repeat the following {
– compute intersection;
– clip outside part;
– test again;
}

22
Sutherland Example

23
Sutherland Example
• Bit 1: x – xwmin= 72–68 = 4 􀃎 0 Bit 1: x – xwmin= 90-68=22􀃎 0
• Bit 2: xwmax - x = 92-72 = 0 􀃎 0 Bit 2: xwmax - x = 92-90=2 􀃎 0
• Bit 3: y – ywmin= 65–59 = 6 􀃎 0 Bit 3: y – ywmin= 68 –59=6􀃎 0
• Bit 4: ywmax - y = 71-65 = 6 􀃎 0 Bit 4: ywmax - y = 71-68= 3􀃎 0
• the region code of (72,65) is 0000
• the region code of (90,68) is 0000
• The line (72-65)- (90,68) is completely inside

24
Sutherland Example

25
Sutherland Example

26
Calculating Line Intersections
•Intersection points with the window
boundaries are calculated using the line-
equation parameters
– Consider a line with the end-points (x1, y1) and
(x2, y2)
– The y-coordinate of an intersection with a
vertical window boundary can be calculated
using:
y = y1 + m (xboundary - x1)
where xboundary can be set to either wxmin or
wxmax
Calculating Line Intersections (cont…)

– The x-coordinate of an intersection with a


horizontal window boundary can be calculated
using:
x = x1 + (yboundary - y1) / m
where yboundary can be set to either wymin or
wymax
– m is the slope of the line in question and can
be calculated as m = (y2 - y1) / (x2 - x1)
Example:
• Line endpoints P1 = (56, 10) & P2 = (62, 16).
Suppose that the coordinates of the two
opposite corners of the clip window are
(xwmin, ywmin) = (60,10) and
(xwmax, ywmax) = (70,16).
Check the for clipping.

29
 Figure 6-14 illustrates a line from P1 to P2.
 The region codes for P1 is 0100 and for P2 is 1001.
 Left Boundary: P1 is inside the left boundary and P2
outside the left boundary. Thus, the line crosses the left
boundary. We calculate the intersection position P´2 and
we clip off the line section P´2 to P2.

 Right Boundary: both


positions are 0 and there is
no boundary crossing by the
line.

30
 Bottom Boundary: P1 is outside the boundary and P2
inside the boundary. We find the intersection at P´1 and we
eliminate the line P1 to P´1 and proceed to the next edge.
 Top Boundary: P´1 is below the boundary and P´2 is above
the boundary. Thus we find the intersection P´´2 and
eliminate the section P´2P´´2.

 The line segment saved is


from P´1 to P´´2.
 For the line P3P4, after
first clipping, the line is
eliminated.

31
POLYGON FILL-AREA CLIPPING

 Graphics packages typically support only fill-areas that are


polygons, and often convex polygons.

 To clip a polygon fill-area, we cannot directly apply a line


clipping method to individual polygon edges because this
approach in general would not produce, in general, a closed
polyline.

32
 A line clipper would produce a disjoint set of line with no
information about how to form the closed boundaries.

33
 To clip the polygon, we need to keep the fill area as an
entity as it is processed through the clipping stages as
shown in the following Figure:

34
Polygon Clipping
• Find the vertices of the
new polygon(s) inside the
window.
• Sutherland-Hodgman
Polygon Clipping:
Check each edge of the polygon
against all window boundaries.
Modify the vertices based on
transitions. Transfer the new edges
to the next clipping boundary.
 One way to clip a convex polygon is to create a new vertex
list at each clipping boundary, and then pass this new
vertex list to the next boundary clipper.

{1, 2, 3}  { 1 , 2 , 2 , 3  , 3 , 1 }
'' '' ''

 For concave-polygon clipping, we would need to identify


the approach to generate multiple vertex lists. 36
Sutherland-Hodgman Polygon Clipping
 This is an efficient method for clipping a convex polygon. The
algorithm generates a single output vertex list. Thus it cannot clip a
concave polygon.

 General Strategy:

The endpoints of each polygon edge is sent through a series of


clippers (left, right, bottom, and top)

As soon as a clipper completes the processing of one pair of


vertices, the clipped coordinate values if any are sent to the next
clipper.

Then the first clipper continues with the next pair of endpoints.
37
 There are four possible cases that need to be considered when
processing a polygon edge against one of the clipping boundaries.

 As each successive pair of endpoints is passed to one of the clippers,


an output is generated for the next clipper according to the results of
one of the following four possible tests:

38
Shutherland-Hodgman Polygon Clipping

V1
• Traverse edges for borders; 4 cases:
V1'
– V1 outside, V2 inside: take V1' and V2
V2

– V1 inside, V2 inside: take V1 and V2 V1

V2
– V1 inside, V2 outside: take V1 and V2'
V1

V2
– V1 outside, V2 outside: take none V2'

V1

V2
Shutherland-Hodgman Polygon Clipping

• Left border:
v1 v1 v2 both inside v1 v2
v2 v2 v3 both inside v2 v3
..... “ “ ........
v1,v2,v3,v4v5,v6,v1
v6 • Bottom Border:
v1 v2 both inside v1 v2
v2 v3 v2 i, v3 o v2 v3'
v5' v3 v4 both outside none
v3' v4 v5 both outside none
v5 v6 v5 o, v6 i v5' v6
v5 v3 v6 v1 both inside v6 v1
v4 v1,v2,v3',v5',v6,v1
Shutherland-Hodgman Polygon Clipping
• v1,v2,v3',v5',v6,v1
• Right border:
v1 v2 v1 i, v2 o v1 v2'
v2 v3‘ v2 o, v3'i v2'' v3'
v1
v2' v3' v5‘ both inside v3' v5'
v2
v1' v5' v6 both inside v5' v6
v2''' v6 v1 both inside v6 v1
v1,v2',v2'',v3',v5',v6,v1
v6
• Top Border:
v2'' v1 v2' both outside none
v2' v2'' v2' o, v2'' i v2''' v2''
v5' v2'' v3' both inside v2'' v3'
v3' v3' v5' both inside v3' v5'
v5' v6 both inside v5' v6
v5 v3 v6 v1 v6 i, v1 o v6 v1'
v2''',v2'',v3',v5',v6,v1'
v4
Shutherland-Hodgman
Polygon Clipping
v1
v2'
v2
v1' v1' v2'''
v2'''
v6 v6
v2'' v2''

v5' v5'
v3' v3'

v5 v3

v4
43
44

You might also like