You are on page 1of 82

POLYGON & CLIPPING

ALGORITHM
UNIT: II

Graphics Programming
Contents

• Introduction to polygon
• Types: convex, concave and complex
• Representation of polygon Inside test
• Polygon filling algorithms – flood fill, seed fill
• Polygon filling algorithms – scan line fill and filling
with patterns
• Windowing and clipping: viewing transformations
• 2-D clipping: Cohen – Sutherland algorithm
• Polygon clipping: Sutherland Hodgeman algorithm
• Generalized clipping
• Discussed about lines, circle & solid objects.
• Now will extend to new graphics primitives i.e.
Polygon.
• What is polygon, its representation, inside test &
filling polygon.
Polygon

• Polygon is a figure having many sides.

• It is represented as a number of line segments


connected end to end to form a closed figure.

• The line segments which form the boundary of polygon


are called edges or sides of polygon & the end points of
the sides are called as vertices of the polygon.
Types of polygon

• Convex
• Concave
• Complex
Convex

• It is a polygon in which if we take any two points


which are surely inside the polygon & if we draw a
line joining these points & if all the points on that
line lies inside the polygon then such a polygon is
called as convex polygon.
• All of its angles are less than 180ᶿ.
• All the diagonals are internal.
Concave

• It is a polygon in which if we take any two points


which are surely inside the polygon & if we draw a
line joining these points & if all the points on that
line are not lying inside the polygon then such a
polygon is called as convex polygon.
• Atleast one angle measures more than 180ᶿ.
• Atleast one of the diagonal is outside the shape of
polygon.
Complex

• A polygon which is neighter convex or concave is


called Complex polygon.
• It includes any polygon which intersects itself or the
polygon which overlaps itself.
• In this type of polygon the vertices are counted at
the end of the edges & not at the intersection points
of the overlapping edges.
Polygon Representation

• The polygon can be represented by listing its n vertices in an


ordered list.

P = {(x1, y1), (x2, y2), ……., (xn, yn)}.

• The polygon can be displayed by drawing a line between


(x1,y1),and(x2,y2)

• Then a line between (x2,y2),and(x3,y3), and soon until the end


vertex.

• In order to close up the polygon, a line between (xn, yn) ,and


(x1,y1)must be drawn.
Display File

It contains the necessary information to construct the polygon.

The information is stored in form of the command such as,


move or line.

The structure of the display file is 3 arrays, one for op-code ie for
commands and other two to store the value of x and y.

The op-code command ‘1’ is used for move and ‘2’ is used to
draw line.

In op- code field if a number other than 1 or 2 is treated as the


numbers of sides of the polygon to be draw.
Op-Code x y
6 5 5
2 7 5 A:5,5 B:7,5
2 9 3
2 7 1
C: 9,3
2 5 1 F:3,3
2 3 3 E:5,1 D:7,1
2 5 5

Display File store the complete definition of the polygon to be drawn


Inside Test

• Why ?

• Want to fill in (color) only pixels inside a polygon

• What is “inside” of a polygon ?

• Methods
1. Even –odd method
2. Winding no. method
Even-odd Method
Winding Number Method
• Give directions to all the edges of the polygon.
• Draw a scan line from the point to be test towards the left most of x
direction.
• The sides which starts above the drawn line and end below the drawn
line is assigned the weight of +1.
• The sides which starts below the drawn line and ends above the drawn
line is assigned the weight of -1.
• Check the edge direction value fro which the scan line is passing & sum
up them.
• If the totalsum value is non-zero, then the point is to be tested is an
interior point otherwise exterior point.
Polygon Filling Algorithms

• Filling is the process of “ colouring in “ a fixed ara or


region.
• Region may be defined at pixel level or geometric
level.
• Filling the polygon means highlighting all the pixels
which lie inside the polygon with a color other than
the background color.
• When the regions are defined at pixel level then we
have Seed Fill algorithm.
• In case of geometric level we have Scan Line
algorithm.
Seed Fill Algorithms: Connectedness

 4-connected region: From a given pixel, the region


that you can get to by a series of 4 way moves (N, S,
E and W)
 8-connected region: From a given pixel, the region
that you can get to by a series of 8 way moves (N, S,
E, W, NE, NW, SE, and SW)

28
4-connected 8-connected
• Seed Fill algorithm is divided into-
1.Boundary/ Edge Fill.
2. Flood Fill
Boundary Fill Algorithm

• It is very simple.
• It needs one point which is surely inside the polygon.
• This point is called as “ Seed Point “ which is nothing but
a point from which we are starting the filling process.
• This is a recursive process.
• This algorithm checks to see if the seed pixel has a
boundary pixel color or not.
• If no, then fill that pixel with color of boundary & make a
recursive call to itself using each of its neighbouring pixel
as a new seed.
• If the pixel color is same as boundary pixel color then
return to its caller.
Boundary Fill Algorithm

void BoundaryFill(int x, int y, newcolor, edgecolor)


{
int current;
current = ReadPixel(x, y);
if(current != edgecolor && current != newcolor)
{
putpixel(x,y, newcolor);
BoundaryFill(x+1, y, newcolor, edgecolor);
BoundaryFill(x-1, y, newcolor, edgecolor);
BoundaryFill(x, y+1, newcolor, edgecolor);
BoundaryFill(x, y-1, newcolor, edgecolor);
}
} 31
Flood Fill Algorithm

• Used when an area defined with multiple color boundarie


s.

• Start at a point( Seed) inside a region.

• Replace a specified interior color (old color) with fill color.

• Fill the 4-connected or 8-connected region until all interio


r points being replaced

32
4-connected

void FloodFill(int x, int y, newcolor, oldColor)


{
if(getpixel(x, y) == oldColor)
{
putpixel (x,y, newcolor);
FloodFill(x+1, y, newcolor, oldColor);
FloodFill(x-1, y, newcolor, oldColor);
FloodFill(x, y+1, newcolor, oldColor);
FloodFill(x, y-1, newcolor, oldColor);
}
}

33
8-connected
void FloodFill(int x, int y, newcolor, oldColor)
{
if(getpixel(x, y) == oldColor)
{
putpixel (x,y, newcolor);
FloodFill (x+1, y, newcolor, oldColor);
FloodFill (x-1, y, newcolor, oldColor);
FloodFill (x, y+1, newcolor, oldColor);
FloodFill (x, y-1, newcolor, oldColor);
FloodFill (x+1, y+1, newcolor, oldColor);
FloodFill (x-1, y-1, newcolor, oldColor);
FloodFill (x-1, y+1, newcolor, oldColor);
FloodFill (x+1, y-1, newcolor, oldColor);

}
}
34
Problem with the Seed fill Algorithm

• Recursive seed-fill algorithm may not fill regions correctly


if some interior pixels are already displayed in the fill
color.

• This occurs because the algorithm checks next pixels


both for boundary color and for fill color.

• Encountering a pixel with the fill color can cause a recursive


branch to terminate, leaving other interior pixels unfilled.

• This procedure requires considerable stacking of neighboring


points, more efficient methods are generally employed.
Example

The boundary of a
polygon: (In practice, a
polygon is defined as a
list of vertices.)
Basic Scan-Line Fill Algorithm

For each scan line:

1.Find the intersections of the scan line


with all edges of the polygon.

2. Sort the intersections by increasing


  x-coordinate.

3. Fill in all pixels between pairs of


  intersections.
• This algorithm begins with the first scan line from the side which have Ymax and
proceeds line by line towards the last side that have Ymin.

• All the attributes (Ymax, Ymin, Xmax, Xmin and Slope) of a polygon sides are
required to store in a array.

• Array are gets sorted with reference to the Y values from Ymax to Ymin.

• For each scan line crossing a polygon , this algorithm locates the intersection
points with the polygon edges.

• These intersection points are then sorted from left to right and the
corresponding positions between each intersection pair are set to the specified
fill colour.

• Every time Y value is calculated by decrementing 1.

• Corresponding X value is calculated by considering the slope of the line.


• Slope(m)= y2-y1/ x2-x1
m = -1/ x2-x1
x2-x1= -1/m
x2= x1 -1/m

• Thus by knowing the slope of every edge , we can find the


intersection point by decrementing 1 every time from Ymax to
Ymin and calculating the corresponding X by X1-1/m.

• In this process at some stage value of Y will be less than the Y min
of that edge (under consideration), then discard that edge and
include the new edge in the list having next largest Ymax value.

• This process continued until the value of y is not reaching the


Ymin value of the polygon.
Polygon Filling with Pattern

1.Pattern is a small group of pixels with a fixed colour


combination used to fill the area in picture.

2. These pattern can be of any shape , however rectangular


and square pattern are used to fill the area.

3. Filling an area with the rectangular pattern is called tiling


and rectangular pattern are referred as tiling.

4. Pattern fill can be implemented by modifying the scan


line algorithms. So that a selected pattern can be
superimposed onto the scan line.
There are two methods of filling with pattern
1. Stamping.
2. True Filling.
In stamping we find the centre of each row and mark
the full pattern with that point as center. In this method
part of the pattern may be outside the boundary of the
polygon.

In true filling method only those pixels are filled with the
pattern which is compulsorily inside the polygon.
Viewing Transformation

Graphics Programming
• Windowing – the method of selecting & enlarging
the portions of a drawing.(defines what is to be
viewed )
• Clipping – eliminates the objects or portions of
objects which are not visible through window. ( what
to omit )
• Viewport – An area on a display device to which a
window is mapped. ( where it is to be displayed on
display device )
Viewing transformation
• Whenever we want to display any scene we are
considaring two models of that scene -
1.Object Model – A model which is stored in computer
with the actual scale.
Ex-may be in mm , cm , km etc.
The space where object model resides is called object
space.
2. Image Model – Image model is one which appears on
display device.
An image we want to draw on display device must be
measured in screen coordinates.
• Viewing Transformation is a 3 step process –
1. Translation – The object with its window is shifted or
translated until its lower left corner of the matches to
the window.
2. Scalling – We are converting the object into image &
window into viewport.
3. Translation- Another translation to move viewport to
its correct position on the screen.
1. Window Co-ordinate to Viewport Co-ordinate
Transformation
Viewport co-ordinate to Device Co-
ordinate
This Transformation can be given as
D= T.S.T-1

Where T=

S=

T-1=
After solving the equation
D=
X X

=
Example

• Suppose that there is a rectangle ABCD whose


coordinates are A(1,1) , B(4,1) , C(4,4) & D(1,4).And
the window coordinates are (2,2) , (5,2) , (5,5) &
(2,5). And the viewport location is (0.5,0) , (1,0) ,
(1,0.5) & (0.5,0.5).Derrive the viewing transformation
matrix.
Windowing & Clipping

Graphics Programming
Windowing
A picture is made up of a collection of objects
specified in world coordinates

World Coordinates
Windowing
Sometime we might interested in some part of the object to display. So
we imagine a box about the portion of the object and we only display
what is enclosed in the box, such box is called windowing.

Window
wymax

wymin

wxmin wxmax
World Coordinates
Windowing
Because drawing things to a display takes time we clip
everything outside the window

Window
wymax

wymin

wxmin wxmax
World Coordinates
Clipping
• The procedure that identifies the portion of a picture that are either inside or
outside of a specified region of space is called clipping.
• Clipping is the process which remove the line/point/portion
which are outside the window boundaries and display only the
portion which is inside the boundary.
• The region against which an object is to be clipped is called
clipping window. 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
Line Clipping
Examine the end-points of each line to see if
they are in the window or not
Situation Solution Example

Both end-points inside


Don’t clip
the window

One end-point inside


the window, one Must clip
outside

Both end-points
Don’t know!
outside the window
Sutherland- Hodgman Polygon Clipping
Algorithm
• A technique for clipping areas developed by
Sutherland & Hodgman
• In this method polygon is simply clipped by
comparing it against each boundary in turn.

Original Area Clip Left Clip Right Clip Top Clip Bottom
Sutherland-Hodgeman Clipping

These Four Scenario:

inside outside inside outside inside outside inside outside


p s
p
s

p s
p s I

Save p Save I Save nothing Save I


and P
Algorithms Steps
Cohen-Sutherland Clipping Algorithm
• An efficient line clipping algorithm

• The key advantage of the


algorithm is that it vastly reduces
the number of line intersections
that must be calculated
Dr. Ivan E. Sutherland
co-developed the Cohen-
Sutherland clipping
algorithm. Sutherland is
a graphics giant and
includes amongst his
Cohen is something of a mystery – can achievements the
anybody find out who he was? invention of the head
mounted display.
Cohen-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


4 3 2 1

above below right left


0000
0001 0010
Window
Region Code Legend

0101 0100 0110


1. If result of logical ANDing is 0 and both the end point
have the outcode 0000 then line is completely visible,
so no clipping.

2. If result of logical ANDing is non zero, ie same bit is set


for both the outcode then line is completely outside so
it must be clipped.

3. If result of logical ANDing is 0 but both the outcode is


not 0000 then in this case we need to find the point of
intersection on the window boundary.
Cohen- Sutherland Algorithm
Line Outcode Outcode Logical Anding Results
P5-P6 0000 0000 0000 Completely Visible
P3-P4 0001 1000 0000 Not sure
P11-P12 1010 0010 0010 Completely Invisible

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
Cohen-Sutherland: Labelling

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
Lines completely contained within the
window boundaries have region code [0000]
for both end-points so are not clipped e.g
p5-p6
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
Cohen- Sutherland Algorithm
Line Outcode Outcode Logical Anding Results
P5-P6 0000 0000 0000 Completely Visible
P3-P4 0001 1000 0000 Not sure
P11-P12 1010 0010 0010 Completely Invisible

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
Example
Intersection Point with the Clipping Boundary.
Consider a line segment with end-points(x1,y1)and(x2,y2) and
m is the slope of the line in question. Slope of a line can be
calculated as m=(y2-y1)/(x2-x1)
Cohen-Sutherland algorithm steps
Example: To find intersection Points

Question:
Use Cohen-Sutherland out-code 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).
Use Cohen-Sutherland out-code algorithm to clip two
lines P1(10,30)- P2(80,90) against a window A(20,20),
B(90,20), C(90,70), D(20,70).

Use Cohen-Sutherland out-code algorithm to clip two


lines P1(4,1)- P2(6,4) against a window A(3,2), B(7,2),
C(7,6), D(3,6).
Text Clipping

1. The simplest method for processing character string


relative to a window boundary is to use the all-or-
none string clipping strategies.

This procedure is implemented by considering a


boundary rectangle around the text pattern. The
boundary position of the rectangle then compared
to the window boundaries and then string is
rejected if there is any overlap. This is the fastest
method of text clipping.
2. An alternative approach is that discard only those
character that are not completely inside the window. In
this case the boundary limits of individual character are
compared to the window , any character that either
overlaps or outside the boundary is clipped.

3. Another method is to clip the component of individual


character. If an individual character overlaps a clip
window boundary , clip the part of a character that are
outside the window.
Exterior Clipping

1. Interior Clipping:
2. Exterior clipping:

If we want to display the part of picture which is outside


the boundary region is called the exterior clipping.

It is used in Multi windowing system. It is also required by


the applications that require overlapping picture.

We can use exterior clipping to provide a space for an


insert into a larger picture.

You might also like