0% found this document useful (0 votes)
50 views71 pages

CH 4

The document discusses algorithms for generating lines and geometrical shapes in computer graphics. It describes three common line drawing algorithms: incremental line, DDA line, and Bresenham's line algorithm. For each algorithm, it provides details on the mathematical principles, implementation, and advantages/disadvantages.

Uploaded by

Sam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views71 pages

CH 4

The document discusses algorithms for generating lines and geometrical shapes in computer graphics. It describes three common line drawing algorithms: incremental line, DDA line, and Bresenham's line algorithm. For each algorithm, it provides details on the mathematical principles, implementation, and advantages/disadvantages.

Uploaded by

Sam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 71

Chapter Four

Geometry and Line Generation


 Point and Lines
 Algorithms
 Circles, curves, Line Thickness
 Line styles, Polygons, Filling

Prepared by Desta L. Computer graphics


Points and Lines
 Point plotting is accomplished by
 converting a single coordinate position furnished by an application
program into appropriate operations for the output device in use.

 With a CRT monitor, for example, the electron beam is turned on to


illuminate the screen phosphor at the selected location.

Prepared by Desta L. Computer graphics


Line
 A line connects two points. It is a basic element in graphics.
 To draw a line, you need two points between which you can draw a line.
 In the following three algorithms, we refer the one point of line as X0, Y0
and the second point of line as X1, Y1.
 A line, or straight line, is an (infinitely) thin, (infinitely) long, straight
geometrical object, i.e. a curve that is long and straight.

Prepared by Desta L. Computer graphics


Cont. …
 Given two points, in Euclidean geometry, one can always find exactly one line
that passes through the two points; this line provides the shortest connection
between the points and is called a straight line.
 Three or more points that lie on the same line are called collinear.
 Two different lines can intersect in at most one point; whereas two different
planes can intersect in at most one line.
 This intuitive concept of a line can be formalized in various ways.
Prepared by Desta L. Computer graphics
Line Cont. …
 A line may have three forms with respect to slope i.e. it may have slope = 1 as
shown in following figure (a), or may have slope < 1 as shown in figure (b) or it
may have a slope > 1 as shown in figure (c).
 Now if a line has slope = 1 it is very easy to draw the line by simply starting from
one point and go on incrementing the x and y coordinates till they reach the
second point.
 So that is a simple case but if slope < 1 or is > 1 then there will be some problem.

Prepared by Desta L. Computer graphics


Cont. ….

There are three line drawing techniques such as:


 Incremental line algorithm
 DDA line algorithm
 Bresenham line algorithm

Prepared by Desta L. Computer graphics


Incremental Line Algorithm
 This algorithm uses simple line equation y = m x + b
Where m = dy / dx, and b = y – m x
 Now check if |m| < 1 then starting at the first point, simply increment x
by 1 (unit increment) till it reaches ending point;
whereas calculate y point by the equation for each x and
conversely if |m|>1 then increment y by 1 till it reaches ending point;
whereas calculate x point corresponding to each y, by the equation.
Prepared by Desta L. Computer graphics
 Now before moving ahead let us discuss why these two cases are tested.
 First if |m| is less than 1 then it means that for every subsequent pixel
on the line there will be unit increment in x direction and there will be
less than 1 increment in y direction and vice versa for slope greater than
1.
Let us clarify this with the help of an example:

Prepared by Desta L. Computer graphics


Example-1: Suppose a line has two points p1 (10, 10) and p2
(20, 18)

 Now difference between y coordinates that is dy = y2 – y1 = 18 – 10 = 8


 Whereas difference between x coordinates is dx = x2 – x1 = 20 – 10 =
10
 This means that there will be 10 pixels on the line in which for x-axis
there will be distance of 1 between each pixel and for y-axis the distance
will be 0.8.

Prepared by Desta L. Computer graphics


Example-2: Consider case of another line with points p1 (10, 10)
and p2 (16, 20)

 Now the difference between y coordinates that is dy = y2 – y1 = 20 – 10 = 10


 Whereas difference between x coordinates is dx = x2 – x1 = 16 – 10 = 6
 This means that there will be 10 pixels on the line in which for x-axis there will be
distance of 0.6 between each pixel and for y-axis the distance will be 1.
 Now having discussed this concept at length let us now understand the algorithm
to draw a line using above technique which is called as incremental line algorithm:

Prepared by Desta L. Computer graphics


Incremental_Line (Point p1, Point p2)
dx = p2.x – p1.x drawPixel (x, y)
dy = p2.y – p1.y x = x + 1
m = dy / dx y = m * x + b
x = p1.x else
y = p1.y for counter = p1.y
b = y – m * x to p2.y
if |m| < 1 drawPixel (x, y)
for counter = p1.x y = y + 1
to p2.x x = (y – b) / m

Prepared by Desta L. Computer graphics


DDA Algorithm (Digital Differential Analyzer)
The DDA algorithm has very simple technique.

Find the difference of dx and dy between x coordinates and y coordinates


respectively by ending points of a line.

If |dx| is greater than |dy|, then |dx| will be step and otherwise |dy| will be step.

if |dx|>|dy| then
step = |dx|
else
step = |dy|
Prepared by Desta L. Computer graphics
Now it is very simple to say that step is the total number of pixel

required for a line.


Next step is to divide dx and dy by step to get xIncrement and

yIncrement respectively that is the increment required in each step to


find next pixel value.
 xIncrement = dx/step
 yIncrement = dy/step

Next a loop is required that will run step times. In the loop drawPixel

and addPrepared
xIncrement
by Desta L.
in x1 and yIncrement in y1.
DDA_Line (Point p1, Point p2)
dx = p2.x – p1. x yIncrement = dy/step
dy = p2.y – p1. y for counter = 1 to step
x1=p1.x drawPixel (x1, y1)
y1=p1.y x1 = x1 + xIncrement
if |dx|>|dy| then y1 = y1 + yIncrement
step = |dx|
else
step = |dy|
xIncrement = dx/step

Prepared by Desta L. Computer graphics


DDA Line Drawing Algorithms

 Advantage
 Does not calculate coordinates based on the complete equation (uses
offset method)
 Disadvantage
 Round-off errors are accumulated, thus line diverges more and
more from straight line
 Round-off operations take time
 Perform integer arithmetic by storing float as integers in numerator and
denominator and performing integer artithmetic.

10/10/2006 TCSS458A Isabelle Bichindaritz


Bresenhams Line Algorithm
 The Bresenham algorithm is another incremental scan conversion algorithm.
 The big advantage of this algorithm is that, it uses only integer calculations.
 Moving across the x axis in unit intervals and at each step choose between two
different y coordinates.
 Bresenham's algorithm finds the closest integer coordinates to the actual line, using
only integer math.
 Assuming that the slope is positive and less than 1, moving 1 step in the x direction,
y either stays the same, or increases by 1.
Prepared by Desta L.
Bresenham’s line drawing
 Efficient line drawing algorithm using only incremental
integer calculations
 Can be adapted to draw circles and other curves
Principle
 Vertical axes show scan line positions
 Horizontal axes show pixel columns
 At each step, determine the best next pixel based on the
sign of an integer parameter whose value is proportional
to the difference between the vertical separations of the
two pixel positions from the actual line.
Prepared by Desta L.
 A decision function is required to resolve this choice.
 If the current point is (xi, yi), the next point can be either (xi+1, yi) or (xi+1, yi+1).
 The actual position on the line is (xi+1, m(xi+1) +c). Calculating the distance
between the true point, and the two alternative pixel positions available gives:
d1 = y-yi
= m*(x+1) +b-yi
d2=yi+1-y
= yi+1–m(xi+1)-b

Prepared by Desta L. Computer graphics


 Bresenham’s line drawing algorithm (positive slope less than 1)
dlower = y – yk
dupper = (yk + 1) – y
dlower – dupper = 2m(xk+1) – 2yk + 2b –1
decision parameter: pk= Δx (dlower – dupper )
pk = 2Δy xk- 2Δx yk + c
sign of pk is the same as sign of dlower – dupper
 If the pixel at y is closer to the line path than the pixel at y +l (that is, dlower < dupper),
k k
then decision parameter pk is negative.
 In that case, we plot the lower pixel; otherwise, we plot the upper pixel.

 Coordinate changes along the line occur in unit steps in either the x or y directions.

Prepared by Desta L.
Let us define a decision function p, to determine which distance is closer to
the true point. By taking the difference between the distances, the decision
function will be positive if d1 is larger, and negative otherwise.
A positive scaling factor is added to ensure that no division is necessary
and only integer math need be used.
pi = dx (d1-d2)
pi = dx (2m * (xi+1) + 2b – 2yi-1 )
pi = 2 dy (xi+1) –2 dx yi + dx (2b-1 ) ------------------ (i)
pi = 2 dy xi – 2 dx yi + k ------------------ (ii)
where k=2 dy + dx (2b-1)
Prepared by Desta L.
Then we can calculate pi+1 in terms of pi without any xi , yi or k .
pi+1 = 2 dy xi+1 – 2 dx yi+1 + k
pi+1 = 2 dy (xi + 1) - 2 dx yi+1 + k since xi+1= xi + 1
pi+1 = 2 dy xi + 2 dy- 2 dx yi+1 + k ------------- (iii)
Now subtracting (ii) from (iii), we get
pi+1 - pi = 2 dy - 2 dx (yi+1 - yi )
pi+1 = pi + 2 dy - 2 dx (yi+1 - yi )
If the next point is: (xi+1,yi) then
d1<d2 => d1-d2<0
=> pi<0
=> pi+1= pi + 2 dy
If the next point is: (xi+1,yi+1) then
d1>d2 => d1-d2>0
=> pi>0
=> pi+1= pi + 2 dy - 2 dx
Prepared by Desta L. Computer graphics
 The pi is our decision variable, and calculated using integer arithmetic from
pre-computed constants and its previous value.
 Now a question is remaining how to calculate initial value of pi. For that use
equation (i) and put values (x1, y1)
pi = 2 dy (x1+1) – 2 dx yi + dx (2b-1 )
where b = y – m x implies that
pi = 2dyx1+2dy–2dxyi+dx(2(y1–mx1)-1)
pi = 2dyx1+2dy–2dxyi+2dxy1–2dyx1-dx
pi = 2dyx1+2dy–2dxyi+2dxy1–2dyx1-dx
 There are certain figures that will cancel each other shown in same different
colour
pi = 2 dy - dx
Prepared by Desta L.
Thus, Bresenham's line drawing algorithm is as follows:

dx = x2-x1 while (x < x2)


dy = y2-y1 x++;
p = 2dy-dx if (p < 0)
c1 = 2dy p = p + c1
c2 = 2(dy-dx) else
x = x1 p = p + c2
y = y1 y++
plot (x,y, colour) plot (x,y, colour)

Again, this algorithm can be easily generalized to other arrangements of the end points
of the line segment, and for different ranges of the slope of the line.

Ex: draw a line using bresenham’s line drawing algorithm b/n P1(5,3) and P2(10,6).

Prepared by Desta L. Computer graphics


Improving Performance of Line Drawing
 Several techniques can be used to improve the performance of line-drawing
procedures.
 These are important because line drawing is one of the fundamental primitives
used by most of the other rendering applications.
 An improvement in the speed of line-drawing will result in an overall improvement
of most graphical applications.
 Removing procedure calls using macros or inline code can produce improvements.
 Unrolling loops also may produce longer pieces of code, but these may run faster.
The use of separate x and y coordinates can be discarded in favour of direct frame
buffer addressing.

Prepared by Desta L. Computer graphics


 Most algorithms can be adapted to calculate only the initial frame buffer address
corresponding to the starting point and to replaced:
 X++ with Addr++
 Y++ with Addr+=XResolution

 Fixed point representation allows a method for performing calculations using only
integer arithmetic, but still obtaining the accuracy of floating-point values.
 In fixed point, the fraction part of a value is stored separately, in another integer:
M = Mint.Mfrac
Mint = Int(M)
Mfrac = Frac(M)× MaxInt

Prepared by Desta L.
 Addition in fixed point representation occurs by adding fractional and integer
components separately, and only transferring any carry-over from the fractional result
to the integer result. The sequence could be implemented using the following two
integer additions:
ADD Yfrac,Mfrac ;
ADC Yint,Mint
 Improved versions of these algorithms exist. For example, the following variations
exist on Bresenham's original algorithm:
 Symmetry (forward and backward simultaneously)
 Segmentation (divide into smaller identical segments - GCD(D x,D y) )
 Double step, triple step, n step
Prepared by Desta L.
Line style and Thickness
 setlinestyle() function-Sets the current line style and width or pattern
 setlinestyle sets the style for all lines drawn by line, lineto, rectangle, drawpoly.
 setlinestyle (linestyle, upattern, thickness); Line Styles
U pattern, thickness
 U pattern- User can define its own pattern.
 0 should be used if using predefined pattern,
other wise any integer number representing user
pattern
Thickness- Thickness of the line in pixels

Prepared by Desta L.
Introduction to Pixel
 A pixel is the smallest unit of a digital image or graphic that can be displayed and
represented on a digital display device.
 A pixel is the basic logical unit in digital graphics. Pixels are combined to form a
complete image, video, text or any visible thing on a computer display.
 A pixel is also known as a picture element.
 A pixel is represented by a dot or square on a computer monitor display screen.
Pixels are the basic building blocks of a digital image or display and are created
using geometric coordinates.
Prepared by Desta L. Computer graphics
 Depending on the graphics card and display monitor, the quantity, size and
color combination of pixels varies and is measured in terms of the display
resolution.
 For example, a computer with a display resolution of 1280 x 768 will produce a
maximum of 98,3040 pixels on a display screen.
 Each pixel has a unique logical address, a size of eight bits or more and, in
most high-end display devices, the ability to project millions of different colors.
 The pixel resolution spread also determines the quality of display; more pixels
per inch of monitor screen yields better image results.

Prepared by Desta L.
Setting a Pixel
 Initial Task:
 Turning on a pixel (loading the frame buffer/bit-map).
 Assume the simplest case, i.e., an 8-bit, non-interlaced graphics system.
 Then each byte in the frame buffer corresponds to a pixel in the output display

Prepared by Desta L. Computer graphics


To find the address of a particular pixel (X, Y) we use the following formula:
addr(X, Y) = addr(0,0) + Y rows * (Xm + 1) + X (all in bytes)
addr(X,Y) = the memory address of pixel (X,Y)
addr(0,0) = the memory address of the initial pixel (0,0)
Number of rows = number of raster lines.
Number of columns = number of pixels/raster line.
Example: For a system with 640 × 480-pixel resolution, find the address of
pixel X = 340, Y = 150?
Solution: addr(340, 150) = addr(0,0) + 150 * 640 (bytes/row) + 340
= base + 96,340 is the byte location
Graphics system usually have a command such as set_pixel (x, y) where x, y
are integers.
Prepared by Desta L. Computer graphics
Circle Drawing Techniques
 A circle is the set of points in a plane that are equidistant from a given point O.
 The distance r from the center is called the radius, and the point O is called the
center.
 Twice the radius is known as the diameter d = 2r.
 The angle a circle subtends from its center is a full angle, equal to 360° or 2π radians.
 A circle has the maximum possible area for a given perimeter, and the minimum
possible perimeter for a given area.
 The perimeter C of a circle is called the circumference, and is given by C = 2 π r
Prepared by Desta L. Computer graphics
 First of all, for circle drawing we need to know what the input will be.
 Well, the input will be one center point (x, y) and radius r.
 Therefore, using these two inputs there are a number of ways to draw a
circle.
 They involve understanding level very simple to complex and reversely
time complexity inefficient to efficient.
 We will see them one by one giving comparative study.
Prepared by Desta L. Computer graphics
Circle Drawing Using Cartesian coordinates

 This technique uses the equation for a circle on radius r centered at (0, 0) given as:

 an obvious choice is to plot

Prepared by Desta L. Computer graphics


 Obviously in most of the cases the circle is not centered at (0, 0), rather there is a
center point (Xc, yc); other than (0, 0). Therefore, the equation of the circle
having center at point (Xc, yc).

 this implies that,

Prepared by Desta L. Computer graphics


 Using above equation, a circle can easily be drawn.
 The value of x varies from xc - r to xc + r .
Whereas y will be calculated using above formula.
 Using this technique, we can write a simple algorithm:

Prepared by Desta L. Computer graphics


Circle Drawing using Polar Coordinates

 A better approach, to eliminate unequal spacing as shown in above figure is to


calculate points along with the circular boundary using polar coordinates r and θ
expressing the circle equation in parametric polar form yields the pair of equations:

Prepared by Desta L. Computer graphics


 Using above equation circle can be plotted by calculating x and y coordinates
as θ takes values from 0 to 360 degrees.
 The step size chosen for θ depends on the application and the display device.
 Larger angular separations along the circumference can be connected with
straight-line segments to approximate the circular path.
 For a more continuous boundary on a raster display, we can set the step size
at 1/r.
 This plots pixel positions that are approximately one unit apart.
 Now let us see how this technique can be sum up in algorithmic form.
Prepared by Desta L.
 Again, this is very simple technique and also solves problem of
unequal space but unfortunately this technique is still inefficient in
terms of calculations involves especially floating point calculations.

Prepared by Desta L. Computer graphics


 Calculations can be reduced by considering the symmetry of circles.
 The shape of circle is similar in each quadrant.
 We can generate the circle section in the second quadrant of the xy-plane by noting
that the two circle sections are symmetric with respect to the y axis and circle
sections in the third and fourth quadrants can be obtained from sections in the first
and second quadrants by considering symmetry about the x axis.
 We can take this one step further and note that there is also symmetry between
octants. Circle sections in adjacent octants within one quadrant are symmetric with
respect to the 450-line dividing the two octants.
 These symmetry conditions are illustrated in above figure. Therefore, above
algorithm can be optimized by using symmetric octants. Let’s see:
Prepared by Desta L.
Prepared by Desta L. Computer graphics
Midpoint Circle Algorithm
 As in the Bresenham line drawing algorithm we derive a
decision parameter that helps us to determine whether or not
to increment in the y coordinate against increment of x
coordinate or vice versa for slope > 1.
 Similarly, here we will try to derive decision parameter which
can give us closest pixel position.

Prepared by Desta L. Computer graphics


 Let us consider only the first octant of a circle of radius r centered on the
origin.
 We begin by plotting point (r, 0) and end when x < y.
 The decision at each step is whether to choose the pixel directly above
the current pixel or the pixel; which is above and to the left (8-way
stepping).
Prepared by Desta L. Computer graphics
 To apply the midpoint method, we define a circle function:

 Therefore, following relations can be observed:

Prepared by Desta L. Computer graphics


 The circle function tests given above are performed for the midpoints between
pixels near the circle path at each sampling step.
 Thus, the circle function is the decision parameter in the midpoint algorithm, and
we can set up incremental calculations for this function as we did in the line
algorithm.
 Figure below shows the midpoint between the two candidate pixels at sampling
position xk+1.
 Assuming we have just plotted the pixel at (xk, yk), we next need to determine
whether the pixel at position (xk+1, yk), we next need to determine whether the
pixel at position (xk+1, yk), or the one at position (xk+1, yk-1) is closer to the
circle. Prepared by Desta L.
Our decision parameter is the circle function evaluated at the midpoint
between these two pixels:

Prepared by Desta L. Computer graphics


 If pk<0, this midpoint is inside the circle and the pixel on scan line yk is closer to
the circle boundary.
 Otherwise, the mid position is outside or on the circle boundary, and we select the
pixel on scan-line yk -1.
 Successive decision parameters are obtained using incremental calculations.
 We obtain a recursive expression for the next decision parameter by evaluating
the circle function at sampling position.

Prepared by Desta L.
Subtracting (1) from (2), we get

 Where yk+1 is either yk or yk-1, depending on the sign of Pk.


 Therefore, if Pk < 0 or negative then yk + 1 will be yk and the formula to calculate
Pk+1 will be:

 Otherwise, if Pk > 0 or positive then yk+1 will be yk-1 and the formula to calculate
Pk+1 will be:
Prepared by Desta L. Computer graphics
 Now a similar case that we observe in line algorithm is that how would be starting
Pk be evaluated.
 For this at the start pixel position will be (0, r).
 Therefore, putting this value in equation, we get
 If radius r is specified as an integer, we can simply round p0 to:
 Since all increments are integer. Finally sum up all in the algorithm:
Prepared by Desta L. Computer graphics
 Now let us consider an example to calculate first octant of the circle using above
algorithm; while one quarter is displayed where you can observe that exact circle
is passing between the points calculated in a raster circle.
Prepared by Desta L. Computer graphics
Prepared by Desta L. Computer graphics
Filling Primitives: Rectangles, Polygons & Circles

 Two part process


 Which pixels to fill?
 What values to fill them with?
 Idea: Coherence
 Spatial: pixels are the
same from pixel-to-pixel
and scan-line to scan line;
 Span: all pixels on a span get the same value
 Scan-line: consecutive scan lines are the same
 Edge: pixels are the same along edges
Prepared by Desta L. Computer graphics
Scan Filling Primitives: Rectangles
 Easy algorithm
 Fill from xmin to xmax
Fill from ymin to ymax
 Issues
 What if two adjacent
rectangles share an edge?
 Color the boundary pixels twice?
 Rules:
 Color only interior pixels
 Color left and bottom edges
Prepared by Desta L. Computer graphics
Scan Filling Primitives: Polygons
 Observe:
 FA, DC intersections are
integer
 FE, ED intersections are not
integer
 For each scan line, how to
figure out which pixels are
inside the polygon?

Prepared by Desta L. Computer graphics 1994 Foley/VanDam/Finer/Huges/Phillips ICG


Polygon Filling Algorithm
 A polygon can be defined as a shape that is formed by line segments that are
placed end to end, creating a continuous closed path. Polygons can be divided into
three basic types such as convex, concave, and complex.
 Convex: Convex polygons are the simplest type of polygon to fill.

Prepared by Desta L. Computer graphics


 Concave: Concave polygons are a superset of convex polygons, having fewer
restrictions than convex polygons.
 The line connecting any two points that lie inside the polygon may intersect more
than two edges of the polygon.
 Thus, more than two edges may intersect any scan line that passes through the
polygon.
 The polygon edges may also touch each other, but they may not cross one another.

Prepared by Desta L.
 Complex: Complex polygons are just what their name suggests: complex. Complex
polygons are basically concave polygons that may have self-intersecting edges.
 The complexity arises from distinguishing which side is inside the polygon when filling it.

Prepared by Desta L. Computer graphics


Difference between Filled and Unfilled Polygon
 When an unfilled polygon is rendered, only the points on the perimeter of the
polygon are drawn. Examples of unfilled polygons are shown below:
 However, when a polygon is filled, the interior of the polygon must be considered.
 All of the pixels within the boundaries of the polygon must be set to the specified
color or pattern. Here, we deal only with solid colors. The following figure shows
the difference between filled and unfilled polygons.

 In order to determine which pixels are inside the polygon, the odd-parity rule is
used within the scan-line polygon fill algorithm.
Prepared by Desta L.
Parity
Parity is a concept used to determine which pixels lie within a polygon, i.e. which
pixels should be filled for a given polygon.
 The Underlying Principle: Conceptually, the odd parity test entails drawing a
line segment from any point that lies outside the polygon to a point P, which we
wish to determine whether it is inside or outside of the polygon.
 Count the number of edges that the line crosses.
 If the number of polygon edges crossed is odd, then P lies within the polygon.
 Similarly, if the number of edges is even, then P lies outside of the polygon.
Prepared by Desta L. Computer graphics
Scan Line Algorithm
 In order to fill a polygon, we do not want to have to determine the type of polygon
that we are filling.
 The easiest way to avoid this situation is to use an algorithm that works for all three
types of polygons.
 Since both convex and concave polygons are subsets of the complex type, using an
algorithm that will work for complex polygon filling should be sufficient for all three
types.
 The scan-line polygon fill algorithm, which employs the odd/even parity concept
works for complex polygon filling.
Prepared by Desta L.
 Reminder: The basic concept of the scan-line algorithm is to draw points from
edges of odd parity to even parity on each scan-line.
 A scan-line is a line of constant y value, i.e., y = c, where c lies within our
drawing region, e.g., the window on our computer screen.
 The following steps show how this algorithm works.
 Step 1 − Find out the Ymin and Ymax from the given polygon.

Prepared by Desta L.
Step 2 − Scanline intersects with each edge of the polygon from
Ymin to Ymax. Name each intersection point of the polygon. As per
the figure shown above, they are named as p0, p1, p2, p3.
Step 3 − Sort the intersection point in the increasing order of X
coordinate i.e. (p0, p1), (p1, p2), and (p2, p3).
Step 4 − Fill all those pair of coordinates that are inside polygons
and ignore the alternate pairs.
Prepared by Desta L. Computer graphics
Flood Fill Algorithm
 Sometimes we come across an object where we want to fill the area and its
boundary with different colors.
 We can paint such objects with a specified interior color instead of searching for
particular boundary color as in boundary filling algorithm.
 Instead of relying on the boundary of the object, it relies on the fill color.
 In other words, it replaces the interior color of the object with the fill color.
 When no more pixels of the original interior color exist, the algorithm is completed
Prepared by Desta L. Computer graphics
Boundary Fill Algorithm
 The boundary fill algorithm works as its name.
 This algorithm picks a point inside an object and starts to fill until it hits the boundary
of the object.
 The color of the boundary and the color that we fill should be different for this
algorithm to work.
 In this algorithm, we assume that color of the boundary is same for the entire object.
 The boundary fill algorithm can be implemented by 4-connected pixels or 8-connected
pixels. Prepared by Desta L. Computer graphics
The 4-Connected Polygon
 In this technique 4-connected pixels are used as shown in the figure.
 We are putting the pixels above, below, to the right, and to the left side of the
current pixels and this process will continue until we find a boundary with different
color.
Algorithm
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol.
Step 2 − Define the boundary values of the polygon.
Prepared by Desta L. Computer graphics
Step 3 − Check if the current seed point is of default color, then repeat the steps 4
and 5 till the boundary pixels reached.
If getpixel(x, y) = dcol then repeat step 4 and 5
Step 4 − Change the default color with the fill color at the seed point.
setPixel(seedx, seedy, fcol)
Step 5 − Recursively follow the procedure with four neighborhood points.
FloodFill (seedx – 1, seedy, fcol, dcol)
FloodFill (seedx + 1, seedy, fcol, dcol)
FloodFill (seedx, seedy - 1, fcol, dcol)
FloodFill (seedx, seedy + 1, fcol, dcol)
Step 6 − Exit

Prepared by Desta L.
 There is a problem with this technique.
 Consider the case as shown below where we tried to fill the entire region.
 Here, the image is filled only partially. In such cases, 4-connected pixels
technique cannot be used.

Prepared by Desta L.
The 8-Connected Polygon

 In this technique 8-connected pixels are used as shown in the figure.


 We are putting pixels above, below, right and left side of the current pixels as we
were doing in 4-connected technique.
 In addition to this, we are also putting pixels in diagonals so that entire area of the
current pixel is covered.
 This process will continue until we find a boundary with different color.
Prepared by Desta L. Computer graphics
Algorithm
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol.

Step 2 − Define the boundary values of the polygon.

Step 3 − Check if the current seed point is of default color then repeat the steps 4 and 5 till the
boundary pixels reached.

If getpixel(x,y) = dcol then repeat step 4 and 5

Step 4 − Change the default color with the fill color at the seed point.

setPixel(seedx, seedy, fcol)

Step 5 − Recursively follow the procedure with four neighborhood points

Prepared by Desta L.
FloodFill (seedx – 1, seedy, fcol, dcol)

FloodFill (seedx + 1, seedy, fcol, dcol)

FloodFill (seedx, seedy - 1, fcol, dcol)

FloodFill (seedx, seedy + 1, fcol, dcol)

FloodFill (seedx – 1, seedy + 1, fcol, dcol)

FloodFill (seedx + 1, seedy + 1, fcol, dcol)

FloodFill (seedx + 1, seedy - 1, fcol, dcol)

FloodFill (seedx – 1, seedy - 1, fcol, dcol)

Step 6 − Exit

Prepared by Desta L.
!!!
OU
K Y
A N
T H
Prepared by Desta L. Computer graphics

You might also like