Unit II CG
Unit II CG
Unit II CG
Output Primitives:
Points and Lines – Line Drawing Algorithms – Circle Generating Algorithms –Ellipse
Generating Algorithms – Filled Area primitives
b=y-mx
Considering a line with positive slope, if the slope is less than or equalto1,wesampleat unit x intervals
(dx=1) and compute successive y values as :
Yk+1=Yk+m
Subscript ktakesintegervaluesstartingfrom0, for the1st point and increases a long x axis in unit
intervalsuntil the endpoint is reached. Y is rounded off to a nearest integer to correspond to a screen
pixel.
Similarly, if the slope is greater than or equal to 1,we sample at unit y intervals (dy=1)and compute
xin successive manner as :
Xk+1=Xk+1/m Digital Differential Analyzer (DDA) line drawing algorithm
Digital Differential Analyzer (DDA) algorithm is the simple line generation algorithm which is
explained step by step here
{
var
dx,dy,steps,k:integer;xinc,
yinc, x,y : real; dx = x2-x1;
dy=y2-y1;
ifabs(dx)>abs(dy)then steps
= abs (dx)
else steps=abs(dy);
x in c = dx/steps; yinc =
dy/steps; x=x1;
y=y1;
Set pixel(round(x),round(y),1);
for(i=1;i<=steps;i++)
{
x = x + xinc;y=y+yi
nc;
Set pixel (round(x),round(y),1); } }
Eg1)Drawalinebetween(5,5)–(10,10)
x1=5,y1=5, x2=10, y2=10
𝑦2−𝑦1 m= 10−5
= =1
𝑥2−𝑥1 10−5
If dx>dy
steps=abs(dx) elsesteps=abs(dy)
From this example, both dx and dy are equal. steps=
abs(dy)
X inc=dx/steps=5/5=1 Y inc = dy/steps
=5/5 = 1
Increment both x and y using x in c and y in c for steps number of times and the points generated are given
intable below and it is also plotted in Fig. 1.31.
K Xk Yk
0 5 5
1 6 6
2 7 7
3 8 8
4 9 9
5 10 10
Fig1.31LinegeneratedusingDDAalgorithm
𝑦2−𝑦1 8−5
m= =
=.6
𝑥2−𝑥1 10−5
From the above equation dy > dx : Hence yinc = .6 and xinc = 1 and the pointsgenerated
are shown in table below and itis plotted in Fig. 1.32.
K Xk Yk
0 5 5
1 6 5.6
2 7 6.2
3 8 6.8
4 9 7.4
5 10 8
Fig1.32LinegeneratedusingDDAalgorithm
Advantages:
1. It is the simplest algorithm and does not require special skills for implementation
2. Fastest method for calculating pixel positions
3. Involves floating point arithmetic
Bresenham line drawing algorithm
Thebigadvantageofthisalgorithmisthatitusesonlyintegercalculations.
The main idea of the Bresenham’s line drawing algorithm: Move across the x-axis in unit intervals
and at each step choose between two different y coordinates. For example from position (2,3) we
have to choose between (3,3) and(3,4), we would like the point that is
closer to the original line.
Deriving The Bresenham Line Algorithm
To illustrate Bresenham’s approach, we first consider the scan conversion process for lineswith
positive slope less than 1 (m<1).
Pixel positions along a line path are determined by sampling at unit x intervals. Starting from the
left endpoint (x0, y0) of a given line, we step to each successive column (x position) and
demonstrate the kth step in this process.
Assuming we have determined that the pixel at (xk, yk) is to be displayed, we next need to decide
which pixel to plot in column xk+1. Our choices are the pixels at positions (xk+1, yk) and (xk+1,
yk+1).
putpixel(x,y,4);
while(x<=xend)
{
x=x+1;
if(p<0)then
p=p+2*dy;
else
y=y+1; p=p+2*(dy-dx);
putpixel(x,y,4);
}
Eg.,Draw a line between (20,10) and (30,18)
}
dx =10,dy=8
The initial decision parameter has the value P0 = 2dy–dx = 6
The increments for calculation successive decision parameters are:
2dy = 16
2dy–2dx=–4
We plot the initial point (20, 10) , and determine successive pixel positions along the linepath from
the decision parameters as:
DifferencebetweenDDALineDrawingAlgorithmandBresenhamsLineDrawingAlgorithm
1. Input radius r and circle centre (xc, yc), and obtain the first point on the circumference of
acircle centred on the origin as:
(x0,y0)=(0,r)
2. Calculatetheinitialvalueofthedecisionparameteras:
P0=5/4–r (or) p=1-r
3. AteachpositionxkStartingwithk=0,perform thefollowingtest.
4. Determinesymmetrypointsintheothersevenoctants
5. Move each calculated pixelposition (x, y) onto the circular path centred at(xc, yc) and
plotthe coordinate values:
x=x+xcandy=y+yc
6. Repeatsteps3to5untilx>=y
eg.,) Given a circle withradius=10, we demonstrate the midpoint circle algorithm bydetermining
positions along the circle octant in the first quadrant from x = 0 to x = y. the initial value of the decision
parameter is P0= 1 – r = – 9
For the circle centred on the origin, the initial point is (x0, y0) = (0, 10), and the initialincrement
term for calculating the decision parameters are
2x0=0and2y0=20
Successive decision parameters values and positions along the circle path are calculatedusing midpoint
algorithm as:
Fig1.34Aplotofthegeneratedpixelpositionsinthefirstquadrant