You are on page 1of 135

ARYA GROUP OF COLLEGES

By:
Er. Naveen Kumar Tiwari
Arya Group Of Colleges
UNIT-2 CONTENTS
2.1 Graphics Primitives
2.1.1 Point And Lines Primitives
2.1.2 Circles And Ellipses Primitives
2.2 Scan Conversion Algorithm For Primitives
2.2.1 DDA Algorithm
2.2.2 Bressman Algorithm
2.2.3 Mid-Point Circle Algorithm
2.2.4 Flood Fill And Boundary Fill Algorithm
2.3 Character Generation Algorithm
2.3.1 Line-Attributes
2.3.2 Area-Fill Attributes
2.3.3 Character Attributes
2.4 Aliasing
2.5 Anti-Aliasing

Back
 DEFINITION:
 A low-level object or operation from which higher-level, more complex objects and
operations can be constructed.
 In graphics, primitives are basic elements, such as lines, curves, and polygons,
which you can combine to create more complex graphical images. In
programming, primitives are the basic operations supported by the programming
language.
 Which of the following graphic primitives are considered as the basic building
blocks of computer graphics ?

 Points(Co-ordinate) and lines are the basic building blocks of computer graphics.

 Graphics primitives include:

 Lines, Circles, Arcs, Rectangles, etc.


 Character Fonts
 Imported Images
 Points also known as pixel.
 A pixel is a point of light. It is just one tiny dots on the raster displays.
 Though it has no structure , it is definitely a building block.
 The resolution of CRT is related to the dot size, the diameter of a single
dot.
 A resolution of 100 dots lines/inch implies a dot size of 0.01 inch.
 The ratio of the distance between the centers of two adjacent horizontal
pixels to that of the vertical ones is called the pixel ratio.
 Pixel ratio should be considered in line-generating algorithms.
 A geometrical point is a location in space.
 Points are defined by uniquely x and y co-ordinate.
 It is a process of representing graphics objects a
collection of pixels. The graphics objects are
continuous. The pixels used are discrete. Each pixel
can have either on or off state.
 The circuitry of the video display device of the
computer is capable of converting binary values (0,
1) into a pixel on and pixel off information. 0 is
represented by pixel off. 1 is represented using pixel
on. Using this ability graphics computer represent
picture having discrete dots.
 Any model of graphics can be reproduced with a
dense matrix of dots or points. Most human beings
think graphics objects as points, lines, circles,
ellipses. For generating graphical object, many
algorithms have been developed.
 Point
 Line
 Sector
 Arc
 Ellipse
 Rectangle
 Polygon
 Characters
 Filled Regions
 The term pixel is a short form of the picture element.
 It is also called a point or dot. It is the smallest
picture unit accepted by display devices.
 A picture is constructed from hundreds of such
pixels.
 Pixels are generated using commands. Lines, circle,
arcs, characters; curves are drawn with closely
spaced pixels. To display the digit or letter matrix of
pixels is used.
 The closer the dots or pixels are, the better will be
the quality of picture. Closer the dots are, crisper will
be the picture.
 Picture will not appear jagged and unclear if pixels
are closely spaced. So the quality of the picture is
directly proportional to the density of pixels on the
screen.
Scan Converting a Point
Each pixel on the graphics display does not represent a mathematical point. Instead, it
means a region which theoretically can contain an infinite number of points. Scan-
Converting a point involves illuminating the pixel that contains the point.

Example: Display coordinates points as shown in fig would both be represented by pixel
(2, 1). In general, a point p (x, y) is represented by the integer part of x & the integer part
of y that is pixels [(INT (x), INT (y).
 A straight line may be defined by two endpoints
& an equation. In fig the two endpoints are
described by (x1,y1) and (x2,y2). The equation of
the line is used to determine the x, y coordinates
of all the points that lie between these two
endpoints.
 Line should appear Straight: We must appropriate
the line by choosing addressable points close to
it. If we choose well, the line will appear straight,
if not, we shall produce crossed lines.
 Lines should terminate accurately: Unless lines
are plotted accurately, they may terminate at
the wrong place.
 Lines should have constant density: Line
density is proportional to the no. of dots
displayed divided by the length of the line.
To maintain constant density, dots should be
equally spaced.
 Line density should be independent of line
length and angle: This can be done by
computing an approximating line-length
estimate and to use a line-generation algorithm
that keeps line density constant to within the
accuracy of this estimate.
 Line should be drawn rapidly: This computation
should be performed by special-purpose
hardware.
 Direct use of line equation
 DDA (Digital Differential Analyzer)
 Bresenham's Algorithm
 It is the simplest form of conversion. First of all
scan P1 and P2 points. P1 has co-ordinates (x1',y1')
and (x2' y2' ).
Y= mx+b
SIMPLE LINE EQUATION

Then m = (y2-y1)/( x2-x1) and b =y-mx.

 If value of |m|≤1 for each integer value of x. But


do not consider

 If value of |m|>1 for each integer value of y. But


do not consider
 Solution: P1 (0,0) P7 (6,18)
x1=0 y1=0 x2=6 y2=18

 We know equation of line is


y =m x + b
y = 3x + b..............equation (1)
 put value of x from initial point in equation (1),
i.e., (0, 0) x =0, y=0
0=3x0+b
0 = b ⟹ b=0
 put b = 0 in equation (1)
y = 3x + 0
y = 3x
 Now calculate intermediate points
Let x = 1 ⟹ y = 3 x 1 ⟹ y = 3
Let x = 2 ⟹ y = 3 x 2 ⟹ y = 6
Let x = 3 ⟹ y = 3 x 3 ⟹ y = 9
Let x = 4 ⟹ y = 3 x 4 ⟹ y = 12
Let x = 5 ⟹ y = 3 x 5 ⟹ y = 15
Let x = 6 ⟹ y = 3 x 6 ⟹ y = 18
 So points are P1 (0,0)
P2 (1,3)
P3 (2,6)
P4 (3,9)
P5 (4,12)
P6 (5,15)
P7 (6,18)
 Step1: Start Algorithm
 Step2: Declare variables x1,x2,y1,y2,dx,dy,m,b,
 Step3: Enter values of x1,x2,y1,y2.
The (x1,y1) are co-ordinates of a starting point of the
line.
The (x2,y2) are co-ordinates of a ending point of the line.
 Step4: Calcúlate dx = x2- x1
 Step5: Calculate dy = y2-y1
 Step6: Calculate m = dy/dx
 Step7: Calculate b = y1-m* x1
 Step8: Set (x, y) equal to starting point, i.e., lowest point and xend
equal to largest value of x.
If dx < 0
then x = x2
y = y2
xend= x1
If dx > 0
then x = x1
y = y1
xend= x2
 Step9: Check whether the complete line has been
drawn if x=xend, stop
 Step10: Plot a point at current (x, y) coordinates
 Step11: Increment value of x, i.e., x = x+1
 Step12: Compute next value of y from equation y =
mx + b
 Step13: Go to Step9.
 DDA stands for Digital Differential
Analyzer. It is an incremental method of
scan conversion of line. In this method
calculation is performed at each step but
by using results of previous steps.
The original differential analyzer
was a physical machine
developed by Vinegar Bush at
MIT in the 1930’s in order to
solve ordinary differential
e q u a t i o n s .
More information here.
 Step1: Start Algorithm
 Step2: Declare x1,y1,x2,y2,dx,dy,x,y as
integer variables.
 Step3: Enter value of x1,y1,x2,y2.
 Step4: Calculate dx = x2-x1
 Step5: Calculate dy = y2-y1
 Step6: If ABS (dx) > ABS (dy)
Then step = abs (dx)
Else
 Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
 Step8: Set pixel (x, y)
 Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
 Step10: Repeat step 9 until x = x2
 Step11: End Algorithm
If a line is drawn from (2, 3) to (6, 15) with use
of DDA. How many points will needed to
generate such line?
P1 (2,3) P11 (6,15)
x1=2
y1=3
x2= 6
y2=15
dx = 6 - 2 = 4
dy = 15 - 3 = 12
m = dy/dx= 12/4 = 3
For calculating next value of x takes x = x
+ 1/m
Advantage:
 It is a faster method than method of using direct use of line
equation.
 This method does not use multiplication theorem.

 It allows us to detect the change in the value of x and y ,so


plotting of same point twice is not possible.
 This method gives overflow indication when a point is
repositioned.
 It is an easy method because each step involves just two
additions.
Disadvantage:
 It involves floating point additions rounding off is done.
Accumulations of round off error cause accumulation of error.
 Rounding off operations and floating point operations consumes
a lot of time.
 It is more suitable for generating line using the software. But it
is less suited for hardware implementation.
 This algorithm is used for scan converting a line. It
was developed by Bresenham.
 It is an efficient method because it involves only
integer addition, subtractions, and multiplication
operations.
 These operations can be performed very rapidly so
lines can be generated quickly.
 In this method, next pixel selected is that one who
has the least distance from true line.
Problem: - Consider the line from (5, 5) to (13, 9). Use
the Bresenham's algorithm to rasterize the line.
Solution :-
Evaluating steps 1 through 4 in the Bresenham’s
algorithm we have,
Given: -
step 1 :- x1 = 5 , y1 = 5 and x2 = 13 , y2 = 9 .
step 2 :- Δx = | 13 - 5| =8 Δy = | 9 - 5 | = 4
step 3 :- x = 5 , y = 5
step 4 :- e = 2 * Δy - Δx = 2 * 4 - 8 = 0 .
Tabulating the results of each iteration in the step 5
through 10.
Advantage:
 1. It involves only integer arithmetic, so it is simple.

 2. It avoids the generation of duplicate points.

 3. It can be implemented using hardware because it


does not use multiplication and division.
 4. It is faster as compared to DDA (Digital
Differential Analyzer) because it does not involve
floating point calculations like DDA Algorithm.
Disadvantage:
 1. This algorithm is meant for basic line drawing
only Initializing is not a part of Bresenham's line
algorithm. So to draw smooth lines, you should want
to look into a different algorithm.
DDA Algorithm Bresenham's Line Algorithm
1. DDA Algorithm use floating point, 1. Bresenham's Line Algorithm use
i.e., Real Arithmetic. fixed point, i.e., Integer Arithmetic

2. DDA Algorithms uses multiplication 2.Bresenham's Line Algorithm uses


& division its operation only subtraction and addition its
operation
3. DDA Algorithm is slowly than 3. Bresenham's Algorithm is faster
Bresenham's Line Algorithm in line than DDA Algorithm in line because it
drawing because it uses real involves only addition & subtraction
arithmetic (Floating Point operation) in its calculation and uses only
integer arithmetic.
4. DDA Algorithm is not accurate and 4. Bresenham's Line Algorithm is
efficient as Bresenham's Line more accurate and efficient at DDA
Algorithm. Algorithm.
5.DDA Algorithm can draw circle and 5. Bresenham's Line Algorithm can
curves but are not accurate as draw circle and curves with more
Bresenham's Line Algorithm accurate than DDA Algorithm.
 What is a circle?
 It is a set of points that are all at a given distance r
from center position (xc, yc).

 The distance relationship equation of a circle is


expressed by the Pythagorean theorem in Cartesian
coordinates as:
( x – x c ) 2 + ( y – yc ) 2 = r 2
 We can re-write the circle equation as:
y = yc ± ( r2 – ( x – xc)2 )0.5
 By substitution with x , xc and yc we can get y.
 Two problems with this approach:
◦ it involves considerable computation at each step.
◦ The spacing between plotted pixel positions is not
uniform, as demonstrated below
 Polar coordinates (r and ) are used to eliminate
the unequal spacing shown above.
 Expressing the circle equation in parametric polar
form yields the pair of equations

◦ x = xc + r cos 
◦ y = yc + r sin 
 When a circle is generated with these
equations using a fixed angular step size, a
circle is plotted with equally spaced points
along the circumference.

 The step size chosen  depends on the


application and the display device.

 Computation can be reduced by considering


the symmetry of circles. The shape of the
circle is similar in each quadrant.

 We can take this one step further and note


that there is also symmetry between octants.
 A method for direct distance comparison is to
test the halfway position between two pixels to
determine if this midpoint is inside or outside
the circle boundary.

 This method is more easily applied to other


conics, and for an integer circle radius.

 we sample at unit intervals and determine the


closest pixel position to the specified circle
path at each step.
 For a given radius r and screen center position (xc, yc),
we can first set up our algorithm to calculate pixel
positions around a circle path centered at the
coordinate origin ( 0, 0 ).

 Then each calculated position (x, y) is moved to its


proper screen position by adding xc to x and yc to y.

 Along the circle section from x = 0 to x = y in the first


quadrant, the slope of the curve varies from 0 to - 1.
 Therefore, we can take unit steps in the positive x
direction over this octant and use a decision parameter
to determine which of the two possible y positions is
closer to the circle path at each step.
 Positions in the other seven octants are then obtained
by symmetry.

 To apply the midpoint method. we define a circle


function:
fcircle(x, y) = x² + y² – r²

 Any point (x, y) on the boundary of the circle with


radius r satisfies the equation fcircle( x, y ) = 0.

 If fcircle( x, y ) < 0, the point is inside the circle


boundary ,
If fcircle( x, y ) > 0, the point is outside the circle
boundary,
If fcircle( x, y ) = 0, the point is on the circle
boundary.
1. Input radius r and circle center (xc, yc ). set the first
point (x0 , y0 ) = (0, r ).

2. Calculate the initial value of the decision parameter as


p0 = 1 – r.

3. At each xk position, starting at k = 0, perform the


following test:
If pk < 0, plot (xk + 1, yk ) and pk+1 = pk + 2xk + 1 + 1,

Otherwise,

plot (xk+ 1, yk – 1 ) and pk+1 = pk + 2xk+1 + 1 – 2yk+1,

where 2xk + 1 = 2xk + 2 and 2yk + 1 = 2yk – 2.


4. Determine symmetry points on the other seven
octants.

5. Move each calculated pixel position (x, y) onto


the circular path centered on (xc, yc) and plot the
coordinate values: x = x + xc , y = y + yc

6. Repeat steps 3 though 5 until x  y.

7. For all points, add the center point (xc, yc )


 Given a circle radius r = 10, demonstrate the
midpoint circle algorithm by determining
positions along the circle octant in the first
quadrant from x = 0 to x = y.

Solution:
 p0 =1 – r = – 9
 Plot the initial point (x0, y0 ) = (0, 10),

 2x0 = 0 and 2y0 =20.


 Successive decision parameter values and
positions along the circle path are calculated
using the midpoint method as appear in the
next table:
K Pk (xk+1, yk+1) 2 xk+1 2 yk+1

0 –9 (1, 10) 2 20

1 –6 (2, 10) 4 20
2 –1 (3, 10) 6 20
3 6 (4, 9) 8 18
4 –3 (5, 9) 10 18

5 8 (6,8) 12 16
6 5 (7,7) 14 14
CS 380
 Given a circle radius r = 15, demonstrate the
midpoint circle algorithm by determining
positions along the circle octant in the first
quadrant from x = 0 to x = y.

Solution:

 p0 = 1 – r = – 14
 plot the initial point (x0 , y0) = (0, 15),
 2x0 = 0 and 2y0 = 30.
 Successive decision parameter values and
positions along the circle path are calculated
using the midpoint method as:
Mid-point Circle Algorithm – Example (2)

K Pk (xk+1, yk+1) 2 xk+1 2 yk+1

0 – 14 (1, 15) 2 30

1 – 11 (2, 15) 4 30

2 – 6 (3, 15) 6 30

3 1 (4, 14) 8 28

4 – 18 (5, 14) 10 28
K Pk (xk+1, yk+1) 2 xk+1 2 yk+1
5 –7 (6,14) 12 28
6 6 (7,13) 14 26
7 –5 (8,13) 16 26
8 12 (9,12) 18 24
9 7 (10,11 ) 20 22
10 6 (11,10) 22 20
 This is an incremental method for scan
converting an ellipse that is centered at
the origin in standard position i.e., with
the major and minor axis parallel to
coordinate system axis.
 It is very similar to the midpoint circle
algorithm. Because of the four-way
symmetry property we need to consider
the entire elliptical curve in the first
quadrant.
 Midpoint ellipse algorithm
plots(finds) points of an ellipse
on the first quadrant by dividing
the quadrant into two regions.
 Each point(x, y) is then projected
into other three quadrants (-x, y),
(x, -y), (-x, -y) i.e. it uses 4-way
symmetry.
 The midpoint ellipse method is applied throughout
the first quadrant in two parts. Now let us take the
start position at (0,ry) and step along the ellipse path
in clockwise order throughout the first quadrant.
 Ellipse function can be defined as:
 fellipse(x,y)=ry2x2+rx2y2-rx2ry2

According to this there are some properties which have


been generated that are:
 fellipse(x,y)<0 which means (x,y) is inside the ellipse
boundary.
 fellipse(x,y)>0 which means (x,y) is outside the ellipse
boundary.
 fellipse(x,y)=0 which means (x,y) is on the ellipse
boundary.
 In region 1 the initial value of a
decision parameter is obtained by
giving starting position = (0,ry).
 i.e. p10=ry2+1/4rx2-rx2ry
 When we enter into a region 2 the
initial position is taken as the last
position selected in region 1 and the
initial decision parameter in region 2
is then:
 p20=ry2(x0+1/2)2+rx2(y0-1)2-rx2ry2
 Take the input and ellipse centre and obtain the
first point on an ellipse centered on the origin
as a (x,y0)= (0,ry).
 Now calculate the initial decision parameter in
region 1 as:
p10=ry2+1/4rx2-rx2ry
 At each xk position in region 1 perform the
following task. If p1k<0 then the next point
along the ellipse centered on (0,0) is (xk+1,yk).
i.e. p1k+1=p1k+2ry2xk+1+ry2
Otherwise the next point along the circle is
(xk+1,yk -1)
i.e. p1k+1=p1k+2ry2xk+1 – 2rx2yk+1+ry2
 Now, again calculate the initial value in region 2
using the last point (x0,y0) calculated in a region
1 as : p20=ry2(x0+1/2)2+rx2(y0-1)2-rx2ry2
 At each yk position in region 2
starting at k =0 perform the following
task. If p2k<0 the next point along
the ellipse centered on (0,0) is (xk , yk-
1)
i.e. p2k+1=p2k-2rx2yk+1+rx2
Otherwise the next point along the
circle will be (xk+1,yk -1)
i.e. p2k+1 =p2k+2ry2xk+1 -2rx2yk+1+rx2
 Now determine the symmetric points
in another three quadrants.
 Plot the coordinate value as: x=x+xc ,
y=y+yc
 Repeat the steps for region 1
until 2ry2x>=2rx2y.
 In geometry, a polygon is a plane figure that is described by a finite number of
straight line segments connected to form a closed polygonal chain or polygonal
circuit. The solid plane region, the bounding circuit, or the two together, may be called
a polygon.
 We know that polygon is a figure with many sides.
 A polygon can be represented as a group of connected edges,forming a close figure.
 The line segments which form the boundary of polygon are called as edges or sides.
 The polygon may be of any shape.
 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.
Types of Polygons and their Properties
A two-dimensional shape which is enclosed by a finite number of
straight lines joining in the form of a closed-loop is called
a polygon. The line segments which make the polygon are known
as polygon’s sides or edges. Whereas the corner or the point
where any two sides join is called the vertex of the polygon. Now,
based on the number of sides and angles, polygons are classified
into different types, which we are going to discuss here.
Polygons are classified into various types based on the number of
sides and measures of the angles. They are:
•Regular Polygons
•Irregular Polygons
•Concave Polygons
•Convex Polygons
Regular Polygon
In a regular polygon, all the sides of the polygon are equal, and all the
interior angles are the same. For example, a regular hexagon has six equal
sides, and all its interior angles measure to 120 degrees. Few more examples
are mentioned below.

Examples:
•A square has all its sides equal to 5cm, and all the angles are at 90°.
•An equilateral triangle has all three sides equal to 10cm and angles measure
to 60°.
•A regular pentagon has 5 equal sides and all the interior angles measures to
108 degrees.
Irregular Polygon
A polygon with an irregular shape. It means the sides and angles of the polygon
are not equal.
Example:
•A quadrilateral with unequal sides.
•An isosceles triangle has only two of its sides equal, and the third side has a
different measurement.
Convex Polygon
In a convex polygon, the measure of the interior angle is less than 180 degrees. It is
exactly opposite to the concave polygon. The vertices of a convex polygon are always
outwards.
Example: See the figure of an irregular hexagon, whose vertices are outwards.

Concave Polygon
In a concave polygon, at least one angle measures more than 180 degrees. The
vertices of a concave polygon are inwards as well as outwards.
Polygon filling is the process of coloring the area of polygon. Area may be defined at
the pixel or geometric level.
Describe the area as the total number of pixels it contain by the boundling pixels that
outline the polygon.
Polygon is an ordered list of vertices as shown in the following figure. For filling
polygons with particular colors, you need to determine the pixels falling on the border
of the polygon and those which fall inside the polygon. In this chapter, we will see how
we can fill polygons using different techniques.


Scan Line Algorithm
This algorithm works by intersecting scanline with polygon edges and fills the polygon
between pairs of intersections. The following steps depict how this algorithm works.

Step 1 − Find out the Ymin and Ymax from the given polygon.

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,p1p0,p1, p1,p2p1,p2, and p2,p3p2,p3.
Step 4 − Fill all those pair of coordinates that are inside polygons and ignore the
alternate pairs.
Inside-outside Test
This method is also known as counting number method. While filling an object, we often
need to identify whether particular point is inside the object or outside it. There are two
methods by which we can identify whether particular point is inside an object or outside.
•Odd-Even Rule
•Nonzero winding number rule
Odd-Even Rule
In this technique, we will count the edge crossing along the line from any point x,y to infinity.
If the number of interactions is odd, then the point x,y is an interior point; and if the number
of interactions is even, then the point x,y is an exterior point. The following example depicts
this concept.

From the above figure, we can see that from the point x,yx,y, the number of
interactions point on the left side is 5 and on the right side is 3. From both ends, the
number of interaction points is odd, so the point is considered within the object.
Nonzero Winding Number Rule
This method is also used with the simple polygons to test the
given point is interior or not. It can be simply understood with
the help of a pin and a rubber band. Fix up the pin on one of
the edge of the polygon and tie-up the rubber band in it and
then stretch the rubber band along the edges of the polygon.
When all the edges of the polygon are covered by the rubber
band, check out the pin which has been fixed up at the point to
be test. If we find at least one wind at the point consider it
within the polygon, else we can say that the point is not inside
the polygon.
In another alternative 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.
Give the value 1 to all the edges which are going to
upward direction and all other -1 as direction values.
Check the edge direction values from which the scan line
is passing and sum up them.
If the total sum of this direction value is non-zero, then
this point to be tested is an interior point, otherwise it is
an exterior point.
In the above figure, we sum up the direction values from
which the scan line is passing then the total is 1 – 1 + 1 =
1; which is non-zero. So the point is said to be an interior
point.
69
 Fill-Area algorithms are used to
fill the interior of a polygonal
shape.
 Many algorithms perform fill
operations by first identifying
the interior points, given the
polygon boundary.

70
The basic filling algorithm is commonly used in
interactive graphics packages, where the user
specifies an interior point of the region to be filled.

4-connected pixels

71
[1] Set the user specified point.
[2] Store the four neighboring pixels
in a stack.
[3] Remove a pixel from the stack.
[4] If the pixel is not set,
Set the pixel
Push its four neighboring pixels into the
stack
[5] Go to step 3
[6] Repeat till the stack is empty.

72
void fill(int x, int y) {
if(getPixel(x,y)==0){
setPixel(x,y);
fill(x+1,y);
fill(x-1,y);
fill(x,y+1);
fill(x,y-1);
}
}

73
 Requires an interior point.
 Involves considerable amount of stack
operations.
 The boundary has to be closed.
 Not suitable for self-intersecting
polygons

74
 Boundary Fill Algorithm
◦ For filling a region with a single
boundary color.
◦ Condition for setting pixels:
 Color is not the same as border color
 Color is not the same as fill color

 Flood Fill Algorithm


◦ For filling a region with multiple
boundary colors.
◦ Condition for setting pixels:
 Color is same as the old interior color

75
Start with max y and move to min y or vice versa
For each scan line:
❖ Find by increasinthe intersections of the scan line with
all edges of the polygon.
❖ Sort the intersections g x coordinate.

❖ Fill in all pixels between pairs of intersections

For scan line number 8 the sorted list of x-coordinates is


(2, 4, 9, 13) Therefore draw line b/w (2,4) & (9,13)
Assume that we are taking scan line from max y
and moving towards min y

 We know that for every scan line we have to


calculate x intersection with every polygon side.
We can determine the next x intersection
value as

 xi+1=xi - 1/m where m is the slope of edge


 It is very easy to identify which polygon sides
should be tested for x-intersection, if we sort edges
in order of maximum y and find active edge list
(Active edge list for scan line contains all the
edges crossed by that scan line ).

 Two pointers are used to indicate the beginning


and ending of active edge list. As the scan line
move down the picture the beginning and end
pointers also moved down to eliminate the edges
which end above the current scan line and include
the new edges which now start on or above the
current scan line .
Boundary Fill Algorithm

This algorithm picks a point inside the


polygon and starts to fill until it hits the
boundary of the object.

Assumption:

In this algorithm, we assume that color of the


boundary is same for the entire object.
void boundaryfill(int x,int y,int fill_color,int
boundary_color)
{
if(getpixel(x,y)!=boundary_color &&
getpixel(x,y)!=fill_color)
{
putpixel(x,y,fill_color);
boundaryfill(x+1,y,fill_color,boundary_color);
boundaryfill(x-1,y, fill_color,boundary_color);
boundaryfill(x,y+1, fill_color,boundary_color);
boundaryfill(x,y-1, fill_color,boundary_color);
}
}
Flood Fill Algorithm
Sometimes we want to fill in an area that
is not defined within a single color
boundary.
We paint such areas by replacing a
specified interior color instead of
searching for a boundary color value.
This approach is called a flood-fill
algorithm.
void floodfill(int x,int y,int fill_color,int old_color)
{
if(getpixel(x,y)==old_color)
{
putpixel(x,y,fill_color);
floodfill(x+1,y,fill_color,old_color);
floodfill(x-1,y, fill_color,old_color);
floodfill(x,y+1, fill_color,old_color);
floodfill(x,y-1, fill_color,old_color);
}
}
Boundary Fill Algorithm /Flood Fill algorithm

The boundary fill algorithm/ flood fill


algorithm can be implemented by 4-
connected pixels or 8-connected
pixels.
4-connected (Example)

Start Position
1
3
2 3
2
1
1 4
2 4
2
1
1
2
2
1
5 1

5
1
1

1
Some region remains unfilled
8-connected (Example)

Start Position
4 1 5
5
2 3
4
3
2
1
6
4 1 6
2 3 4
3
2
1
7 8 8
7
4 1 4
2 3 3
2
1
12
11 9 12 11
7 10 10
9
4 1 7
2 3 4
3
2
1
11
11 9
10
7 10
9
7
4 1
4
2 3
3
2
1
9
10
7 10
9
7
4 1
4
2 3
3
2
1
9
7
9

4 1 7

2 3 4
3
2
1
7

4 1 7
2 3 4
3
2
1
4 1
2 3 4
3
2
1
1
2 3
3
2
1
1
2

2
1
1

1
FLOOD-FILL ALGORITHM BOUNDARY-FILL ALGORITHM

It can process the image containing more than one boundary It can only process the image containing single boundary
colours. colour.

Flood-fill algorithm is comparatively slower than the Boundary-fill algorithm is faster than the Flood-fill
Boundary-fill algorithm. algorithm.

In Flood-fill algorithm a random colour can be used to paint


In Boundary-fill algorithm Interior points are painted by
the interior portion then the old one is replaced with a new
continuously searching for the boundary colour.
one.

Memory consumption is relatively low in Boundary-fill


It requires huge amount of memory.
algorithm.

Flood-fill algorithms are simple and efficient. The complexity of Bounndary-fill algorithm is high.
Definition
Parameter that affects the way a primitive will be
displayed

Line Attribute
. Type
. Width
. Color
. Pen & Brush

10
Lecture 2 30/9/2008 7
Type
. Solid
. Dotted – very short dash with spacing equal to
or greater than dash itself
. Dashed – displayed by generating an interdash
spacing
Pixel count for the span and interspan length is specified
by the mask . Ex. 111100011110001111
Note : Fixed pixel with dashes can produce unequal length
dashes. It depend on line orientation. So, need to adjust the
number of plotted pixels for different slopes.
10
Lecture 2 30/9/2008 8
Width
. Specify in pixels and proportion of a standard line width.
. Thicker line can be produced by.
. Adding extra pixel vertically when |m| < 1
. Adding extra pixel horizontally when |m| > 1
. Issues:
. Line have different thickness on the slope
. Problem with
. End of the line
. Joining the two lines (polygon)
Width

11
Lecture 2 30/9/2008 0
11
Lecture 2 30/9/2008 1
11
Lecture 2 30/9/2008 2
Pen and Brush
. The selected “pen” or “brush” determine the way a line will be drawn.
. Pens and brushes have size, shape, color and pattern attribute.
. Pixel mask is applied in both of them.

11
Lecture 2 30/9/2008 3
Similar to line : type + width
Thicker curves can be produced by:
1. Plotting additional pixel
2. Filling the space between two concentric circles.
3. Using thicker pen or brush

11
Lecture 2 30/9/2008 4
Width

11
Lecture 2 30/9/2008 5
11
Lecture 2 30/9/2008 6
11
Lecture 2 30/9/2008 7
11
Lecture 2 30/9/2008 8
11
Lecture 2 30/9/2008 9
12
Lecture 2 30/9/2008 0
Text and Characters
•Very important output primitive
• Many pictures require text
• Two general techniques used
– Bitmapped (raster)
– Stroked (outline)

12
Lecture 2 30/9/2008 1
Each character represented (stored) as a 2-D array
– Each element corresponds to a pixel in a rectangular
“character cell”
– Simplest: each element is a bit (1=pixel on, 0=pixel off)

00111000
01101100
11000110
11000110
11111110
11000110
11000110
00000000

12
Lecture 2 30/9/2008 2
Each character represented (stored) as
a series of line segments
– sometimes as more complex primitives
Parameters needed to draw each stroke
– endpoint coordinates for line segments

12
Lecture 2 30/9/2008 3
Characteristics of Bitmapped Characters
• Each character in set requires same amount of memory
to store
• Characters can only be scaled by integer scaling factors
• "Blocky" appearance
• Difficult to rotate characters by arbitrary angles
• Fast (BitBLT)

Characteristics of Stroked Characters


• Number of stokes (storage space) depends on complexity
of character
• Each stroke must be scan converted more time to display
• Easily scaled and rotated arbitrarily
• – just transform each stroke
12
Lecture 2 30/9/2008 4
When each function reference a single attribute that specify exactly
how primitive to be displayed ; then its called
individual (unbundled attribute).

.Bundled attributes is where a set of attributes value can be chosen


by specifying the appropriate index table.

.The table for each primitive that defines group of attribute values is
called bundled table.

.Attribute that can be bundled are:


. Bundled Line Attribute
. Bundled Area-Fill Attribute
. Bundled Text Attribute
. Bundled Marker Attribute
12
Lecture 2 30/9/2008 5
Letters, numbers, and other character
can be displayed in a variety of size
and styles.
• Typeface: The overall design style for a set of characters is
call typeface: Zar, nazanin, Titr.

 Font: Referred to a set of cast metal character forms in a


particular size and forma: 10 point Zar.
 Two different representation are used
for storing computer fonts:

1. Bitmap font (or bitmapped font)

2. Outline font
 Bitmap font (or bitmapped font): A
simple method for representing the
character shapes in a particular typeface
is to use rectangular grid pattern.
 The character grid only need to be
mapped to a frame buffer position.
 Bitmap fonts required more space,
because each variation (size and format)
must be stored in a font cash.

Bold Italic
 Graphic primitives such as lines and
arcs are used to define the outline of
each character.
 Require less storage since variation
does not require a distinct font cash.
 We can produce boldface, italic, or
different size by manipulating the curve
definition for the character outlines.

 It does take more time to process the


outline fonts, because they must be scan
converted into frame buffer.
Definition:-

(1) In computer graphics, the process by which smooth curves and other lines become
jagged because the resolution of the graphics device or file is not high enough to
represent a smooth curve. Smoothing and antialiasing techniques can reduce the effect
of aliasing.

(2) In digital sound, aliasing is a static distortion resulting from a low sampling rate-
below 40 kilohertz (Khz).

(3) aliasing is an effect that causes different signals to become indistinguishable


(or aliases of one another) when sampled. It also often refers to
the distortion or artifact that results when a signal reconstructed from samples is
different from the original continuous signal.
Aliasing can occur in signals sampled in time, for instance digital audio, and is referred to
as temporal aliasing. It can also occur in spatially sampled signals (e.g. moiré
patterns in digital images); this type of aliasing is called spatial aliasing.

Aliasing is generally avoided by applying low pass filters or anti-aliasing filters (AAF) to
the input signal before sampling and when converting a signal from a higher to a lower
sampling rate.
Suitable reconstruction filtering should then be used when restoring the sampled signal to
the continuous domain or converting a signal from a lower to a higher sampling rate.
For spatial anti-aliasing, the types of anti-aliasing include
fast sample anti-aliasing (FSAA), multisampling anti-aliasing, and super sampling.
Anti-aliasing is a technique used in computer graphics to remove the aliasing effect.
The aliasing effect is the appearance of jagged edges or “jaggies” in a rasterized image (an
image rendered using pixels).

Aliasing occurs when real-world objects which comprise of smooth, continuous curves are
rasterized using pixels.

The problem of jagged edges technically occurs due to distortion of the image when scan
conversion is done with sampling at a low frequency, which is also known as Under
sampling. Aliasing occurs when real-world objects which comprise of smooth, continuous
curves are rasterized using pixels.

Cause of anti-aliasing is Undersampling. Undersampling results in loss of information of


the picture. Undersampling occurs when sampling is done at a frequency lower than
Nyquist sampling frequency. To avoid this loss, we need to have our sampling frequency
atleast twice that of highest frequency occurring in the object.

This minimum required frequency is referred to as Nyquist sampling frequency (fs):


fs =2*fmax

You might also like