Professional Documents
Culture Documents
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
• Convex
• Concave
• Complex
Convex
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.
• Why ?
• 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
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
32
4-connected
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
The boundary of a
polygon: (In practice, a
polygon is defined as a
list of vertices.)
Basic Scan-Line Fill Algorithm
• 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.
• 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.
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
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
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
p s
p s I
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).
1. Interior Clipping:
2. Exterior clipping: