Topic IV, V - SCAN CONVERSION OF GRAPHICS
PRIMITIVES
Unit Structure
2.0 Objectives
2.1 Introduction
2.2 Scan-Conversion of a Lines
2.3 Scan-Conversion of Circle and Ellipse
2.3.1 Digital Differential Analyzer Algorithm
2.3.2 Bresenham's Line-Drawing Algorithm
2.4 Drawing Ellipses and Other Conics
2.4.1 Bresenham's Method of Circle Drawing
2.4.2 Midpoint Circle Algorithm
2.5 Drawing Ellipses and Other Conics
2.0 OBJECTIVES
The objective of this topic is:
To understand the basic idea of scan conversion techniques.
To understand the algorithms for scan conversion of line, circle and other
conics.
2.1 INTRODUCTION
Scan conversion or rasterization is the process of converting the primitives
from its geometric definition into a set of pixels that make the primitive in image
space. This technique is used to draw shapes like line, circle, ellipse, etc. on the
screen. Some of them are discussed below:
2.2 SCAN – CONVERSION OF LINES
A straight line can be represented by a slope intercept equation as
where m represents the slope of the line and b as the y intercept.
If two endpoints of the line are specified at positions (x1,y1) and (x2,y2), the
values of the slope m and intercept b can be determined as
If ∆x and ∆y are the intervals corresponding to x and y respectively for a line,
then for given interval ∆x, we can calculate ∆y.
Similarly for given interval ∆y, ∆x can be calculated as:
For lines with magnitude |m| < 1, ∆x can be set proportional to a small horizontal
deflection and the corresponding horizontal deflection is set proportional to ∆y
and can be calculated as:
For lines with |m|>1, ∆y can be set proportional to small vertical deflection and
corresponding ∆x which is set proportional to horizontal deflection is calculated
using:
The following shows line drawn between points (x1, y1) and (x2, y2).
Figure 2.1 : A line representation in Cartesian coordinate system
ALGORITHM I
2.2.1 Digital Differential Analyzer (DDA) Algorithm
Sampling of the line at unit interval is carried out in one coordinate and
corresponding integer value for the other coordinate is calculated.
If the slope is less than or equal to 1( |m| ≤ 1), the coordinate x is sampled at
unit intervals (∆x = 1) and each successive values of y is computed as:
where k varies from 1 to the end point value taking integer values only. The
value of y calculated is rounded off to the nearest integer value.
For slope greater than 1 (|m| > 1), the roles of y and x are reversed, i.e., y is
sampled at unit intervals (∆y = 1) and corresponding x values are calculated as:
For negative value slopes, we follow the same procedure as above, only the
sampling unit ∆x and ∆y becomes ‘-1’ and;
Pseudo code
Pseudo code for DDA algorithm is as follows:
LineDDA(Xa, Ya, Xb, Yb) // to draw a line from (Xa, Ya) to (Xb, Yb)
{
Set dx = Xb - Xa, dy = Yb - Ya;
Set steps = dx;
SetX = Xa, Y = Ya;
int c = 0;
Call PutPixel(Xa, ya);
For (i=0; i <steps; i++)
{
X=X+1;
c = c + dy; // update the fractional part
If (c > dx)
{ // (that is, the fractional part is greater than 1
now
Y = y +1; // carry the overflowed integer over
c = c - dx // update the fractional part Call PutPixel(X, Y);
}
}
}
PRACTICAL CAT1: USE PYTHON/C/C++ TO IMPLEMENT THE DDA ALGORITHM
ABOVE, FOLLOWING THE ABOVE PSEDO CODE
NOTE: TO BE PRESENTED INDIVIDUALLY
ALGORITHM II
2.2.2 Bresenham’s Line Drawing Algorithm
This line drawing algorithm proposed by Bresenham, is an accurate and
efficient raster-line generating algorithm using only incremental integer calculations.
For lines |m| ≤ 1, the Bresenham’s line drawing algorithm
STEPS
I. Read the end points of the line and store left point in (x0, y0)
II. Plot (x0, y0), the first point.
III. Calculate constants ∆x, ∆y, 2∆y and 2∆y - 2∆x, and obtain a decision
parameter p0 as;
IV. Perform the following test for each xk, starting at k = 0:
If pk< 0, then next plotting point is (xk+1, yk)
and;
Otherwise, the next point to plot is (xk+1, yk+1)
and;
V. Repeat step 4 ∆x times.
NOTE: For a line with positive slope more than 1, the roles of the x and y
directions are interchanged.
PRACTICAL CAT2:
PART 1: OBTAIN A PSEUDO CODE FOR THE BRENSENHAM ALGORITHM
PART 2: USE PYTHONTO IMPLEMENT THE DDA ALGORITHM ABOVE, FOLLOWING
YOUR PSEDO CODE
PART 3: Compare DDA and Bresenham's line drawing algorithms
PART 4: Given a line with endpoints at (20, 10) and (30,18) use Brensenham’s line
drawing algorithm to generate the successive pixel positions. Let (X0, Y0=(20,10).
NOTE: TO BE PRESENTED INDIVIDUALLY
2.3 SCAN – CONVERSION OF CIRCLE AND ELLIPSE
A circle with centre (xc, yc) and radius r can be represented in equation form in three
ways:
2 2 2
Analytical representation: r = (x – xc) + (y – yc)
2 2 2
Implicit representation: (x – xc) + (y – yc) – r = 0
Parametric representation: x = xc + r cosθ
y = yc +ysinθ
A circle is symmetrical in nature. Eight – way symmetry can be used by reflecting each
o
point about each 45 axis. The points obtained in this case are given below with
illustration by figure.
P1 = (x, y) P5 = (-x, -y)
P2 = (y, x) P6 = (-y, -x)
P3 = (-y, x) P7 = (y, -x)
P4 = (-x, y) P8 = (x, -y)
Figure 2.2: Eight way symmetry of a circle
2.3.1 Bresenham’s circle drawing algorithm
Let us define a procedure Bresenham_Circle (Xc, Yc, R) procedure for Bresenham’s
circle drawing algorithm for circle of radius R and centre (Xc, Yc)
Bresenham_Circle (Xc,Yc, R)
{
Set X = 0;
Set Y= R;
Set D = 3 – 2R;
While (X < Y)
{
Call Draw_Circle (Xc, Yc, X, Y);
X=X+1;
if (D < 0)
{
D=D+4X+6;
}
else
{
Y=Y–1;
D=D+4(X–Y)+10;
}
Call Draw_Circle (Xc, Yc, X, Y);
}
}
Draw_Circle (Xc, Yc, X, Y)
{
Call PutPixel (Xc + X, Yc, +Y);
Call PutPixel (Xc – X, Yc, +Y);
Call PutPixel (Xc + X, Yc, – Y);
Call PutPixel (Xc – X, Yc, – Y);
Call PutPixel (Xc + Y, Yc, + X);
Call PutPixel (Xc – Y, Yc, – X);
Call PutPixel (Xc + Y, Yc, – X);
Call PutPixel (Xc – Y, Yc, – X);
}
2.3.2 Midpoint circle drawing algorithm
The midpoint circle drawing algorithm helps us to calculate the complete perimeter
points of a circle for the first octant. We can quickly find and calculate the points of
other octants with the help of the first octant points. The remaining points are the mirror
reflection of the first octant points.
This algorithm is used in computer graphics to define the coordinates needed for
rasterizing the circle. The midpoint circle drawing algorithm helps us to perform the
generalization of conic sections. Bresenham’s circle drawing algorithm is also
extracted from the midpoint circle drawing algorithm. In the algorithm, we will use the
8-way symmetry property.
In this algorithm, we define the unit interval and consider the nearest point of the circle
boundary in each step.
The decision parameter is based on a circle equation. As we know that the
equation of a circle is x2 +y2 =r2 when the centre is (0, 0).
Now let us define the function of a circle i.e.: fcircle(x,y)= x2 +y2 - r2
1. If fcircle < 0 then x, y is inside the circle boundary.
2. If fcircle > 0 then x, y is outside the circle boundary.
3. If fcircle = 0 then x, y is on the circle boundary.
The Decision parameter
pk =fcircle(xk+1,yk-1/2) where pk is a decision parameter and in this ½ is taken
because it is a midpoint value through which it is easy to calculate value
of yk and yk-1.
I.e. pk= (xk+1)2+ (yk-1/2)2-r2
If pk <0 then midpoint is inside the circle, in this condition we
select y is yk otherwise we will select next y as yk-1 for the condition of pk > 0.
1. If pk < 0 then yk+1=yk, by this the plotting points will be ( xk+1 ,yk). By this
the value for the next point will be given as:
Pk+1=pk +2(xk+1) +1
2. If pk > 0 then yk+1=yk-1, by this the plotting points will be (xk+1, yk-1). By this
the value of the next point will be given as:
Pk+1=pk+2(xk+1) +1-2(yk+1)
The Initial decision parameter
P0 = fcircle (1, r-1/2)
This is taken because of (x0, y0) = (0, r)
i.e. p0 =5/4-r or 1-r, (1-r will be taken if r is integer)
The algorithm steps
1. In this the input radius r is there with a centre (xc , yc).
To obtain the first point m the circumference of a circle is centered on the
origin as (x0,y0) = (0,r).
2. Calculate the initial decision parameters which are:
p0 =5/4-r or 1-r
3. Now at each xk position starting k=0, perform the following task.
if pk < 0 then plotting point will be ( xk+1 ,yk) and
Pk+1=pk +2(xk+1) +1
else the next point along the circle is (xk+1, yk-1) and
Pk+1=pk+2(xk+1) +1-2(yk+1)
4. Determine the symmetry points in the other quadrants.
5. Now move at each point by the given centre that is:
x=x+xc
y=y+yc
6. At last repeat steps from 3 to 5 until the condition x>=y!
(VIEW EXAMPLE IN THE ATTACHED POWERPOINT)
PRACTICAL CAT CONT:
PART 1: OBTAIN A PSEUDO CODE FOR THE CIRCLE ALGORITHM
PART 2: USE PYTHONTO IMPLEMENT THE CIRLE DRAWING ALGORITHM ABOVE
PART 3: Compare and contrast the Brensenham’s and the mid-point circle algorithm
PART 4: Use the mid-point circle algorithm to plot the first six points along the circle octants in
the first quadrant, given that the circle radius r=10.
2.3.3 Midpoint Ellipse Algorithm
This is a modified form of midpoint circle algorithm for drawing ellipse. The general
2 2 2 2 2 2
equation of an ellipse in implicit form is f (x, y) = b x + a y – a b = 0
Now the algorithm for ellipse follows as:
MidPoint_Ellipse( Xc, Yc, Rx, Ry)
{
/* Xc and Yc here denotes the x coordinate and y coordinate of the center of the ellipse
and Rx and Ry are the x-radius and y-radius of the ellipse respectively */
Set Sx = Rx * Rx;
Set Sy = Ry * Ry;
Set X = 0;
Set Y = Ry;
Set Px = 0;
Set Py = 2 * Sx * Y;
Call Draw_Ellipse (Xc, Yc, X, Y);
Set P = Sy – (Sx * Ry) + (0.25 * Sx);/* First Region*/ While ( Px<Py)
{
X=X+1;
Px = Px + 2 * Sy;
If (P < 0)
{P = P + Sy + Px;}
Else
{
Y=Y–1;
Py = Py – 2 * Sx;
P = P + Sy + Px – Py;
}
Call Draw_Ellipse (Xc, Yc, X, Y);
}
P = Sy * (X + 0.5)2 + Sx * (Y – 1)2 – Sx * Sy; /*Second Region*/
While (Y > 0)
{
Y=Y–1;
Py = Py – 2 * Sx;
If (P > 0)
{P = P + Sx – Py;}
Else
{
X=X+1;
Px = Px + 2 * Sy;
P = P + Sx – Py + Px;
}
Call Draw_Ellipse (Xc, Yc, X, Y);
}
}
Draw_Ellipse (Xc, Yc, X, Y)
{
Call PutPixel (Xc + X, Yc + Y);
Call PutPixel (Xc – X, Yc + Y);
Call PutPixel (Xc + X, Yc – Y);
Call PutPixel (Xc – X, Yc – Y);
}
2.4 DRAWING ELLIPSES AND OTHER CONICS
The equation of an ellipse with center at the origin is given as
Using standard parameterization, we can generate points on it as
Differentiating the standard ellipse equation we get
Now the DDA algorithm for circle can be applied to draw the ellipse.
Similarly a conic can be defined by the equation
If starting pixel on the conic is given, the adjacent pixel can be determined similar to
the circle drawing algorithm.
NOTE: READ ON THE MID POINT ELLIPSE DRAWING ALGORITHM
ASSIGNMENT:
1. Write down the equation of a standard ellipse.
2. Which scan conversion technique can be applied to draw an ellipse?
3. Explain how ellipse and other conics can be drawn using scan conversion
technique.