Subject Name: Computer Graphics and Multimedia
Subject Code: IT-6003
Semester: 6th
Downloaded from [Link]
Unit-II
Scan conversion techniques:
The process of determining which pixels provide best approximation to the desired line is known as
rasterization. When combined with the process of generating the picture in scan line order it is known as scan
conversion.
The line should be appearing as a straight line and it should start and end accurately.
Characteristics of a line:
The line should be displayed with constant brightness along its length independent of its length and
The line should be drawn rapidly.
orientation.
The basic concept behind all drawing algorithms is to reduce the computations and provide the result rapidly,
so most of the line drawing algorithms use incremental methods. In these methods line starts with starting
point and then a fix increment is added to get the next point on the line. This is continued till the end of line.
DDA (Digital Differential Analyzer) Algorithm:
13
12
11
10
6 7 8 9 10 11 12 13
Figure 1.9 Representation of line with DDA algorithm
Procedure DDA (X1, Y1, X2, Y2: Integer);
Var Length, I: Integer;
X,Y,Xinc,Yinc:Real;
Begin
Length: = ABS(X2 - X1);
If ABS (Y2 - Y1) > Length Then
Length: = ABS (Y2-Y1);
Xinc: = (X2 - X1)/Length;
Yinc: = (Y2 - Y1)/Length;
X: = X1+0.5*SIGN(Xinc);
Y: = Y1+0.5*SIGN(Yinc);
For I: = 0 To Length Do
Begin
Plot (Round(X), Round(Y));
X: = X + Xinc;
Y: = Y + Yinc
Page no: 1 Follow us on facebook to get real-time updates from RGPV
Downloaded from [Link]
Chameli Devi Group of Institutions
Department of Information Technology
End {For}
End; {DDA}
DDA (digital differential analyzer) creates good lines but it is too time consuming due to the round function
and long operations on real values.
DDA works with floating point arithmetic
Key points:
Rounding to integers necessary
It takes lot of computation time because at each and every step it has to round off.
Bresenham's Line Algorithm:
A) Bresenham's line algorithm for m<1
F0R (X1, Y1) =1, 2 AND (X2, Y2) =8, 5
K PK XK+1 YK+1
0 -1 2 2
1 5 3 3
2 -3 4 3
3 3 5 4
4 -5 6 4
5 1 7 5
6 -7 8 5
calculate x = xn - x0 and y = yn - y0
input line endpoints, (x0,y0) and (xn, yn)
calculate parameter p0 = 2 y - x
set pixel at position (x0,y0)
repeat the following steps until (xn, yn) is reached:
if pi < 0
calculate new pi+1 = pi + 2 y
set the next pixel at position (xi +1, yi )
if pi ≥
set the next pixel at position (xi +1, yi + 1 )
calculate new pi+1 = pi + 2y - 2x
Repeat steps until dx-1 times.
B) Bresenham's line algorithm for m>1
calculate x = xn - x0 and y = yn - y0
input line endpoints, (x0,y0) and (xn, yn)
calculate parameter p0 = 2 x - y
set pixel at position (x0,y0)
repeat the following steps until (xn, yn) is reached:
if pi < 0
calculate new pi+1 = pi + 2 x
set the next pixel at position (xi , yi +1)
if pi ≥
set the next pixel at position (xi +1, yi + 1 )
calculate new pi+1 = pi + 2x - 2y
Page no: 2 Follow us on facebook to get real-time updates from RGPV
Downloaded from [Link]
Chameli Devi Group of Institutions
Department of Information Technology
Repeat steps until dy-1 times.
F0R (X1, Y1) =1, 3 AND (X2, Y2) =8, 12
K PK XK+1 YK+1
0 5 2 4
1 1 3 5
2 -3 3 6
3 11 4 7
4 7 5 8
5 3 6 9
6 -1 6 10
7 13 7 11
8 9 8 12
B ese ha ’s algo ith uses i tege a ith eti
Key points
Constants need to be computed only once
B ese ha ’s algo ith ge e ally faste tha DDA
Midpoint Circle Algorithm:
1. Input radius r
2. Plot a point at (0, r)
3. Calculate the initial value of the decision parameter as p0 = 5/4 – ≈ – r
4. At each position xk, starting at k = 0, perform the following test:
if pk < 0
plot point at (xk +1, yk)
compute new pk+1 = pk + 2xk+1 + 1
else
plot point at (xk + 1, yk – 1)
compute new pk+1 = pk + 2xk+1 + 1 – 2yk+1
6. Repeat steps 4 and 5 until x y.
5. Determine symmetry points in the other seven octants and plot points
For radius =8
So your first point is (0, r) = (0, 8)
K PK XK+1 YK+1
0 -7 1 8
1 -4 2 8
2 1 3 7
3 -6 4 7
4 3 5 6
5 2 6 5
Brese ha ’s Circle Algorith :
x0 = 0
y0 = r
p0 = 3 – 2r
if pi < 0 then
yi+1 = yi
pi+1 = pi + 4xi + 6
Page no: 3 Follow us on facebook to get real-time updates from RGPV
Downloaded from [Link]
Chameli Devi Group of Institutions
Department of Information Technology
else if pi ≥ 0 then
yi+1 = yi – 1
pi+1 = pi + 4(xi – yi) + 10
Stop when xi ≥ yi and determine symmetry points in the other octants
For radius=6
So your first point is (0, r) = (0, 6)
K PI XK+1 YK+1
0 -9 1 6
1 1 2 5
2 -1 3 5
3 17 4 4
Midpoint circle algorithm
Let us suppose centre is other than origin here xc, yc is (5,5)
For radius =8
So your first point is (0,r)=(0,8)
K PK XK+1 YK+1 PLOTTED POINTS
X=xc+x y=yc+y
0 -7 1 8
X=5+1=6,y=5+8=13
X=xc+x y=yc+y
1 -4 2 8
X=5+2=7,y=5+8=13
X=xc+x y=yc+y
2 1 3 7
X=5+3=8,y=5+7=12
X=xc+x y=yc+y
3 -6 4 7
X=5+4=9,y=5+7=12
X=xc+x y=yc+y
4 3 5 6
X=5+5=10,y=5+6=11
X=xc+x y=yc+y
5 2 6 5
X=5+6=11,y=5+5=10
Remaining points you can calculate
XC+X, YC-Y
XC-X, YC+Y
XC-X, YC-Y
XC+Y, YC+X
XC+Y, YC-X
XC-Y, YC+X
,XC-Y, YC-X
Brese ha ’s Circle Algorith :
For radius=6 suppose center is( xc,yc)=3
K PI XK+1 YK+1 PLOTTED POINTS
X=xc+x y=yc+y
0 -9 1 6
X=3+1=4,y=3+6=9
X=xc+x y=yc+y
1 1 2 5
X=3+2=5,y=3+5=8
Page no: 4 Follow us on facebook to get real-time updates from RGPV
Downloaded from [Link]
Chameli Devi Group of Institutions
Department of Information Technology
X=xc+x y=yc+y
2 -1 3 5
X=3+3=6,y=3+5=8
X=xc+x y=yc+y
3 17 4 4
X=3+4=7,y=3+4=7
Boundary Fill Algorithm:
A boundary-fill procedure accepts as input the coordinates of an interior point (x, y), a fill color, and a
boundary color. Starting from (x, y), the procedure tests neighboring positions to determine whether they are
of the boundary color. If not, they are painted with the fill color, and their neighbors are tested. This process
continues until all pixels up to the boundary color for the area have been tested.
void BoundaryFill4(int x, int y, int fill, int boundary)
{
int current;
current=GetPixel(x,y);
if (current!=boundary) and (current!=fill)
{
SetPixel(x,y,fill);
BoundaryFill4(x+1,y,fill,boundary);
BoundaryFill4(x-1,y,fill,boundary);
BoundaryFill4(x,y+1,fill,boundary);
BoundaryFill4(x,y-1,fill,boundary);
}
}
Flood Fill Algorithm:
Sometimes we want to fill in (or recolor) an area that is not defined within a single-color boundary. It has area
bordered by several different color regions. We can 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. We start from a
specified interior point (x, y) and reassign all pixel values that are currently set to a given interior color with
the desired fill color. If the area we want to paint has more than one interior color, we can first reassign pixel
values so that all interior points have the same color.
void FloodFill4(int x, int y, int fill, int old_color)
{
if (GetPixel(x,y)== old_color)
{
SetPixel(x,y,fill);
FloodFill4(x+1,y,fill,boundary);
FloodFill4(x-1,y,fill,boundary);
FloodFill4(x,y+1,fill,boundary);
FloodFill4(x,y-1,fill,boundary);
}}
Bezier Curves:
Bezier curve is discovered by the French engineer Pierre Bézier. These curves can be generated under the
control of other points. Approximate tangents by using control points are used to generate curve. The Bezier
curve can be represented mathematically as –
Page no: 5 Follow us on facebook to get real-time updates from RGPV
Downloaded from [Link]
Chameli Devi Group of Institutions
Department of Information Technology
Suppose we are given n+1 control point positions : Pk = (Xk,Yk,Zk), with k = o to n. These coordinate points are
blended to produce the position vector P(u), which describes the path of an approximating Bezier Polynomial
Function between Po and Pn.
�
� � = ∑�=0 Pk BEZk, n u 0<=u<=1
The Bezier blending functions BEZk,n(u) are the Bernstein polynomials.
BEZk,n(u) = C(n,k)uk(1-u)n-k
Where,
C(n,k) are the binomial coefficients.
C(n,k) = n! / k!(n-k)!
The simplest Bézier curve is the straight line from the point P0P0 to P1P1. A quadratic Bezier curve is
determined by three control points. A cubic Bezier curve is determined by four control points.
Figure 3.27 Representation of Bezier Curve
Properties of Bezier Curves
Bezie u es ha e the follo i g p ope ties −
They generally follow the shape of the control polygon, which consists of the segments joining the control
They always pass through the first and last control points.
points.
They are contained in the convex hull of their defining control points.
The degree of the polynomial defining the curve segment is one less that the number of defining polygon
A Bezier curve generally follows the shape of the defining polygon.
point. Therefore, for 4 control points, the degree of the polynomial is 3, i.e. cubic polynomial.
The direction of the tangent vector at the end points is same as that of the vector determined by first and
The convex hull property for a Bezier curve ensures that the polynomial smoothly follows the control
last segments.
No straight line intersects a Bezier curve more times than it intersects its control polygon.
points.
They are invariant under an affine transformation.
Bezier curves exhibit global control means moving a control point alters the shape of the whole curve.
A given Bezier curve can be subdivided at a point t=t0 into two Bezier segments which join together at the
point corresponding to the parameter value t=t0.
Page no: 6 Follow us on facebook to get real-time updates from RGPV
Downloaded from [Link]
Chameli Devi Group of Institutions
Department of Information Technology
B-Spline Curves
First, the number of specified polygon vertices fixes the order of the resulting polynomial which
The Bezier-curve produced by the Bernstein basis function has limited flexibility.
The second limiting characteristic is that the value of the blending function is nonzero for all
defines the curve.
parameter values over the entire curve.
The B-spline basis contains the Bernstein basis as the special case. The B-spline basis is non global.
A B-spline curve is defined as a linear combination of control points Pi and B-spline basis function Ni, k t given
by
{pi: i= , , …. } a e the o t ol poi ts
Where,
k is the order of the polynomial segments of the B-spline curve. Order k means that the curve is made
up of piecewise polynomial segments of degree k - 1,
the Ni, k t a e the o alized B-spli e le di g fu tio s . They a e des i ed y the o de k a d y
a non-decreasing sequence of real numbers normally called the “knot sequence”.
ti: i = 0, . . . n + K
The Ni, k functions are described as follows –
and if k > 1,
And
B-spli e u es ha e the follo i g p ope ties −
Properties of B-spline Curve
The sum of the B-spline basis functions for any parameter value is 1.
Each basis function is positive or zero for all parameter values.
Each basis function has precisely one maximum value, except for k=1.
The maximum order of the curve is equal to the number of vertices of defining polygon.
The degree of B-spline polynomial is independent on the number of vertices of defining polygon.
B-spline allows the local control over the curve surface because each vertex affects the shape of a curve
The curve exhibits the variation diminishing property.
only over a range of parameter values where its associated basis function is nonzero.
The curve generally follows the shape of defining polygon.
Any affine transformation can be applied to the curve by applying it to the vertices of defining polygon.
The curve line within the convex hull of its defining polygon.
Page no: 7 Follow us on facebook to get real-time updates from RGPV
We hope you find these notes useful.
You can get previous year question papers at
[Link] .
If you have any queries or you want to submit your
study notes please write us at
[Link]@[Link]