Computer Graphics
Unit 2
Line equation
m is slope of line
c is intersection of line on y axis
Slope of the line
Line drawing algorithms
DDA – Digital Differential Analyzer algorithm.
Bresenham’s Line Drawing algorithm.
DDA line drawing algorithm
1. Accept two end points (x1,y1) and (x2,y2).
2. Find dx=x2-x1 and dy = y2-y1
3. If dx > dy, step = dx
4. Else if dy > dx, step = dy
5. x = x1, y = y1;
6. Xinc = dx/step and Yinc = dy/step
7. Put pixel ( x, y )
8. Xt = x + Xinc and Yt = y + Yinc
9. x = round(Xt) and y = round(Yinc)
10. step--
11. Goto 7 till step == 0
Additional steps to DDA
Case 1
If dx > dy and x1 < x2
Xinc = 1
Yinc = m
Case 2
If dx > dy and x1 > x2
Xinc = -1
Yinc = m
Case 3
If dx < dy and y1 < y2
Xinc = 1/m
Yinc = 1
Case 4
If dx < dy and y1 > y2
Xinc = 1/m
Yinc = - 1
Case 1
Xt = X + Xinc
Yt = Y + Yinc
80
10
10 100
Case 2
Xt = X – Xinc
Yt = Y + Yinc
80
10
10 100
Case 3
Xt = X + Xinc
Yt = Y + Yinc
100
10
10 80
Case 4
Xt = X + Xinc
Yt = Y - Yinc
40
20
20 30
Example
Lets x1 = 0, y1 = 0
X2 = 2, y2 = 3
dx = x2 – x1 = 2-0 = 2
dy = y2-y1 = 3-0 = 3
dy>dx Therefore step =dy
xin = dx/step = 2/3 = 0.67
yin = dy/step = 3/3 = 1
x=x1 y=y1
x=0 y=0
x y Plot
0 0 (0,0)
0.67 1 (1,1)
1.34 2 (1,2)
2.01 3 (2,3)
4
Y 0 1 2 3
X
Bresenham’s line drawing algorithm –(1962) at
IBM
d2
d1
Algorithm
1) Read (x1, y1) and (x2, y2)
2) dx = x2 – x1 and dy = y2 – y1
3) Initialize x = x1 y = y1
4) Plot (x, y)
5) Calculate decision parameter Pk
Pk = 2dy – dx
6) If Pk < 0
{
x = x+1
y= y
Pk= Pk + 2dy
}
If Pk >=0
{
x = x+ 1
y= y + 1
Pk = Pk+ 2dy – 2dx
}
Plot ( x, y )
7) Repeat step 6 dx times
8) Stop
y=m(xk+1)+b
d1 = y-yk
=m(xk+1)+b-yk
d2 = (yk+1)-yk
=(yk+1)- m(xk+1)+b
d1-d2 = 2m(xk+1)-2yk+2b-1
d1-d2 = 2*(dy/dx)(xk+1)-2yk+2b-1
dx(d1-d2) = 2dy(xk+1)-2dx*yk + dx(2b-1)
= 2dy*xk-2dx*yk + c
Pk for kth step
Pk= dx*(d1-d2)
=2dy*xk-2dx*yk+c
For k+1 step Pk+1 is
Pk+1 = 2dy*xk+1 – 2dx*yk+1+c
Subtracting Pk
Pk+1 – Pk =2dy*[(xk+1)-xk] – 2dx[(yk+1)-yk]
But xk+1= xk +1
Pk+1 = Pk + 2dy – 2dy – 2dx[(yk+1)-yk]
[(yk+1)-yk] is either 0 or 1 depending on the sign
of Pk
The first decision parameter P0 is evaluated
at(x0, y0) is given below as
P0 = 2dy – dx
Similarly for slop > 1 && slop = 1 decision
parameter
Pk+1 = Pk+2dx-2dy*[(xk+1)-xk]
P0=2dx-dy
Draw Line between (6,6) to (12,9)
dx = 12-6 = 6
dy = 9-6 = 3
Calculate P0 = 2dy-dx = 2(3)-6 = 0
i x y Pk Plot
1 6 6 0 (6,6)
2 7 7 -6 (7,7)
3 8 7 0 (8,7)
4 9 8 -6 (9,8)
5 108 0 (10,8)
6 119 -6 (11,9)
11
10
Y 9
8
7
6
5
4
4 5 6 7 8 9 10 11 12
X
Difference between DDA and
Bresenham’s algorithms
Symmetry of circle
Symmetry of circle….
Circle is an eight-way symmetric figure. The
shape of circle is the same in all quadrants. In
each quadrant, there are two octants. If the
calculation of the point of one octant is done,
then the other seven points can be calculated
easily by using the concept of eight-way
symmetry.
Bresenham’s Circle Drawing Algorithm
Step1: Start Algorithm
Step2: Declare p, q, x, y, r, d variables. p, q are coordinates
of the center of the circle. r is the radius of the circle.
Step3: Enter the value of r
Step4: Calculate d = 3 - 2r
Step5: Initialize x=0 y= r
Step6: Check if the whole circle is scan converted
If x > = y
Stop
Step7: Plot eight points by using concepts of eight-
way symmetry. The center is at (p, q). Current active
pixel is (x, y).
putpixel (x+p, y+q)
putpixel (y+p, x+q)
putpixel (-y+p, x+q)
putpixel (-x+p, y+q)
putpixel (-x+p, -y+q)
putpixel (-y+p, -x+q)
putpixel (y+p, -x+q)
putpixel (x+p, -y-q)
Step8: Find location of next pixels to be scanned
If d < 0
then d = d + 4x + 6
increment x = x + 1
If d ≥ 0
then d = d + 4 (x - y) + 10
increment x = x + 1
decrement y = y - 1
Step9: Go to step 6
Step10: Stop Algorithm
Decision variable di
Assume (xi, yi) is the last
scan converted pixel.
Now next pixel is to be
decided.
Whether to choose
T or S
T(xi+1, yi) and S(xi+1,yi-1)
D(T) = (xi+1)2 + yi2 – r2
D(S) = (xi+1)2 + (yi-1)2 – r2
Now D(T) will be always >0 because T is
outside the circle.
D(S) will be always <0 because S is inside the
circle.
The decision variable di can be defined as
di = D(T) + D(S)
di =2(xi +1)2 + yi2 + (yi-1)2 – 2r2
When di < 0 we have D(T) < D(S)
We choose pixel T to draw.
When di > 0 we have D(T) >= D(S)
We choose pixel S to draw.
Now di+1 for next step
d
i+1 = 2(xi+1 +1) + yi+1
2 + (y
i+1-1) – 2r
2 2 2
Hence
d - d = 2(x
i+1 +1) + yi+1 + (yi+1-1)
2 2 2
i+1 i
– 2(xi +1)2 - yi2 - (yi-1)2
Since x
i+1 =xi+1
d
i+1 = di + 4xi +2 (y i+1-y i) - 2 (yi+1-yi) + 6
2 2
If di < 0
And T is chosen then yi+1=yi and so on
Polygon
Polygon is a closed figure with many vertices
and edges and at each vertex exactly two
edges meet and no edge crosses each other.
Types of polygons
1) Convex polygon
2) Concave polygon
Convex Polygon
A polygon is called convex of line joining any
two interior points of the polygon lies inside
the polygon.
A
B
Concave Polygon
In this polygon if we take points and draw the
line and points on this line do not entirely fall
within the polygon , then this is the concave
polygon.
Inside outside test
The method to check whether the point is
inside of polygon or outside of polygon is
called Inside – outside test.
There are two test.
1) Even odd test
2) Non zero winding number
Even Odd Method
Suppose we want to check whether A is inside
or not.
Draw a horizontal line from A to B.
B is surely outside point
1) Count the number of times the line
intersects with polygon edges.
2) A point is inside the polygon if either count
of intersections is odd or point lies on an
edge of polygon. If none of the conditions is
true, then point lies outside.
Even Odd Method
B A
The even odd test fails when the intersection
is vertex.
To solve this problem check whether the
edges forming the vertex are at same side of
the line AB. Then count this intersection as
even. E.g 2
F
E
Total
G
Intersection=1+2=3
A is inside
c D
If the edges falls opposite of the line AB then
count the intersection as odd.
Total Intersection =1
D
The point A is inside
E
Non Zero Winding Numbers
Non Zero Winding Numbers
Give directions to all edges of the polygon.
Draw scan line from the point to be tested
towards the left most of X direction.
Give value 1 to the edges travelling upward
direction.
Give value -1 to all other directions.
Check the edge direction values from which
the scan line is passing and sum up them.
If the total sum is non zero the point is
interior.
Else the point is exterior.
In the example sum = 1-1 = 0, the point is
Exterior
A
B
1-1=0
Point is
exterior
Polygon filling
Seed fill algorithm
In this approach color filling is started at a
point inside a region. Therefore name of the
algorithm is seed fill.
Boundary filling algorithm
Boundary Fill Algorithm starts at a pixel inside
the polygon to be filled and paints the interior
proceeding outwards towards the boundary.
This algorithm works only if the color with
which the region has to be filled and the color
of the boundary of the region are different.
It may not fill regions sometimes correctly when
some interior pixel is already filled with color.
The algorithm will check this boundary pixel for
filling and will found already filled so recursive
process will terminate.
This algorithm uses the recursive method.
First of all, a starting pixel called as the seed
is considered. The algorithm checks
boundary pixel or adjacent pixels are colored
or not. If the adjacent pixel is already filled or
colored then leave it, otherwise fill it. The
filling is done using four connected or eight
connected approaches.
Four connected boundary fill
In this boundary fill algorithm
four neighboring points of the
pixel are tested. These are
pixels positions that are the
right, left, above and below the
current pixel.
Eight connected boundary fill
8-connected pixels : More complex
figures are filled using this approach.
The pixels to be tested are the 8
neighbouring pixels, the pixel on the
right, left, above, below and the 4
diagonal pixels. Areas filled by this
method are called 8-connected.
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.
Once again, this algorithm relies on the Four-connect or
Eight-connect method of filling in the pixels. But instead of
looking for the boundary color, it is looking for all adjacent
pixels that are a part of the interior.
Flood fill algorithm
Difficulties in Boundary fill and Flood
fill
First problem is that if some inside pixels are
already displayed in fill color then recursive
branch terminates leaving further internal
pixels unfilled.
To solve this problem we have to first change
color of any internal pixel that is having
painted in fill color.
Second problem is of stacking of neighboring
points but in case of large polygons the space
required will be very large.
To solve all these problems scan line
algorithm is required.
Scan Line algorithm
This algorithm works by intersecting scanline
with polygon edges and fills the polygon
between pairs of intersections.
This algorithm fills the horizontal spans
instead of 4 connected or 8 connected.
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,p1], [p1,p2] and [p2,p3].
Step 4 − Fill all those pair of coordinates that
are inside polygons and ignore the alternate
pairs
Scan line intersection with vertex
Scan line 1 intersect vertex at B. Both the
edges of vertex are on the same side.
Therefore treat this vertex as even. Therefore
we can fill area between JB and BD.
Scan line 2 intersect vertex at E. Both the
edges of vertex are on the opposite sides.
Therefore treat this vertex as odd. Therefore
we can only fill the area between IE, not
beyond that.
Algorithm
1. Read n number of vertices of polygon.
2. Read x and y coordinates of all vertices.
3. Find ymin and ymax.
4. Sort polygon edges from ymax to ymin.
5. For each y value from max to min, determine
which sides can be intersected and find x
values of these intersection points.
6. Sort these x values and pair these values.
7. Pass these x value pairs and scan line y
value to fill color.
8. Stop
Example
AE Active
edges for
AB scan line 1
ED
CD
CB
Sorted list of
edges
Character Generation Methods
Stroke Method
Starburst Method
Bitmap Method
Stroke method
This method uses small line segments to
generate characters.
The small line segments are drawn like a
stroke of a pen to form a character.
The stroke method supports the scaling of
the character.
Stroke method
Start
Starbust method
In this method a fix pattern of line segments
are used to generate characters.
There are 24 line segments that are used.
Out of 24 line segments, segments required
to display a particular character are
highlighted.
This method is called starbust because of
characteristic appearance.
Starbust method
Bitmap method
Also called as dot matrix method.
In this method characters are represented by
an array of dots in the matrix form.
It is two dimensional array having columns
and rows.
An 5x7 array is commonly used to represent
a character as shown in figure.
However 7x9 and 9x13 arrays are also used.
Bitmap method
Each dot in the matrix is a pixel.
The character’s bit pattern is stored in a
frame buffer.
Usually the dot pattern for all character are
stored in the hardware device called a
character generator chip.
Bitmap method
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1
1 0 0 0 1
1 0 0 0 1