0% found this document useful (0 votes)
37 views55 pages

CG U2 Technical

this the technical book of computer graphics unit 2

Uploaded by

suncrest5000
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)
37 views55 pages

CG U2 Technical

this the technical book of computer graphics unit 2

Uploaded by

suncrest5000
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/ 55

UNIT - II

4 Polygons and Polygon Filling

Syllabus
Polygons : Introduction to polygon, types : convex, concave and complex. Inside test.

Polygon Filling : flood fill, seed fill, scan line fill.

Contents
4.1 Introduction to Polygon . . . . . . . . . . . . . . . . . . Dec.-06, May-11,12, · · · · · · · Marks 4
4.2 Types : Convex, Concave and Complex . . . . . Dec.-06, 09,18,
. . . . . . . . . . . . . . . . . . May-05, 10, 11, 12, · · · · · · · · Marks 4
4.3 Representation of Polygon
4.4 Inside Test . . . . . . . . . . . . . . . . . . Dec.-06, 09, 10, 12, 14, 15,18
. . . . . . . . . . . . . . . . . . May-05, 07, 10, 13, 16,19 · · · Marks 6
4.5 Polygon Filling Algorithms . . . . . . . . . . . . . . . . May-05, 06, 07, 08, 09, 10, 11, 12, 13,
. . . . . . . . . . . . . . . . . . 14, 15,18,19
. . . . . . . . . . . . . . . . . . Dec.-05, 06, 07, 08, 09, 10, 11,
. . . . . . . . . . . . . . . . . . 12, 15,19 · · · · · · · · · · · · · · · Marks 16

TM

TECHNICAL PUBLICATIONS(4- -An1)up thrust for knowledge


Computer Graphics 4-2 Polygons and Polygon Filling

4.1 Introduction to Polygon SPPU : Dec.-06, May-11,12

In this chapter we are going to study different types of polygons, their representation
and filling algorithms for them.
· A polyline is a chain of connected line segments.

· It is specified by giving the vertices (nodes) P0, P1, P2 … and so on.


· The first vertex is called the initial or starting point and the last vertex is called
the final or terminal point, as shown in the Fig. 4.1.1 (a).
· When starting point and terminal point of any polyline is same, i.e. when polyline
is closed then it is called polygon. This is illustrated in Fig. 4.1.1 (b).
P0
Starting P1 P3
point P1 P2
P0
P2 P4 P4
Terminal
point
P3
(a) Polyline (b) Polygon
Fig. 4.1.1
Review Questions

1. What is polygon ? SPPU : Dec.-06, May-12, Marks 2


2. Is circle a polygon ? Justify SPPU : May-11, Marks 4

4.2 Types : Convex, Concave and Complex


SPPU : May-05,10,11,12, Dec.-06,09,18

· The classification of polygons is based on where the line segment joining any two
points within the polygon is going to lie. There are three types of polygons :
n Convex
n Concave and
n Complex
· A convex polygon is a polygon in which the line segment joining any two points
within the polygon lies completely inside the polygon. Fig. 4.2.1 shows the
examples of convex polygons.
E
D

B C
A F

Fig. 4.2.1 Convex polygons

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4-3 Polygons and Polygon Filling

· A concave polygon is a polygon in which the line segment joining any two points
within the polygon may not lie completely inside the polygon. Fig. 4.2.2 shows the
examples of concave polygons.
A F

C D
B E

Fig. 4.2.2 Concave polygons

· A polygon is simple if it is described by a single,


non-intersecting boundary; otherwise it is said to be
complex.
Fig. 4.2.3 Complex polygon

Review Questions

1. With suitable diagram explain concave and convex polygons ?


SPPU : May-05, Dec.-06,18, Marks 2

2. What are different types of polygons ? Explain with example


SPPU : Dec.-09, May-10, 11, 12, Marks 4

4.3 Representation of Polygon


· There are three approaches to represent polygons according to the graphics
systems :
n Polygon drawing primitive approach
n Trapezoid primitive approach
n Line and point approach
· Some graphics devices supports polygon drawing primitive approach. They
candirectly draw the polygon shapes. On such devices polygons are saved as a
unit.
· Some graphics devices support
trapezoid primitive. In such
devices, trapezoids are formed
from two scan lines and two
line segments as shown in the
Fig. 4.3.1. Here, trapezoids are
(a) Polygon (b) Polygon as a series of trapezoids
drawn by stepping down the
Fig. 4.3.1 Representation of polygon
line segments with two vector
generators and, for each scan
line, filling in all the pixels between them. Therefore every polygon is broken up
into trapezoids and it is represented as a series of trapezoids.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4-4 Polygons and Polygon Filling

· Most of the other graphics devices do not provide any polygon support at all. In
such cases polygons are represented using lines and points. A polygon is
represented as a unit and it is stored in the display file. In a display file polygon
can not be stored only with series of line commands because they do not specify
how many of the following line commands are the part of the polygon. Therefore,
new command is used in the display file to represent polygons. The opcode for
new command itself specify the number of line segments in the polygon. The
Fig. 4.3.2 shows the polygon and its representation using display file.

DF_OP DF_ x DF_ y


(4,6)
6 0 2
2 0 4 (0,4) (6,4)
2 4 6
2 6 4
(0,2) (6,2)
2 6 2
2 4 0 (4,0)

2 0 2
Fig. 4.3.2 Polygon and its representation using display file

Review Question

1. Explain various approaches used to represent polygon.

4.4 Inside Test SPPU : May-05,07,10,13,16,19 Dec.-06,09,10,12,14,15,18

· Once the polygon is entered in the


display file, we can draw the Even x
outline of the polygon.
· To show polygon as a solid object Odd x
we have to set the pixels inside Odd x
the polygon as well as pixels on x
Even
the boundary of it.
· Now the question is how to Fig. 4.4.1
determine whether or not a point
is inside of a polygon. One simple method of doing this is to construct a line
segment between the point in question and a point known to be outside the
polygon, as shown in the Fig. 4.4.1. Now count how many intersections of the line
segment with the polygon boundary occur. If there are an odd number of
intersections, then the point in question is inside; otherwise it is outside. This
method is called the even-odd method of determining polygon inside points.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4-5 Polygons and Polygon Filling

· If the intersection point is vertex of


the polygon then we have to look Counts odd
at the other endpoints of the two x
segments which meet at this
vertex. If these points lie on the
same side of the constructed line, x
then the point in question counts Counts even
as an even number of intersections.
Fig. 4.4.2
If they lie on opposite sides of the
constructed line, then the point is counted as a single intersection. This is
illustrated in Fig. 4.4.2.
· Another approach to do the inside test is the winding-number method. Let us
consider a point in question and a point on the polygon boundary. Conceptually,
we can stretch a piece of elastic between these points and slide the end point on
the polygon boundary for one complete rotation. We can then examine the point
in question to see how many times the elastic has wound around it. If it is wound
at least once, then the point is inside. If there is no net winding, then the point is
outside.
· Like even-odd method, in winding
number method we have to picturise a
line segment running from outside the 1
polygon to the point in question and –1 1
x
consider the polygon sides which it (x, y)
aa
crosses. Here, instead of just counting the
intersections, we have to give a direction
Fig. 4.4.3
number to each boundary line crossed,
and we have to sum these direction numbers. The direction number indicates the
direction the polygon edge was drawn relative to the line segment we have
constructed for the test. This is illustrated in Fig. 4.4.3.
· As shown in the Fig. 4.4.3, point (xa, ya) is a test point and line y = ya is a
horizontal line runs from outside the polygon to point (xa, ya). The polygon edges
crossed by this line could be drawn in two ways. The edge could be drawn
starting below the line, cross it, and end above the line or starting above the line,
cross it, and end below the line. In first case we have to give direction number as
–1 and in the second case we have give direction number as 1. After giving the
direction numbers we have to take sum of these direction numbers which
indicates whether the point in inside the polygon or not. The sum of the direction
numbers for the sides that cross the constructed horizontal line segment is called
the winding number for the point in question. For polygons or two dimensional
objects, the point is said to be inside when the value of winding number is
nonzero. It is imporant to note that the line we choose must not pass through any
vertices.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4-6 Polygons and Polygon Filling

Review Questions

1. Explain even-odd method to determine polygon interior points.


SPPU : Dec.-12,15, May-13,16, Marks 4

2. Explain the different methods for testing a pixel inside of polygon.


SPPU : May-05,07,10,13, Dec.-09,10,12,14, Marks 6

3. Explain the winding number method. Does the method supports intersection polygons?
SPPU : Dec.-06, Marks 4

4. Write and explain any one inside test algorithm. SPPU : Dec.-18, May-19, Marks 4

5. Explain any one inside test algorithm. SPPU : May-19, Marks 2

4.5 Polygon Filling Algorithms


SPPU : May-05,06,07,08,09,10,11,12,13,14,15,18,19, Dec.-05,06,07,08,09,10,11,12,15,19

· Filling the polygon means highlighting all the pixels which lie inside the polygon
with any colour other than background colour. Polygons are easier to fill since
they have linear boundaries.
· There are two basic approaches used to fill the polygon.
· One way to fill a polygon is to start from a given "seed", point known to be inside
the polygon and highlight outward from this point i.e. neighbouring pixels until
we encounter the boundary pixels. This approach is called seed fill because colour
flows from the seed pixel until reaching the polygon boundary, like water flooding
on the surface of the container.
· Another approach to fill the polygon is to apply the inside test i.e. to check
whether the pixel is inside the polygon or outside the polygon and then highlight
pixels which lie inside the polygon. This approach is known as scan-line
algorithm. It avoids the need for a seed pixel but it requires some computation.
Let us see these two methods in detail.

4.5.1 Seed Fill

· The seed fill algorithm is further classified as flood fill algorithm and boundary fill
algorithm.
· Algorithms that fill interior-defined regions are called flood-fill algorithms; those
that fill boundary-defined regions are called boundary-fill algorithms or edge-fill
algorithms.

4.5.1.1 Boundary Fill Algorithm / Edge Fill Algorithm


· In this method, edges of the polygons are drawn. Then starting with some seed,
any point inside the polygon we examine the neighbouring pixels to check

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4-7 Polygons and Polygon Filling

whether the boundary pixel is reached. If boundary pixels are not reached, pixels
are highlighted and the process is continued until boundary pixels are reached.
· Boundary defined regions may be either 4-connected or 8-connected as shown in
the Fig. 4.5.1.
· If a region is 4-connected, then every pixel in the region may be reached by a
combination of moves in only four directions : left, right, up and down.
· For an 8-connected
region every pixel in the
region may be reached
by a combination of
moves in the two
horizontal, two vertical
(a) Four connected region (b) Eight connected region
and four diagonal
Fig. 4.5.1
directions.
· In some cases, an 8-connected algorithm is more accurate than the 4-connected
algorithm. This is illustrated in Fig. 4.5.2. Here, a 4-connected algorithm produces
the partial fill.

Seed

Fig. 4.5.2 Partial filling resulted using 4-connected algorithm

· The following procedure illustrates the recursive method for filling a 4-connected
region with colour specified in parameter fill colour (f-colour) up to a boundary
colour specified with parameter boundary colour (b-colour)
Procedure : boundary_fill (x, y, f_colour, b_colour)
{
if (getpixel (x,y) ! = b_colour && getpixel (x, y) ! = f_colour)

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4-8 Polygons and Polygon Filling

{ putpixel (x, y, f_colour)


boundary_fill (x + 1, y, f_colour, b_colour);
boundary_fill (x, y + 1, f_colour, b_colour);
boundary_fill (x – 1, y, f_colour, b_colour);
boundary_fill (x, y – 1, f_colour, b_colour);
}
}
Note 'getpixel' function gives the colour of specified pixel and 'putpixel' function draws the
pixel with specified colour.
· Same procedure can be modified according to 8 connected region algorithm by
including four additional statements to test diagonal positions, such as
(x + 1, y + 1).

4.5.1.2 Flood Fill Algorithm


· Sometimes it is required to fill in an area that is not defined within a single colour
boundary. In such cases we can fill areas by replacing a specified interior colour
instead of searching for a boundary colour. This approach is called a flood-fill
algorithm.
· Like boundary fill algorithm, here we start with some seed and examine the
neighbouring pixels. However, here pixels are checked for a specified interior
colour instead of boundary colour and they are replaced by new colour.
· Using either a 4-connected or 8-connected approach, we can step through pixel
positions until all interior point have been filled. The following procedure
illustrates the recursive method for filling 8-connected region using flood-fill
algorithm.
Procedure : flood_fill (x, y, old_colour, new_colour).
{
if ( getpixel (x, y) = old_colour)
{ putpixel (x, y, new_colour);
flood_fill (x + 1, y, old_colour, new_colour);
flood_fill (x – 1, y, old_colour, new_colour);
flood_fill (x, y + 1, old_colour, new_colour);
flood_fill (x, y – 1, old_colour, new_colour);
flood_fill (x + 1, y + 1, old_colour, new_colour);
flood_fill (x – 1, y – 1, old_colour, new_colour);
flood_fill (x + 1, y – 1, old_colour, new_colour);

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4-9 Polygons and Polygon Filling

flood_fill (x – 1, y + 1, old_colour, new_colour);


}
}
Note 'getpixel' function gives the colour of specified pixel and 'putpixel' function draws the
pixel with specified colour.

4.5.2 Scan Line Algorithm

· Recursive algorithm for seed fill methods have got two difficulties :
n The first difficulty is that if some inside pixels are already displayed in fill
colour then recursive branch terminates, leaving further internal pixels
unfilled. To avoid this difficulty, we have to first change the colour of any
internal pixels that are initially set to the fill colour before applying the seed
fill procedures.
n Another difficulty with recursive seed fill methods is that it cannot be used
for large polygons. This is because recursive seed fill procedures require
stacking of neighbouring points and in case of large polygons stack space
may be insufficient for stacking of neighbouring points.
· To avoid this problem more efficient method can be used. Such method fills
horizontal pixel spans across scan lines, instead of proceeding to 4-connected or
8-connected neighbouring points. This is achieved by identifying the rightmost and
leftmost pixels of the seed pixel and then drawing a horizontal line between these
two boundary pixels. This procedure is repeated with changing the seed pixel
above and below the line just drawn until complete polygon is filled. With this
efficient method we have to stack only a beginning position for each horizontal
pixel span, instead of stacking all unprocessed neighbouring positions around the
current position.
· Fig. 4.5.3 illustrates the scan line
y
algorithm for filling of polygon.
· For each scan line crossing a polygon,
this algorithm locates the intersection Scan line
points of the scan line with the polygon
edges. These intersection points are then
sorted from left to right and the x
corresponding positions between each 6 9 12 15
intersection pair are set to the specified
fill colour. Fig. 4.5.3

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4 - 10 Polygons and Polygon Filling

· In Fig. 4.5.3, we can see that there are two stretches of interior pixels from x = 6 to
x = 9 and x = 12 to x = 15.
· The scan line algorithm first finds the largest and smallest y values of the
polygon. It then starts with the largest y value and works its way down, scanning
from left to right, in the manner of a raster display.
· The important task in the scan line algorithm is to find the intersection points of
the scan line with the polygon boundary.
· When intersection points are even, they are sorted from left to right, paired and
pixels between paired points are set to the fill colour.
· In some cases intersection
y
point is a vertex. When scan
line intersects polygon vertex B D
a special handling is 1 C
required to find the exact A 2 3 4 G Scan line 1
1 E 3 4
Scan line 2
intersection points. To handle 2 F H
Scan line 3
such cases, we must look at 1 2 3 4
J I
the other endpoints of the
x
two line segments of the 0
polygon which meet at this
Fig. 4.5.4 Intersection points along the scan line
vertex. If these points lie on that intersect polygon vertices
the same (up or down) side
of the scan line, then the point in question counts as an even number of
intersections. If they lie on opposite sides of the scan line, then the point is
counted as single intersection. This is illustrated in Fig. 4.5.4.
· As shown in Fig. 4.5.4, each scan line intersects the vertex or vertices of the
polygon.
· For scan line 1, the other end points (B and D) of the two line segments of the
polygon lie on the same side of the scan line, hence there are two intersections
resulting two pairs : 1 - 2 and 3 - 4. Intersections points 2 and 3 are actually same
points.
· For scan line 2 the other endpoints (D and F) of the two line segments of the
polygon lie on the opposite sides of the scan line, hence there is a single
intersection resulting two pairs : 1 - 2 and 3 - 4.
· For scan line 3, two vertices are the intersection points. For vertex F the other end
points E and G of the two line segments of the polygon lie on the same side of
the scan line whereas for vertex H, the other endpoints G and I of the two line
segments of the polygon lie on the opposite side of the scan line. Therefore, at
vertex F there are two intersections and at vertex H there is only one intersection.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4 - 11 Polygons and Polygon Filling

· This results two pairs : 1 - 2 and 3 - 4 and points 2 and 3 are actually same
points.
· It is necessary to calculate x intersection points for scan line with every polygon
side.
· We can simplify these calculations by using coherence properties.
· A coherence property of a scene is a property of a scene by which we can relate
one part of a scene with the other parts of a scene. Here, we can use a slope of an
edge as a coherence property. By using this property we can determine the x
intersection value on the lower scan line if the x intersection value for current scan
line is known. This is given as
1
xi + 1 = xi –
m

where m is the slope of the edge


· As we scan from top to bottom value of y coordinates between the two scan line
changes by 1.
yi + 1 = yi – 1
· Many times it is not necessary to compute the x intersections for scan line with
every polygon side.
Consider only
· We need to consider only the these sides
y
polygon sides with endpoints
straddling the current scan line. See Scan
Fig. 4.5.5. line

· It will be easier to identify which


polygon sides should be tested for
x-intersection, if we first sort the
sides in order of their maximum
x
y value. 0

· Once the sides are sorted we can Fig. 4.5.5 Consider only the sides which
process the scan lines from the top intersect the scan line
of the polygon to its bottom
producing an active edge list for each scan line crossing the polygon boundaries.
· The active edge list for a scan line contains all edges crossed by that scan line.
· Fig. 4.5.6 shows sorted edges of the polygon with active edges.
· A scan line algorithm for filling a polygon begins by ordering the polygon sides
on the largest y value.
· It begins with the largest y value and scans down the polygon.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4 - 12 Polygons and Polygon Filling

BC
B D BA

C DC
G
A DE

E AJ Top
Scan line
F GF Active
H
edges
GH

J I EF Bottom

HI

JI

Sorted list of edges


Fig. 4.5.6 Sorted edges of the polygon with active edges

· For each y, it determines which sides can be intersected and finds the x values of
these intersection points.
· It then sorts, pairs and passes these x values to a line drawing routine.
Scan Line Conversion Algorithm for Polygon Filling :
1. Read n, the number of vertices of polygon
2. Read x and y coordinates of all vertices in array x[n] and y[n].
3. Find ymin and ymax.
4. Store the initial x value (x1) y values y1 and y2 for two endpoints and x increment
Dx from scan line to scan line for each edge in the array edges [n] [4].
While doing this check that y1 > y2, if not interchange y1 and y2 and
corresponding x1 and x2 so that for each edge, y1 represents its maximum
y coordinate and y2 represents its minimum y coordinate.
5. Sort the rows of array, edges [n] [4] in descending order of y1, descending order
of y2 and ascending order of x2.
6. Set y = ymax
7. Find the active edges and update active edge list :
if (y > y2 and y £ y1)
{ edge is active }
else
{ edge is not active }
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4 - 13 Polygons and Polygon Filling

8. Compute the x intersects for all active edges for current y value [initially
x-intersect is x1 and x intersects for successive y values can be given as
xi + 1 ¬ xi + Dx
1 y -y1
where Dx =– and m = 2 i.e. slope of a line segment
m x2 - x1
9. If x intersect is vertex i.e. x-intersect = x1 and y = y1 then apply vertex test to
check whether to consider one intersect or two intersects. Store all x intersects in
the x-intersect [ ] array.
10. Sort x-intersect [ ] array in the ascending order,
11. Extract pairs of intersects from the sorted x-intersect [ ] array.
12. Pass pairs of x values to line drawing routine to draw corresponding line
segments
13. Set y = y – 1
14. Repeat steps 7 through 13 until y ³ ymin.
15. Stop
In step 7, we have checked for y £ y1 and not simply y < y1. Hence step 9 a becomes
redundant. Following program takes care of that.

Features of scan line algorithm


1. Scan line algorithm is used for filling of polygons.
2. This algorithm solves the problem of hidden surfaces while generating display scan
line.
3. It is used in orthogonal projection.
4. It is non recursive algorithm.
5. In scan line algorithm we have to stack only a beginning position for each
horizontal pixel scan, instead of stacking all unprocessed neighbouring positions
around the current position. Therefore, it is efficient algorithm.

Review Questions

1. What is polygon filling. SPPU : May-18, Dec.-19, Marks 2

2. Explain boundary-fill/edge-fill algorithm for polygon. SPPU : May-05,09, Dec.-19, Marks 6

3. Explain polygon filling by seed-fill algorithm.


SPPU : Dec.-05,08, May-08,18, Marks 6

4. What are the steps involved in filling polygon in scan line method?
SPPU : Dec.-05,06,08, May-06,07,08,18,19, Marks 8

5. Explain with example and compare seed-fill and edge-fill algorithm for polygon.
SPPU : May-06,10, Dec.-07, 09, Marks 8

6. Enlist any three polygon filling algorithms. SPPU : May-13, Marks 4

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 4 - 14 Polygons and Polygon Filling

7. Explain how a polygon is filled with pattern. SPPU : Dec.-12, May-13, Marks 4

8. List various polygon filling algorithms. Explain scan line algorithm with mathematical formulation
SPPU : May-11, Marks 16

9. Compare different polygon filling algorithm. SPPU : Dec.-12, Marks 4

10. State the characteristics of scan line polygon fill algorithm and compare it with boundary fill
algorithm. SPPU : May-09, 10, Marks 8

11. Explain Scanline algorithm for polygon filling and explain how it can be extended for hidden
removal. SPPU : Dec.-10, Marks 10

12. Explain scan line algorithm with example. SPPU : May-14,19, Marks 6

13. What is scan line polygon filling algorithm ? Explain with an example.
SPPU : May-12, Marks 10

14. Describe scan line algorithm to generate solid area on the screen. SPPU : Dec.-11, Marks 8

15. Write algorithm to fill the polygon area using flood fill method. SPPU : May-15, Marks 4

16. Write flood fill algorithm. SPPU : Dec.-15, Marks 3

qqq

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
UNIT - II

5 Windowing and Clipping

Syllabus
Viewing transformations, 2-D clipping: Cohen- Sutherland algorithm line Clipping algorithm,
Sutherland Hodgeman Polygon clipping algorithm, Weiler Atherton Polygon Clipping algorithm.

Contents
5.1 Introduction . . . . . . . . . . . . . . . . . . May-05,13, Dec.-05,12, · · · Marks 4
5.2 Viewing Transformations . . . . . . . . . . . . . . . . . May-05,12, Dec.-10,11,19 · Marks 8
5.3 2 D Clipping . . . . . . . . . . . . . . . . . . May-07,12 · · · · · · · · · · · · · · Marks 2
5.4 Cohen-Sutherland Line Clipping Algorithm . . . May-06,07,08,10,11,12,13, 15, 16,17,19
. . . . . . . . . . . . . . . . . . Dec.-10, 15,18 · · · · · · · · · · Marks 8
5.5 Polygon Clipping . . . . . . . . . . . . . . . . . . Dec.-05,06,07,08,11,14,17,18
. . . . . . . . . . . . . . . . . . May-07,10,14, · · · · · · · · · · · Marks 8
5.6 Generalized Clipping . . . . . . . . . . . . . . . . . . Dec.-07,11,12, May-09, · · · Marks 8
5.7 Interior and Exterior Clipping . . . . . . . . . . . . . . May-05,13, Dec.-05,12, · · · Marks 4

TM

TECHNICAL PUBLICATIONS(5- -An1)up thrust for knowledge


Computer Graphics 5-2 Windowing and Clipping

5.1 Introduction SPPU : May-05,13, Dec.-05,12

· We have to identify the visible part of the picture for inclusion in the display
image. This selection process is not straight forward. Certain lines may lie partly
inside the visible portion of the picture and partly outside. These lines cannot be
omitted entirely from the display image because the image would become
inaccurate. This is illustrated in Fig. 5.1.1.
· The process of selecting and viewing the picture with different views is called
windowing, and a process which divides each element of the picture into its
visible and invisible portions, allowing the invisible portion to be discarded is
called clipping.

Fig. 5.1.1 Concept of windowing and clipping


Review Questions

1. What is window ? SPPU : May-05, Marks 2

2. What is windowing and clipping ? SPPU : May-13, Dec.-05, 12, Marks 4

5.2 Viewing Transformations SPPU : May-05,12, Dec.-10,11,19

· The picture is stored in the computer memory using any convenient Cartesian
co-ordinate system, referred to as World Co-ordinate System (WCS).
· When picture is displayed on the display device it is measured in Physical Device
Co-ordinate System (PDCS) corresponding to the display device. Therefore,
displaying an image of a picture involves mapping the co-ordinates of the points
and lines that form the picture into the appropriate physical device co-ordinate
where the image is to be displayed. This mapping of co-ordinates is achieved with
the use of co-ordinate transformation known as viewing transformation.
· Sometimes the two dimensional viewing transformation is simply referred to as
the window to view port transformation or the windowing transformation.
· The viewing transformation which maps picture co-ordinates in the WCS to
display co-ordinates in PDCS is performed by the following transformations.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5-3 Windowing and Clipping

n Converting world co-ordinates to viewing co-ordinates.


n Normalizing viewing co-ordinates.
n Converting normalized viewing coordinates to device co-ordinates

Determine
Convert
world - Convert
normalized
MC co-ordinate WC world VC Normalize NVC DC
viewing
of scene using co-ordinates viewing
co-ordinates
modeling - to viewing co-ordinates
to device
co-ordinate co-ordinates
co-ordinates
transformations

Fig. 5.2.1 (a) Two-dimensional viewing transformation pipeline

· World Co-ordinate System (WCS) is infinite in extent and the device display area
is finite.
· To perform a viewing transformation we select a finite world co-ordinate area for
display called a window.
· An area on a device to which a window is mapped is called a viewport.
· The window defines what is to be viewed; the viewport defines where it is to be
displayed, as shown in the Fig. 5.2.1 (b).

Window yv max
yw max
View port

yw min yv min

xv min xv max
xw min xw max

World co-ordinates Device co-ordinates


Fig. 5.2.1 (b) Window and viewport
The steps involved in viewing transformations :
1. Construct the scene in world co-ordinates using the output primitives and attributes.
2. Obtain a particular orientation for the window by setting a two-dimensional
viewing co-ordinate system in the world-co-ordinate plane and define a window in
the viewing co-ordinate system.
3. Use viewing co-ordinates reference frame to provide a method for setting up
arbitrary orientations for rectangular windows.
4. Once the viewing reference frame is established, transform descriptions in world
co-ordinates to viewing co-ordinates.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5-4 Windowing and Clipping

5. Define a view port in normalized co-ordinates and map the viewing co-ordinate
description of the scene to normalized co-ordinates.
6. Clip all the parts of the picture which lie outside the viewport.

5.2.1 Viewing Co-ordinate Reference Frame

· Fig. 5.2.1 (b) shows the two dimensional viewing pipeline.


· The window defined in world co-ordinate is first transformed into the viewport
co-ordinate. For this, viewing co-ordinate reference frame is used. It provides a
method for setting up arbitrary orientations for rectangular windows.
· To obtain the matrix for converting world co-ordinate positions to viewing
co-ordinates, we have to translate the viewing origin to the world origin and then
align the two co-ordinate reference frames by rotation. This is illustrated in
Fig. 5.2.2.
· As shown in the Fig. 5.2.2, the composite transformation (translation and rotation)
converts world co-ordinates to viewing co-ordinates.
y y view
world y world
ew
vi
y

y0
T
x view
x0 x world x world
R
x
vi
ew

a) Translation of the viewing b) Rotation to align the axes of the


origin to the world origin. two systems.

Fig. 5.2.2

· The matrix used for this composite transformation is given by


M WC, VC = T. R

where T is the translation matrix that takes the viewing origin point P0 to the world
origin, and R is the rotation matrix that aligns the axes of the two reference frames.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5-5 Windowing and Clipping

5.2.2 Transformation to Normalized Co-ordinates

· Once the viewing reference frame is established, we can transform, descriptions in


word co-ordinate to viewing co-ordinates. We then define a viewport in
normalized co-ordinate.
Normalized Co-ordinates
· The different display devices may have different screen sizes as measured in
pixels.
· Size of the screen in pixels increases as resolution of the screen increases.
· When picture is defined in the pixel values then it is displayed large in size on the
low resolution screen while small in size on the high resolution screen as shown
in the Fig. 5.2.3.

(a) More resolution (b) Less resolution


Fig. 5.2.3 Picture definition in pixels

· To avoid this and to make our programs to be device independent, we have to


define the picture co-ordinates in some units other than pixels and use the
interpreter to convert these co-ordinates to appropriate pixel values for the
particular display device. The device independent units are called the normalized
device co-ordinates. In these units, the screen measures 1 unit wide and 1 unit
length as shown in the Fig. 5.2.4.
· The lower left corner of the screen is the origin, and the upper-right corner is the
point (1, 1).
· The point (0.5, 0.5) is the center of the screen no matter what the physical
dimensions or resolution of the actual display device may be.
· The interpreter uses a simple linear formula to convert the normalized device
co-ordinates to the actual device co-ordinates.
x = xn ´ XW … (5.2.1)
y = yn ´ YH … (5.2.2)

where
x : Actual device x co-ordinate.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5-6 Windowing and Clipping

y : Actual device y co-ordinate.


(0, 1) (1, 1)
xn : Normalized x co-ordinate.
yn : Normalized y co-ordinate.
XW : Width of actual screen in pixels.
YH : Height of actual screen in pixels.
(0, 0) (1, 0)
· The transformation which map the
Fig. 5.2.4 Picture definition in normalized
viewing co-ordinate to normalized device co-ordinates
device co-ordinate is called
normalization transformation. It involves scaling of x and y, thus it is also
referred to as scaling transformation.

5.2.3 Window to Viewport Co-ordinate Transformation

· Once the window co-ordinates are transferred to viewing co-ordinates we choose


the window extents in viewing co-ordinates and select the viewport limits in
normalized co-ordinates. Object descriptions are then transferred to normalize
device co-ordinates. In this transformation, the relative placement of the object in
the normalized co-ordinates is same as in the viewing co-ordinates.
· Fig. 5.2.5 shows the mapping of object from window to viewport.
· A point at position (x w , yw) in the window is mapped into position (xv, yv) in the
associated viewport.

ywmax
yvmax

(xw, yw) (xv, yv)


ywmin yvmin

xwmin xwmax xvmin xvmax


Fig. 5.2.5 Window to viewport mapping

· To maintain the same relative placement in the viewport as in the window, we


require that
x v - x vmin x w - x wmin
=
x vmax - x vmin x wmax - x wmin
y v - y vmin y w - y wmin
=
y vmax - y vmin y wmax - y wmin

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5-7 Windowing and Clipping

Solving these equations for the viewport position (xv, yv) we have,
xv = xvmin + (xw – xwmin) sx
yv = yvmin + (yw – ywmin) sy

where scaling factors are


x vmax - x vmin
sx =
x wmax - x wmin
y vmax - y vmin
sy =
y wmax - y wmin

· The mapping of window to viewport can also be achieved by performing


following transformations :
1. By performing scaling transformation using a fixed-point position of
(xwmin, ywmin) that scales the window area to the size of the viewport area.
2. By translating the scaled window area to the position of the viewport area.
· While performing these transformations scaling factors sx and sy are kept same to
maintain the relative proportions of objects.
· The character strings are transformed in two ways; they are either transformed
without any change or if they are formed with line segments, they are transformed
as a sequence of line transformations.
Workstation Transformation
· The transformation of object description from normalized co-ordinates to device
co-ordinates is called workstation transformation.
· The workstation transformation is accomplished by selecting a window area in
normalized space and a viewport area in the co-ordinates of the display device.
· By using workstation transformation we can partition a view so that different
parts of normalized space can be displayed on different output devices, as shown
in the Fig. 5.2.6. (See Fig. 5.2.6 on next page).
· The workstation transformation is achieved by performing following steps :
1. The object together with its window is translated until the lower left corner of
the window is at the origin.
2. Object and window are scaled until the window has the dimensions of the
viewport.
3. Translate the viewport to its correct position on the screen.
This is illustrated in Fig. 5.2.7. (See Fig. 5.2.7 on page 5 - 9)
Computer Graphics 5-8 Windowing and Clipping

Normalized space
1
View port

Window 1 Window 2
0 1

Monitor 1 Monitor 2

Fig. 5.2.6 Mapping selected portion of the scene in normalized co-ordinates to different
monitors with workstation transformations

· The workstation transformation is given as


W = T × S × T –1 … (5.2.3)
· The transformation matrices for individual transformation are as given below :
é 1 0 0ù
ê ú
T = ê 0 1 0ú
ê - X w min - y w min 1úû
ë

éSx 0 0ù
x v max - x v min
S = ê0 Sy 0ú where Sx =
ê ú x w max - x w min
êë 0 0 1úû

y vmax - y vmin
Sy =
y wmax - y wmin

é 1 0 0ù
–1 ê ú
T = ê 0 1 0ú
ê x v min y vmin 1úû
ë
· The overall transformation matrix for W is given as
W = T × S × T –1

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5-9 Windowing and Clipping

Object Translate

Window

Scale Translate

Fig. 5.2.7 Steps in workstation transformation

é 1 0 0ù éSx 0 0ù é 1 0 0ù
ê ú ê0 ê ú
= ê 0 1 0ú Sy 0ú ê 0 1 0ú
ê ú
ê - X w min - y w min 1úû êë 0 0 1úû ê x v min y v min 1úû
ë ë

é Sx 0 0ù
ê ú
= ê 0 Sy 0ú
ê x vmin - x w min . S x y v min - y w min . S y 1úû
ë
· Fig. 5.2.8 shows the complete viewing transformation.

Viewport Normalized Device


co-ordinate Normalization Translate Scale Translate co-ordinates
(VC) transformation co-ordinates (DC)
(NC)

Workstation transformation

Viewing transformation

Fig. 5.2.8 Viewing transformation

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 10 Windowing and Clipping

Applications
· By changing the position of the viewport, we can view objects at different
positions on the display area of an output device.
· By varying the size of viewports, we can change the size and proportions of
displayed objects.
· We can achieve zooming effects by successively mapping different-sized windows
on a fixed-size viewport.
Example 5.2.1 Find the normalization transformation window to viewpoint, with window,
lower left corner at (1, 1) and upper right corner at (3, 5) onto a viewpoint with lower left
corner at (0, 0) and upper right corner at (1/2, 1/2).
Solution : Given : Co-ordinates for window

xw min = 1 yw min = 1
xw max = 3 yw max = 5

Co-ordinates for view port


xv min = 0 yv min = 0
xv max = 1/2 = 0.5 yv max = 1/2 = 0.5

We know that,
x v max - x v min 0 .5 - 0
Sx = = = 0.25
x w max - x w min 3 -1
y v max - y v min 0 .5 - 0
and Sy = = = 0.125
y w max - y w min 5 -1

We know that transformation matrix is given as,


é Sx 0 0ù
–1 ê ú
T× S× T = ê 0 Sy 0ú
ê x v min - x w min S x y v min - y w min S y 1 úû
ë

é 0 . 25 0 0ù
= ê 0 0 . 125 0ú
ê ú
êë 0 - (1 ´ 0 . 25) 0 - (1 ´ 0 . 125) 1úû

é 0 . 25 0 0ù
= ê 0 0 . 125 0ú
ê ú
êë - 0 . 25 - 0 . 125 1úû

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 11 Windowing and Clipping

Example 5.2.2 Find the normalization transformation N that uses the rectangle
A (1, 1), B (5, 3), C (4, 5), D (0, 3) as a window and the normalized device screen as a
viewport.
Solution : To align rectangle about A we have to rotate it with the co-ordinate axes.
Then we can calculate, S x and S y and finally we compose the rotation and the
transformation N to find the required normalization transformation N R .
The slope of the line segment AB is,
3 -1 1
m = =
5 -1 2

The Fig. 5.2.9 (a) shows the specified window and viewport. As shown in the
Fig. 5.2.9 (a), the angle of rotation is - q and it is given by,
2 1
tan q = =
4 2
y
C(4, 5)

3 (0, 1) (1,1)
D B(5, 3)

A(1, 1) 4 (0, 0) (1,0)


x

(a) Window (b) Viewport


Fig. 5.2.9
Thus,
1 1 2 2
sin q = and so sin ( - q) = - , cos q = , cos ( - q) =
5 5 5 5
Referring section 3.7.1.
The rotation matrix about A (1, 1) is given as,
æ 2 -1 ö
ç 0÷
ç 5 5 ÷
ç 1 2 ÷
R - q, A = ç 0÷
5 5
ç ÷
çç æç 1 - 3 ö÷ æç 1 - 1 ö÷ 1 ÷÷
èè 5ø è 5ø ø

The x extent of the rotated window is the length of AB. Similarly, the y extent is the
length of AD. Using the distance formula we can calculate these length as,

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 12 Windowing and Clipping

d (A, B) = 22 + 4 2 = 20 = 2 5

d (A, D) = 12 + 22 = 5

Also, the x extent of the normalized device screen is 1, as is the y extent. Calculating
s x and s y ,
Viewport x extent 1
sx = =
Window x extent 2 5
Viewport y extent 1
sy = =
Window y extent 5

é sx 0 0ù
ê ú
We have, N = ê 0 sy 0ú
ê - s x x W min + x V min - s y y W min + y V min 1úû
ë

where x W min = 1, x V min = 0, y W min = 1 and y V min = 0


é 1 ù é 1 ù
ê 0 0ú ê 0 0ú
2 5 2 5
ê 1 ú ê 1 ú
= ê 0 0ú = ê 0 0ú
ê 5 ú ê 5 ú
ê -1 -1 ú ê -1 -1 ú
ê2 5 ´ 1+ 0 5
´ 1 + 0 1ú ê 1ú
ë û ë2 5 5 û

The normalization transformation is then,


é 2 1 ù é 1 ù
ê - 0ú ê2 5 0 0ú
ê 5 5 ú
1 2 ê 1 ú
N R = R - qA ×N = ê 0ú ê 0 0ú
ê 5 5 ú ê 5 ú
êæ 3 ö æ 1 ö ú ê -1 -1 ú
êç1 - ÷ ç1 - ÷ 1ú ê2 5 1ú
ëè 5ø è 5ø û ë 5 û

é 1 1 ù
ê 5 - 0ú
5
ê 1 2 ú
= ê 0ú
10 5
ê- 3 -1 ú
ê 1ú
ë 10 5 û

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 13 Windowing and Clipping

Example 5.2.3 Find the complete viewing transformation that maps a window in world
coordinates with x extent 1 to 10 and y extent 1 to 10 onto a viewport with x extent 1 4
to 3 4 and y extent 0 to 1 2 in normalized device space and then maps a workstation
window with x extent 1 4 to 1 2 and y extent 1 4 to 1 2 in the normalized device space
into a workstation viewport with x extent 1 to 10 and y extent 1 to 10 on the physical
display device.
Solution : For normalization transformation
é sx 0 0ù
ê ú
T = ê 0 sy 0ú
ê - s x x wmin + x vmin - s y y wmin + y vmin 1úû
ë
The given parameters are x wmin = 1, x wmax = 10, y wmin = 1,
1
y wmax = 10, x vmin = 1 4, x vmax = 3 4, y vmin = 0, and y vmax =
2
3 1 1
Viewport x extent - 1
\ sx = = 4 4 = 2 =
Window x extent 10 - 1 9 18
1 1
Viewport y extent - 0 1
sy = = 2 = 2 =
Window y extent 10 - 1 9 18

Substituting values of S x and S y we have


é 1 ù é1 ù
ê 18 0 0ú ê 18 0 0ú
ê 1 ú ê 1 ú
T1 = ê 0 0ú = ê 0 0ú
18 18
ê 1 1 1 ú ê7 1 ú
ê- + - + 0 1ú ê - 1ú
ë 18 4 18 û ë 36 18 û
1 1
The given parameters for workstation transformation are x wmin = , x wmax = ,
4 2
1 1
y wmin = , y wmax = , x vmin = 1, x vmax = 10, y vmin = 1 and y vmax = 10.
4 2
10 - 1 9
sx = = = 36
1 1 1
-
2 4 4
10 - 1 9
sy = = = 36
1 1 1
-
2 4 4
é 36 0 0ù é 36 0 0ù
ê ú ê 0
\ T2 = ê 0 36 0ú 36 0ú
ê ú
ê - 36 × 1 + 1 - 36 × 1 + 1 1ú êë - 8 - 8 1úû
ë 4 4 û
The complete viewing transformation will be T = T1 ´ T2

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 14 Windowing and Clipping

é1 ù
ê 18 0 0ú
é 36 0 0ù é 2 0 0ù
ê 1 ú ê 0 ú ê
T = ê0 0ú 36 0 = 0 2 0ú
18 ê ú ê ú
ê7 1 ú êë - 8 - 8 1úû êë - 1 - 10 1úû
ê - 1ú
ë 36 18 û

Review Questions

1. What is window and viewport ? SPPU : May-05, Marks 4

2. State the application of viewing transformation. SPPU : May-05, Marks 4

3. Describe viewing transformation. SPPU : Dec.-10, 11,19, May-12, Marks 8

5.3 2 D Clipping SPPU : May-07, 12

· The procedure that identifies the portions of a picture that are either inside or
outside of a specified region of space is referred to as clipping.
· The region against which an object is to be clipped is called a clip window or
clipping window. It usually is in a rectangular shape, as shown in the Fig. 5.3.1.
· The clipping algorithm determines which points, lines or portions of lines lie
within the clipping window. These points, lines or portions of lines are retained
for display. All others are discarded.
P9
Clipping window
P4
P2 P2
P10
P11
P1 P12 P1 P12
P8
P6 P6
P5 P8'
P3 P'5

P7'
P7
(a) Before clipping (b) After clipping
Fig. 5.3.1
5.3.1 Point Clipping

· The points are said to be interior to the clipping window if


xw min £ x £ xw max and yw min £ y £ yw max

The equal sign indicates that points on the window boundary are included within the
window.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 15 Windowing and Clipping

5.3.2 Line Clipping

· The lines are said to be interior to the clipping window and hence visible if both
end points are interior to the window, e.g. line P1 P2 in Fig. 5.3.1.
· If both end points of a line are exterior to the window, the line is not necessarily
completely exterior to the window, e.g. line P7 P8 in Fig. 5.3.1.
· If both end points of a line are completely to the right of, completely to the left of,
completely above, or completely below the window, then the line is completely
exterior to the window and hence invisible. For example, line P3 P4 in Fig. 5.3.1.
· The lines which across one or more clipping boundaries require calculation of
multiple intersection points to decide the visible portion of them.
· To minimize the intersection calculations and to increase the efficiency of the
clipping algorithm, initially, completely visible and invisible lines are identified
and then the intersection points are calculated for remaining lines.
· There are many line clipping algorithms. Let us discuss a few of them.

Review Questions

1. What is point clipping


2. What is line clipping ? SPPU : May-07,
May-12, Marks 2

5.4 Cohen-Sutherland Line Clipping Algorithm


SPPU : May-06,07,08,10,11,12,13,15,16,17,19, Dec.-10,15,18

· This is one of the oldest and most popular line clipping algorithm developed by
Dan Cohen and Ivan Sutherland.
· To speed up the processing this algorithm performs initial tests that reduce the
number of intersections that must be calculated.
· This algorithm uses a four digit (bit) code to indicate which of nine regions
contain the end point of line.
· The four bit codes are called region codes or 1001 1000 1010
outcodes. These codes identify the location of
the point relative to the boundaries of the 0000
0001 window 0010
clipping rectangle as shown in the Fig. 5.4.1.
· Each bit position in the region code is used to
indicate one of the four relative co-ordinate 0101 0100 0110
positions of the point with respect to the
clipping window : to the left, right, top or Fig. 5.4.1 Four-bit codes for nine
regions

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 16 Windowing and Clipping

bottom. The rightmost bit is the first bit and the bits are set to 1 based on the
following scheme :
n Set Bit 1 - if the end point is to the left of the window
n Set Bit 2 - if the end point is to the right of the window
n Set Bit 3 - if the end point is below the window
n Set Bit 4 - if the end point is above the window
Otherwise, the bit is set to zero.
· Once we have established region codes for all the line endpoints, we can
determine which lines are completely inside the clipping window and which are
clearly outside.
· Any lines that are completely inside the window boundaries have a region code of
0000 for both endpoints and we trivially accept these lines.
· Any lines that have a 1 in the same bit position in the region codes for each
endpoint are completely outside the clipping rectangle, and we trivially reject
these lines.
· A method used to test lines for total clipping is equivalent to the logical AND
operator.
· If the result of the logical AND operation with two end point codes is not 0000,
the line is completely outside the clipping region.
· The lines that cannot be identified as completely inside or completely outside a
clipping window by these tests are checked for intersection with the window
boundaries.
Example 5.4.1 Consider the clipping window and the lines shown in Fig. 5.4.2. Find the
region codes for each end point and identify whether the line is completely visible, partially
visible or completely invisible.
P9

P4
P2

P10
P1 P8
P6
P3 P5

P7

Fig. 5.4.2

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 17 Windowing and Clipping

Solution : The Fig. 5.4.3 shows the clipping window and lines with region codes. These
codes are tabulated and end point codes are logically ANDed to identify the visibility of
the line in Table 5.4.1.

1001 1000 1010


P9

P4 P2

P10
0001 P1 0000 P8 0010
P6
P5
P3

P7
0101 0100 0110

Fig. 5.4.3

Line End point codes Logical ANDing Result

P1 P 2 0000 0000 0000 Completely visible

P3 P 4 0001 0001 0001 Completely invisible

P5 P 6 0001 0000 0000 Partially visible

P7 P 8 0100 0010 0000 Partially visible

P9 P10 1000 0010 0000 Partially visible

Table 5.4.1
· The Cohen-Sutherland algorithm begins the clipping process for a partially visible
line by comparing an outside endpoint to a clipping boundary to determine how
much of the line can be discarded. Then the remaining part of the line is checked
against the other boundaries, and the process is continued until either the line is
totally discarded or a section is found inside the window.
This is illustrated in Fig. 5.4.4.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 18 Windowing and Clipping

P2 P2 P2

P1' P1'

P1 P1

(a) (b) (c)

P2
P2' P2'

P1' P1'

(d) (e)
Fig. 5.4.4 Sutherland-Cohen subdivision line clipping

· As shown in the Fig. 5.4.4, line P1 P2 is a partially visible and point P1 is outside
the window. Starting with point P1, the intersection point P1¢ is found and we get
two line segments P1 – P1¢ and P1¢ – P2.
· We know that, for P1 – P1¢ one end point i.e. P1 is outside the window and thus
the line segment P1 – P1¢ is discarded.
· The line is now reduced to the section from P1¢ to P2. Since P2 is outside the clip
window, it is checked against the boundaries and intersection point P2¢ is found.
Again the line segment is divided into two segments giving P1¢ – P2¢ and P2¢ – P2 .
We know that, for P2¢ – P2 one end point i.e. P2 is outside the window and thus the
line segment P2¢ – P2 is discarded.
· The remaining line segment P1¢ – P2¢ is completely inside the clipping window and
hence made visible.
· The intersection points with a clipping boundary can be calculated using the
slope-intercept form of the line equation.
· The equation for line passing through points P1 (x1, y1) and P2 (x2, y2) is
y = m(x – x1) + y1 or y = m(x – x2) + y2 … (5.4.1)
y2 -y1
where m = (slope of the line)
x2 - x1

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 19 Windowing and Clipping

· Therefore, the intersections with the clipping boundaries of the window are given
as :
n Left : xL, y = m(xL – x1) + y1 ; m¹¥
n Right : xR, y = m(xR – x1) + y1 ; m¹¥
1
n Top : yT, x = x1 + æç ö÷ (yT – y1) ; m¹0
è mø
1
n Bottom : yB, x = x1 + æç ö÷ (yB – y1) ; m¹0
è mø

Sutherland and Cohen subdivision line clipping algorithm :


1. Read two end points of the line say P1 (x1, y1) P2

and P2 (x2, y2). (Wx1, Wy1)

2. Read two corners (left-top and right-bottom) of


the window, say (Wx1, Wy1 and Wx2, Wy2).
3. Assign the region codes for two endpoints P1 and P1
P2 using following steps :
Initialize code with bits 0000 (Wx2, Wy2)
Fig. 5.4.5
Set Bit 1 - if (x < Wx1)
Set Bit 2 - if (x > Wx2)
Set Bit 3 - if (y < Wy2)
Set Bit 4 - if (y > Wy1)
4. Check for visibility of line P1 P2
a) If region codes for both endpoints P1 and P2 are zero then the line is
completely visible. Hence draw the line and go to step 9.
b) If region codes for endpoints are not zero and the logical ANDing of them is
also non-zero then the line is completely invisible, so reject the line and go to
step 9.
c) If region codes for two endpoints do not satisfy the conditions in 4a) and
4b)the line is partially visible.
5. Determine the intersecting edge of the clipping window by inspecting the region
codes of two endpoints.
a) If region codes for both the end points are non-zero, find intersection points
P1¢ and P2¢ with boundary edges of clipping window with respect to point P1
and point P2, respectively
b) If region code for any one end point is non-zero then find intersection point
P1¢ or P2¢ with the boundary edge of the clipping window with respect to it.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 20 Windowing and Clipping

6. Divide the line segments considering intersection points.


7. Reject the line segment if any one end point of it appears outsides the clipping
window.
8. Draw the remaining line segments.
9. Stop.
Example 5.4.2 Use the Cohen-Sutherland outcode algorithm to clip two lines
P1 (40, 15) – P2 (75, 45) and P3 (70, 20) – P4 (100, 10) against a window A (50, 10),
B (80, 10), C (80, 40), D(50,40).
Solution : Line 1 : P1 (40, 15) P2 (75, 45) xL = 50 y B = 10 xR = 80 yT = 40
Point Endcode ANDing Position
P1 0001 0000 Partially visible
P2 0000

6 45 - 15 6
xL, y = m (xL – x) + y1 = (50 – 40) + 15 m= =
7 75 - 40 7

= 23.57 P2(75, 45)


(50, 40) I2
(80, 40)
æ1ö æ7ö
yT, x = x 1 + ç ÷ (y T - y 1 ) = 40 + ç ÷ (40 – 15)
èm ø è6ø
I1

= 69.16 P1(40, 15)


(50, 10) (80, 10)
I1 = (50, 23.57) I 2 = (69.06, 40)
Fig. 5.4.6
Line 2 : P3 (70, 20) P4 (100, 10)

Point Endcode ANDing Position


P3 0000 0000 Partially visible
P4 0010

10 - 20 - 10 - 1 (50,40)
(80,40)
Slope m ¢ = = =
100 - 70 30 3
-1
x R , y = m (xR – x1) + y1 = (80 – 70) + 20 = 16.66 I 2 (80,16.66)
3
I = (80, 16.66) (50,10) (80,10)
Fig. 5.4.6 (a)

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 21 Windowing and Clipping

Example 5.4.3 Use outcode based line clipping (–8, 8) (12, 8)


method to clip a line starting from (– 13, 5)
and ending at (17, 11) against the window
having its lower left corner at (– 8, – 4) and (–8, –4) (12, –4)
upper right corner at (12, 8).
Fig. 5.4.7

Solution : xL = – 8 yB = – 4
x R = 12 yT = 8

Given line : P1 (–13, 5), P2 (17, 11)

Point Endcode ANDing Position

P1 0001 0000 Partly visible

P2 1010

6 1
Slope of line : m = =
30 5 P2
1 (17, 11)
xL, y = m (xL – x) + y1 = (– 8 – (– 13)) + 5 I2
5 (–8, 8) (12, 8)
I1
1
= (– 8 + 13) + 5 = 1 + 5 = 6
5 (–13, 5)
P1
1
y T , x = x1 + (y T - y) + x = – 13 + 5 (8 – 5)
m (–8, –4) (12, –4)

= – 13 + 15 = 2 Fig. 5.4.7 (a)

I1 = (xL , xL,y) = (– 8, 6) (
I 2 = YT, x , YT ) = (2, 8)
Example 5.4.4 Let R be the rectangular window whose lower left-hand corner is at L ( - 3, 1)
and upper right-hand corner is at R (2, 6). If the line segment is defined with two and
points with A ( - 4, 2) and B( - 1, 7),
a) The region codes of the two end points.
b) Its clipping category and
c) Stages in the clipping operations using Cohen-sutherland algorithm.
Solution :
a) Region of code for point A = 0001
Region of code for point B = 1000
b) Since (0001) AND (1000) = 0000 line segment AB is partially visible.
c) Stages in the clipping line segment AB are :

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 22 Windowing and Clipping

1. To push the 1 to 0 in code for A (0001), we clip against the window boundary
line x min = - 3.
y

1000
8
B(–1, 7)
7
1001 R (2, 6)1010
I2 6
5
4
0001 I1 0000 0010
3
A (–4, 2)
2
1
L(–3, 1)
0 x
–6 –5 –4 –3 –2 –1 1 2 3 4 5 6

0101 0100 0110

Fig. 5.4.8
y2 -y1 7- 2 5
2. m = = =
x2 - x1 - 1 - ( - 4) 3

3. We obtain the intersection point as,


5
I 1 = x min , m (x min - x 1 ) + y 1 = – 3, [ - 3 - ( - 4)] + 2
3
11
= – 3,
3
æ1ö 3
I2 = x 1 + ç ÷ ( y max - y 1 ), y max = - 4 + (6 - 2), 6
è mø 5
-8
= ,6
5
4. Clip (do not display) segments AI 1 AI 2 .
5. Display segment I1I2 since both end points lie in the window.
Example 5.4.5 Clip the line PQ having coordinates P(4, 1) and Q(6, 4) against the clip
window having vertices A(3, 2), B(7, 2), C(7, 6) and D(3, 6) using Cohen Sutherland line
clipping algorithm.
Solution : Line : A (4, 1), B (6, 4), xL = 3, y B = 2, xR = 7, y T = 6

Point Endcode ANDing Position


P 0100 0000 Partially visible
Q 0000

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 23 Windowing and Clipping

4 -1 3 (3, 6) D (7, 6) C
Line slope m = =
6-4 2
æ1ö (6, 4)
y B ,x = x 1 + ç ÷ (y B - y 1 ) Q
è mø

æ1ö
= x P + ç ÷ (2 - y p ) (3, 2) I (7, 2)
è mø A B
(4, 1)
2 2 14 P
= 4 +
3
( 2 - 1) = 4 + 3 = 3
Fig. 5.4.9
æ 14 ö
I = ( y B , x, y B ) = ç , 2÷
è 3 ø

Example for Practice

Example 5.4.6 : Find the clipping co-ordinates to clip the line segment P1 P2 against the
window ABCD using Cohen Sutherland line clipping algorithm
P1 = (10, 30) and P2 = ( 80, 90) window ABCD - A (20, 20), B (90, 20),
C (90, 70) and D (20, 70).

Review Question

1. Explain Cohen-Sutherland algorithm with the help of suitable example.


SPPU : May-06, 07, 08, 10, 11, 12, 13, 15, 16,17,19, Dec.-10, 15,18, Marks 8

5.5 Polygon Clipping SPPU : Dec.-05,06,07,08,11,14,17,18, May-07,10,14

· A polygon is nothing but the collection of lines. Therefore, we might think that
line clipping algorithm can be used directly for polygon clipping. However, when
a closed polygon is clipped as a collection of lines with line clipping algorithm,
the original closed polygon becomes one or more open polygon or discrete lines as
shown in the Fig. 5.5.1. Thus, we need to modify the line clipping algorithm to
clip polygons.
· We consider a
polygon as a
closed solid area.
Hence after
clipping it should
remain closed. To
achieve this we
require an
(a) Before clipping (b) After clipping
algorithm that
Fig. 5.5.1 Polygon clipping done by line clipping algorithm

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 24 Windowing and Clipping

will generate additional line segment which make the polygon as a closed area.
· For example, in Fig. 5.5.2 the lines a - b, c - d, d - e, f - g, and h - i are added to
polygon description to make it closed.

a b c d

i
e

g f

(a) (b)
Fig. 5.5.2 Modifying the line clipping algorithm for polygon

a b

e d

Fig. 5.5.3 Disjoint polygons in polygon clipping

· Adding lines c - d and d - e is particularly difficult. Considerable difficulty also


occurs when clipping a polygon results in several disjoint smaller polygons as
shown in the Fig. 5.5.3. For example, the lines a - b, c - d, d - e and g - f are
frequently included in the clipped polygon description which is not desired.

5.5.1 Sutherland - Hodgeman Polygon Clipping

· A polygon can be clipped by processing its boundary as a whole against each


window edge. This is achieved by processing all polygon vertices against each clip
rectangle boundary in turn. Beginning with the original set of polygon vertices, we
could first clip the polygon against the left rectangle boundary to produce a new
sequence of vertices.
· The new set of vertices could then be successively passed to a right boundary
clipper, a top boundary clipper and a bottom boundary clipper, as shown in
Fig. 5.5.4. At each step a new set of polygon vertices is generated and passed to
the next window boundary clipper. This is the fundamental idea used in the
Sutherland - Hodgeman algorithm.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 25 Windowing and Clipping

Original polygon Left clipped Right clipped

Top clipped Bottom clipped


Fig. 5.5.4 Clipping a polygon against successive window boundaries

· The output of the algorithm is a list of polygon vertices all of which are on the
visible side of a clipping plane. Such each edge of the polygon is individually
compared with the clipping plane.

V1' V2 V1
V1

V2

V1 – Outside V1 – Inside
V2 – Inside V2 – Inside
Save V'1 and V2 Save V2

(a) (b)

V2

V1
V1' V1
V2

V1 – Inside V1 – Outside
V2 – Outside V2 – Outside
Save V'1 Save nothing

(c) (d)
Fig. 5.5.5 Processing of edges of the polygon against the left window boundary

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 26 Windowing and Clipping

This is achieved by processing two vertices of each edge of the polygon around the
clipping boundary or plane. This results in four possible relationships between the edge
and the clipping boundary or plane. (See Fig. 5.5.5).
1. If the first vertex of the edge is outside the window boundary and the second
vertex of the edge is inside then the intersection point of the polygon edge with
the window boundary and the second vertex are added to the output vertex list
(See Fig. 5.5.5 (a)).
2. If both vertices of the edge are inside the window boundary, only the second
vertex is added to the output vertex list. (See Fig. 5.5.5 (b)).
3. If the first vertex of the edge is inside the window boundary and the second vertex
of the edge is outside, only the edge intersection with the window boundary is
added to the output vertex list. (See Fig. 5.5.5 (c)).
4. If both vertices of the edge are outside the window boundary, nothing is added to
the output list. (See Fig. 5.5.5 (d)).
· Once all vertices are processed for one clip window boundary, the output list of
vertices is clipped against the next window boundary.
· Going through above four cases we can realize that there are two key processes in
this algorithm.
1. Determining the visibility of a point or vertex (Inside - Outside test) and
2. Determining the intersection of the polygon edge and the clipping plane.
· One way of determining the visibility of a point or vertex is described here.
· Consider that two points A and B define the window boundary and point under
consideration is V, then these three points define a plane.
· Two vectors which lie in that plane are AB and AV. If this plane is considered in
the xy plane, then the vector cross product AV ´ AB has only a z component
given by (xV – xA) (yB – yA) – (yV – yA) (xB – xA).
· The sign of the z component decides the position of point V with respect to
window boundary.
If z is : Positive – Point is on the right side of the window boundary
Zero – Point is on the window boundary
Negative – Point is on the left side of the window boundary
Example 5.5.1 Consider the clipping boundary as shown in the Fig. 5.5.6 and determine the
positions of points V1 and V2.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 27 Windowing and Clipping

6
B(2,5)
4
V1(1,3)
V2(4,3)
2
A(2,1)
x
2 4 6 8
Fig. 5.5.6

Solution : Using the cross product for V1 we get,

(xV – xA) (yB – yA) – (yV – yA) (xB – xA)


= (1 – 2) (5 – 1) – (3 – 1) (2 – 2)
= (– 1) (4) – 0
= –4

The result of the cross product for V1 is negative hence V1 is on the left side of the
window boundary.
Using the cross product for V2 we get, (4 – 2) (5 – 1) – (3 – 1) (2 – 2)
= (2) (4) – 0
= 8

The result of the cross product for V2 is positive hence V1 is on the right side of the
window boundary.
The second key process in Sutherland - Hodgeman polygon clipping algorithm is to
determine the intersection of the polygon edge and the clipping plane. Any of the line
intersection (clipping) techniques discussed in the previous sections such as Cyrus-Beck
or mid point subdivision can be used for this purpose.

Sutherland-Hodgeman Polygon Clipping Algorithm


1. Read coordinates of all vertices of the polygon.
2. Read coordinates of the clipping window
3. Consider the left edge of the window
4. Compare the vertices of each edge of the polygon, individually with the clipping
plane

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 28 Windowing and Clipping

5. Save the resulting intersections and vertices in the new list of vertices according to
four possible relationships between the edge and the clipping boundary discussed
earlier.
6. Repeat the steps 4 and 5 for remaining edges of the clipping window. Each time
the resultant list of vertices is successively passed to process the next edge of the
clipping window.
7. Stop.
Example 5.5.2 For a polygon and clipping window shown in Fig. 5.5.7 give the list of vertices
after each boundary clipping.
V4
Clipping window

V3

V5

V2

V1

Fig. 5.5.7
V4
Solution : Original polygon vertices are V1, V2, V3,
V'3 V'4
V4, V5. After clipping each boundary the new
vertices are given in Fig. 5.5.7 (a).
After left clipping : V1, V 1¢ , V 2¢ , V3, V4, V5
After right clipping : V1, V 1¢ , V 2¢ , V3, V4, V5 V3
After top clipping : V1, V 1¢ , V 2¢ , V3, V 3¢ , V 4¢ , V5 V'2
V5
After bottom clipping : V 2¢¢, V 2¢ , V3, V 3¢ , V 4¢ , V5,
V 5¢ V2
V'5
V''2
· The Sutherland-Hodgeman polygon clipping
V'1
algorithm clips convex polygons correctly,
V1
but in case of concave polygons, clipped
polygon may be displayed with extraneous Fig. 5.5.7(a)
lines, as shown in Fig. 5.5.8.
Computer Graphics 5 - 29 Windowing and Clipping

· The problem of extraneous lines


for concave polygons in
Sutherland-Hodgeman polygon
clipping algorithm can be solved
by separating concave polygon
into two or more convex (a) (b)
polygons and processing each
convex polygon separately. Fig. 5.5.8 Clipping the concave polygon in (a) with
the Sutherland-Hodgeman algorithm produces the
two connected areas in (b)
5.5.2 Weiler-Atherton Algorithm

The clipping algorithms previously discussed


C2 C3
require a convex polygon. In context of many
applications, e.g. hidden surface removal, the ability to V3
I1
clip to concave polygon is required. A powerful but V2
somewhat more complex clipping algorithm developed V4
I2
by Weiler and Atherton meets this requirement. This V5 I3
algorithm defines the polygon to be clipped as a
V1 V6
subject polygon and the clipping region is the clip I4
polygon. C1 C4

The algorithm describes both the subject and the Fig. 5.5.9
clip polygon by a circular list of vertices. The
boundaries of the subject polygon and the clip polygon may or may not intersect. If they
intersect, then the intersections occur in pairs. One of the intersections occurs when a
subject polygon edge enters the inside of
For subject polygon For clip polygon
the clip polygon and one when it leaves.
As shown in the Fig. 5.5.9, there are four
V1 C1
intersection vertices I1, I2 , I3 and I4. In
V2 I4
these intersections I1 and I3 are entering
Start I1 I3 Finish
intersections, and I2 and I3 are leaving
intersections. The clip polygon vertices are V 3 I2

marked as C1, C2, C3 and C4. V 4 I 1 Finish

In this algorithm two separate vertices I2 C2

lists are made one for clip polygon and one V5 C3


for subject polygon including intersection Start I3 C4
points. The Table 5.5.1 shows these two V6
lists for polygons shown in Fig. 5.5.10. I4
The algorithm starts at an entering V1
intersection (I1) and follows the subject
polygon vertex list in the downward Table 5.5.1 List of polygon vertices

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 30 Windowing and Clipping

direction (i.e. I1, V3, V4, I2). At the occurrence of leaving intersection the algorithm
follows the clip polygon vertex list from the leaving intersection vertex in the downward
direction (i.e. I2, I1). At the occurrence of the entering intersection the algorithm follows
the subject polygon vertex list from the entering intersection vertex. This process is
repeated until we get the starting vertex. This process we have to repeat for all
remaining entering intersections which are not included in the previous traversing of
vertex list. In our example, entering vertex I3 was not included in the first traversing of
vertex list,. Therefore, we have to go for another vertex traversal from vertex I3.
The above two vertex traversals gives two clipped inside polygons. There are :
I1, V3, V4, I2, I1 and I3, V6, I4, I3

5.5.3 Liang-Barsky Polygon Clipping

This section gives a brief outline of the


Liang-Barsky polygon clipping algorithm.
2 3 2
This algorithm is optimized for rectangular
clipping window but is extensible to
arbitrary convex windows. The algorithm Window
3 3
is based on concepts from their two and 4
three dimensional line clipping algorithm.
The algorithm is claimed to be twice as
fast as the Sutherland-Hodgeman clipping 2 3 2
algorithm.
The Liang-Barsky algorithm assumes Fig. 5.5.10 Nine regions of clipping plane
that the clipping region and polygon are
coplanar. Each edge of the clipping window divides the plane into two half planes. The
side of the half plane containing the window is called the inside or the visible half. The
other half plane is the outside or invisible half plane. The four window edges divide
the clipping plane into nine regions, as shown in the Fig. 5.5.10.
Entering and Leaving Vertices
Assume that [Pi , Pi + 1 ] is an edge of a polygon. If it is not horizontal or vertical then
the infinite line, L i , containing the polygon edge intersects each of the four window
edges. In fact, as shown in the Fig. 5.5.11, such an infinite line always starts in a corner
region, labelled 2, and ends in the diagonally opposite corner region, whether it
intersects the window or not. For example, line A and C intersect the window while line
B does not. As shown in the Fig. 5.5.11, each line has four window edge intersections.
The first intersection of the infinite line, L i , with a window edge represents a transition
from an invisible (outside) to a visible (inside) side of the window edge. Such an
intersection is called an entering intersection.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 31 Windowing and Clipping

Left edge Right edge


Outside Inside Inside Outside
Pi+1
Last Outside
intersection
Top edge

Entering Leaving
intersection Inside
intersection
Inside

Bottom edge

Pi+1 Outside
Pi
First Pi+1
A intersection
intermediate intersections
Pi
C
Pi
B
Fig. 5.5.11 Entering and leaving intersections

The last intersection of the infinite line, L i , with a window edge represents a
transition from a visible to an invisible side of the window edge and is called a leaving
intersection. The two intermediate intersections can occur in either entering-leaving
order as illustrated by the lines A and C, or in leaving-entering order, as illustrated by
the line B in the Fig. 5.5.11.
If the intermediate intersections occur in entering leaving order, then the infinite line,
L i , intersects the window. However, the visibility of any part or all of the polygon edge,
Pi Pi + 1 , is depend on the location Pi+1
of Pi Pi + 1 along L i , as shown by
the polygon edges on lines A and Li+1
C in Fig. 5.5.11.
C Window
If the intermediate intersections
Turning
occur in leaving-entering order (as vertex
Pi
in case of line B), no part of the
infinite line, L i , is visible in the
window.
Turning Vertices A
Pi+2
If a subsequent polygon edge
reenters the window through a
different window edge, then it is Li+2
necessary to include one or more of
the window corners in the output B

Fig. 5.5.12 Turning vertices


®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 32 Windowing and Clipping

polygon. This is illustrated in Fig. 5.5.12. Liang and Barsky call such a window corner a
turning vertex.
A necessary, but not sufficient, condition that a turning vertex exist if an entering
intersection occurs on Pi+ 1 Pi + 2 and outside the window as shown in the Fig. 5.5.12.
When this condition occurs, the Liang-Barsky polygon clipping algorithm adds the
turning vertex closest to the entering vertex to the output polygon. This is a necessary
but not sufficient condition, because, the entire polygon could lie outside the window.
Thus, extraneous turning vertices can be added to the output polygon. This is the main
drawback of the Liang-Barsky algorithm.
However, it is important to note that unless the first entering intersection is a
window corner and hence coincident with the second entering intersection, and thus that
the first entering intersection cannot be in the window, yields a sufficient condition for
including a turning vertex in the output polygon.
It is possible to calculate the actual intersections of the Line L i and the window
edges, as well as the ordering of the intersections by using the parametric equation of
the polygon edge.
P(t) = Pi + (Pi+ 1 - Pi ) t
where 0 < t £ 1 represents the polygon edge except for the
initial point, Pi , and
-¥ < t < ¥ represents the infinite line containing the
polygon edge

Development of the Algorithm


Liang-Barsky algorithm assumes a regular clipping region and hence it can be
developed by using the components of the parametric line, i.e.

x = xi + ( xi + 1 - x i ) t = x i + Dxt

y = y i +( y i+ 1 - y i ) t = y i + Dyt
For a regular clipping region, the edges are given by,
x left £ x £ x right
y bottom £ y £ y top

Using subscripts e and l to denote entering and leaving intersections, the parametric
values for the four intersections with the window edges are

t xe = (x e - x i ) Dx i Dx i ¹ 0

t xl = (x l - x i ) Dx i Dx i ¹ 0

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 33 Windowing and Clipping

t ye = (y e - y i ) Dy i Dy i ¹ 0
t yl = (y l - y i ) Dy i Dy i ¹ 0
ì x left Dx i > 0
where xe = í
î x right Dx i £ 0

ìx Dx i > 0
x l = í right
î x left Dx i £ 0

ì y bottom Dy i > 0
ye = í
î y top Dy i £ 0

ìy Dy i > 0
y l = í top
î y bottom Dy i £ 0

The first and second entering and leaving intersections are given by
t e1 = min (t xe , t ye )
t e2 = max (t xe , t ye )
t l1 = min (t xl , t yl )
t l2 = max (t xl , t yl )

Let us observe the conditions that decides whether to contribute, not to contribute,
partly contribute, or to add turning vertex to the output polygon.

Conditions for no contribution to the output polygon


1. Termination of edge before the
first entering intersection Pi+1
(1 < t e1 ), see line A in Fig. 5.5.13.
Pi
2. Starting of edge after the first
entering intersection (0 ³ t e1 )
and ending of edge before the
B Window
second entering intersection
(1 < t e2 ), see line B in Fig. 5.5.13.
Pi+1
3. Starting of edge after the second Pi
entering intersection (0 ³ t e2 )
Pi
and after the first leaving A
Pi+1
intersection (0 ³ t l1 ), see line C
C
in Fig. 5.5.13. Fig. 5.5.13 Conditions for no contribution
of the output polygon
Conditions for contribution to the
output polygon

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 34 Windowing and Clipping

4. If the second entering


intersection occurs
before the first leaving
intersection (t e2 £ t l1 ), Pi Window
and Pi lies on L i before P
Pi +1 i +1
D
the first leaving Pi
Pi +1 F
E
intersection (0 < t l1 ), Pi
and Pi + 1 lies on L i
after the second entering
intersection (1 ³ t e2 ),
the edge is either
partially or wholly
contained within the Fig. 5.5.14 Condition for contribution to the output polygon
window and hence
partially or wholly visible. See lines D, E and F in Fig. 5.5.14.
If the edge is partially visible, we can obtain the intersection point and contribution
to the output polygon using parametric equation.

Conditions for addition of a turning vertex to the output polygon


5. If the first entering
intersection lies on Pi Pi + 1
(0 < t e1 £ 1), then the Pi+1
Pi
turning vertex is (x e , y e ), Pi
H Pi+1
G
e.g. line G in Fig. 5.5.15.
6. If the second entering
intersection lies on Pi Pi + 1 Window
(0 < t e2 £ 1), and is
outside the window Pi+1

(t l1 < t e2 ), then the turning I


vertex is either (x e , y l )
when the second entering Pi
intersection lies on a
vertical edge of the
window (t xe > t ye ), e.g. Fig. 5.5.15 Conditions for addition of a turning vertex
line H in the Fig. 5.5.15, or
(x l , y e ) when the second entering intersection lies on a horizontal edge of the
window (t ye > t xe ), e.g. line I in Fig. 5.5.15.
Computer Graphics 5 - 35 Windowing and Clipping

The Table 5.5.2 summarizes the six output conditions for polygon edges.

Sr.No. Condition Contribution

1. 1 < te1 No

2. 0 ³ te1 and 1 < te2 No

3. 0 ³ te2 and 0 ³ t l1 No

4. te2 £ t l1 and 0 £ t l1 and 1 ³ te2 Visible segment

5. 0 £ te1 £ 1 Turning vertex (xe , ye )

6. 0 < te2 £ 1 and t l1 < te2 Turning vertex


(xe , yl ) for t xe > t ye
(xl , ye ) for t ye > t xe
Table 5.5.2 Output conditions for polygon edges
The conditions listed in the Table 5.5.2 are independent of the others except the
condition 5. If condition 5 generates turning vertex, condition 4 may generate a visible
segment or condition 6 may generate an another turning vertex. If the polygon edge
penetrates the window, a visible segment may be generated by condition 4; or if the
polygon edge is completely outside the window, another turning vertex may be
generated by condition 6. Therefore, in the algorithm we must evaluate the condition 5
prior to evolution of conditions 4 and 6.

Horizontal and vertical edges


Upto this point, we have ignored the horizontal and vertical polygon edges. These
polygon edges are easily detected by looking for Dy or Dx = 0 respectively. For the
horizontal line, Dy = 0 and t ye ® - ¥ and t yl ® + ¥. Thus, the horizontal line is
characterized by
- ¥ £ t xe £ t xl £ + ¥
Similarly the vertical line is characterized by
- ¥ £ t ye £ t yl £ + ¥

Algorithm
For each polygon edge P(i) P(i + 1)
· Determine the direction of the edge and find whether it is diagonal, vertical or
horizontal.
· Determine the order of the edge-window intersections.
· Determine the t-values of the entering edge-window intersections and the t-values
for the first leaving intersection.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 36 Windowing and Clipping

· Analyze the edge.


if 1 >= t_enter_1 then [condition 2 or 3 or 4 or 6 and not 1]
if 0 >= t_enter_1 then [condition 5]
call output (turning vertex);
end if
if 1 >= t_enter_2 then [condition 3 or 4 or 6 and not 1 or 2]
[determine second leaving t-value there is output]
if (0 < t_enter_2) or (0 < t_leave_1) then
[condition 4 or 6, not 3]
if t_enter_2 <= t_leave_1 then [condition 4 - visible segment]
call output (appropriate edge intersection)
else [end condition 4 and begin condition 6-turning vertex]
call output (appropriate turning vertex)
end if [end condition 6]
end if [end condition 4 or 6]
end if [end condition 3 or 4 or 6]
end if [end condition 2 or 3 or 4 or 6]
next i end edge P[i]P[i + 1]

Example 5.5.3 Find clipping co-ordinates for a line p 1 p 2 using Liang-Barsky algorithm
where p 1 = (50, 25) and p 2 = (80, 50), against window with ( xw min , y w min ) = (20, 10)
and ( xw max , y w max ) = (70, 60).
Solution : x 1 = 50, y 1 = 25, x 2 = 80, y 2 = 50

x min = 20, y min = 10, x max = 70, y max = 60


p 1 = – (x 2 - x 1 ) = – (80 – 50) = – 30
p 2 = (x 2 - x 1 ) = (80 – 50) = 30
p 3 = – (y 2 - y 1 ) = – (50 – 25) = – 25
p 4 = (y 2 - y 1 ) = (50 – 25) = 25

q 1 = (x 1 - x min ) = (50 – 20) = 30


q 2 = (x max - x 1 ) = (70 – 50) = 20
q 3 = (y 1 - y min ) = (25 – 10) = 15
q 4 = (y max - y 1 ) = (60 – 25) = 35

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 37 Windowing and Clipping

q 1 p 1 = 30/–30 = – 1.000
q 2 p 2 = 20/30 = 0.667
q 3 p 3 = 15/–25 = – 0.600
q 4 p 4 = 35/25 = 1.400

t 1 = max (– 1.000, – 0.600) = 0.00 Since for these values p < 0


t 2 = min (0.667, 1.400) = 0.667 Since for these values p > 0

Note : Minimum value of t 1 = 0


xx 1 = x 1 + t 1 * dx = 50 + 0.00 ´ 30.00 = 50.00
yy 1 = y 1 + t 1 * dy = 25 + 0.00 ´ 25.00 = 25.00
xx 2 = x 1 + t 2 * dx = 50 + 0.67 ´ 30.00 = 70.00
yy 2 = y 1 + t 2 * dy = 25 + 0.67 ´ 25.00 = 41.67

90

80

70
(xwmax, ywmax)
60
p2 = (80, 50)
50

40 (xx2, yy2) = (70, 41.67)

30 p1 = (50, 25)

20 (xx1, yy1)

10
(xwmin, ywmin)
0
10 20 30 40 50 60 70 80 90

Fig. 5.5.16
Example 5.5.4 Find clipping co-ordinates for a line AB, where A = (5, 20) and B = (30, 30).
Co-ordinates of window are : ( xw min , y w min ) = (15, 15) and ( xw max , y w max ) = ( 45, 45).
Solve using Liang-Barsky line clipping algorithm.
Solution : x 1 = 5, y 1 = 20, x 2 = 30, y 2 = 30

x min = 15, y min = 15, x max = 45, y max = 45


®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 38 Windowing and Clipping

p 1 = – (x 2 - x 1 ) = – (30 – 5) = – 25
p 2 = (x 2 - x 1 ) = (30 – 5) = 25
p 3 = – (y 2 - y 1 ) = – (30 – 20) = – 10
p 4 = (y 2 - y 1 ) = (30 – 20) = 10

q 1 = (x 1 - x min ) = (5 – 15) = – 10
q 2 = (x max - x 1 ) = (45 – 5) = 40
q 3 = (y 1 - y min ) = (20 – 15) = 5
q 4 = (y max - y 1 ) = (45 – 20) = 25

q 1 p 1 = – 10/– 25 = 0.400
q 2 p 2 = 40/25 = 1.600
q 3 p 3 = 5/– 10 = – 0.500
q 4 p 4 = 25/10 = 2.500

t 1 = max (0.400, – 0.500) = 0.400 Since for these values p < 0


t 2 = min (1.600, 2.500) = 1.600 Since for these values p > 0

Note : Maximum value of t 2 = 1


xx 1 = x 1 + t 1 * dx = 5 + 0.40 ´ 25.00 = 15.00
yy 1 = y 1 + t 1 * dy = 20 + 0.40 ´ 10.00 = 24.00
xx 2 = x 1 + t 2 * dx = 5 + 1.00 ´ 25.00 = 30.00
yy 2 = y 1 + t 2 * dy = 20 + 1.00 ´ 10.00 = 30.00

(See Fig. 5.5.17 on next page)


Computer Graphics 5 - 39 Windowing and Clipping

50
(xwmax, ywmax)

40

B = (30, 30)
30
(xx2, yy2)

(xx1, yy1) = (15, 24)


20
A = (5, 20)

(xwmin, ywmin)
10

0
10 20 30 40 50

Fig. 5.5.17
Examples for Practice

Example 5.5.5 : Find clipping co-ordinates for a line AB, where A = (1, 3) and B = (5, 12).
Co-ordinates of window are : ( xw min , y w min ) = (2, 1) and
( xw max , y w max ) = (9, 10). Solve using Liang-Barsky line clipping
algorithm.

Example 5.5.6 : Find clipping co-ordinates for a line p 1 p 2 using Liang-Barsky algorithm
where p 1 = (90, 20) and p 2 = (20, 90), against window with
( xw min , y w min ) = (10, 10) and ( xw max , y w max ) = (60, 60).

Example 5.5.7 : Find clipping co-ordinates for a line p 1 p 2 using Liang-Barsky algorithm
where p 1 = (60, 20) and p 2 = (110, 40), against window with
( xw min , y w min ) = (30, 10) and ( xw max , y w max ) = (90, 50).

Example 5.5.8 : Find clipping co-ordinates for a line p 1 p 2 using Liang-Barsky algorithm
where p 1 = (5, –3) and p 2 = (3, 4), against window with
( xw min , y w min ) = (–1, 2) and ( xw max , y w max ) = (6, 5).

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 40 Windowing and Clipping

Review Questions

1. Can line clipping algorithm be used for polygon clipping ? Justify ? SPPU : Dec.-05, Marks 8
2. Describe Sutherland - Hodgeman polygon clipping algorithm with example.
SPPU : Dec.-06, 07, 08, 11, 14,17, 18, May-10, 14, Marks 8

3. What is polygon clipping ? SPPU : May-07, Marks 2

5.6 Generalized Clipping SPPU : Dec.-07,11,12, May-09

· We have seen that in Sutherland - Hodgeman polygon clipping algorithm we need


separate clipping routines, one for each boundary of the clipping window. But
these routines are almost identical. They differ only in their test for determining
whether a point is inside or outside the boundary.
· It is possible to generalize these routines so that they will be exactly identical and
information about the boundary is passed to the routines through their
parameters.
· Using recursive technique the generalized routine can be 'called' for each boundary
of the clipping window with a different boundary specified by its parameters.
· This form of algorithm allows us to have any number of boundaries to the
clipping window, thus the generalized algorithm with recursive technique can be
used to clip a polygon along an arbitrary convex clipping window.

Review Question

1. Explain the concept of generalized clipping with the help of suitable example.
SPPU : Dec.-07, 11, 12, May-09, Marks 8

5.7 Interior and Exterior Clipping SPPU : May-05,13, Dec.-05,12

· So far we have discussed only algorithms for clipping point, line and polygon to
the interior of a clipping region by eliminating every thing outside the clipping
region. However, it is also possible to clip a point, line or polygon to the exterior
of a clipping region, i.e., the point, portion of line and polygon which lie outside
the clipping region. This is referred to as exterior clipping.
· Exterior clipping is important in a multiwindow display environment, as shown in
Fig. 5.7.1.
· Fig. 5.7.1 shows the overlapping windows with window 1 and window 3 having
priority over window 2. The objects within the window are clipped to the interior
of that window.

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Computer Graphics 5 - 41 Windowing and Clipping

Window 2

Window 1 Window 3

Fig. 5.7.1 Clipping in multiwindow environment

· When other higher-priority windows such as window 1 and/or window 3 overlap


these objects, the objects are also clipped to the exterior of the overlapping
windows.

Review Question

1. What is interior and exterior clipping. SPPU : May-05, 13, Dec.-05, 12, Marks 4

qqq

®
TECHNICAL PUBLICATIONS - An up thrust for knowledge

You might also like