Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit II CG

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Unit II:

Output Primitives:
Points and Lines – Line Drawing Algorithms – Circle Generating Algorithms –Ellipse
Generating Algorithms – Filled Area primitives

Points and Lines:

LINE DRAWING ALGORITHMS


A line connects two points. It is a basic element in graphics. To draw a line, you need two end points
(x1,y1) and (x2,y2). The line segment is sampled at unit intervals in one coordinate and corresponding
integer values nearer to the line path are determined for other coordinates.

Equation of a line is given by:


y=mx+b
wheremistheslopeofthelineandbistheslope intercept.

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

• Get the input so two end points(X1,Y1)and(X2,Y2)


• Calculate the difference between two end points (dx and dy)
• Basedonthecalculateddifference,youneedtoidentifythenumberofstepstoputpixel.If
dx>dy, then you need more steps in x coordinate; otherwise in y coordinate.
• Calculate the increment in x coordinate and y coordinate.
• Putthepixelbysuccessfullyincrementingxandycoordinatesaccordinglyandcomplete the
drawing of the line.
Algorithm for DDA
Procedure line DDA (x1,y1,x2,y2:integer)

{
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

Eg2) Draw a line between (5,5)–(10,8)


x1= 5,y1= 5,x2=10,y2=8

𝑦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).

Bresenham line drawing algorithm


1. Input the two-line end-points, storing the left end-point in (x0,y0)
2. Plot the point(x0,y0)
3. Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get the first value for the
decision parameter as:
P0=2dy–dx
4. At each xk along the line, starting at k=0,performthefollowingtest:
Ifpk<0, the next point to plot is
(xk+1,yk)and pk+1= pk + 2dy
Otherwise, the next point to plot is
(xk+1,yk+1)and pk+1=pk+2dy–2dx
Repeatstep4(dx) times
Detailed algorithm

Procedure lineBresenham (x1, y1, x2, y2 : integer)var


dx,dy,x,y,xend,p:integer;
{
dx=abs(x1-x2);
dy=abs(y1-y2);
p=2*dy-dx;
if(x1>x2)then
{
x=x2;y=y2;
xend=x1;
}
else
{
x=x1; y=y1;
xend=x2;
}

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

Features Digital Differential AnalyzerLine Bresenham’s Line Drawing


Drawing Algorithm Algorithm
Arithmetic DDA algorithm uses floating points i.e. Bresenham’s algorithm uses fixed
Real Arithmetic. Points i.e. Integer Arithmetic
Operations DDAalgorithmusesmultiplicati onand Bresenham’salgorithmusesonlys
division in its operations. ubtraction and addition in its
operations.
Speed DDA algorithm is rather slowlythan Bresenham’s algorithm is faster
Bresenham’salgorithminlinedra wing than DDA
becauseitusesrealarithmetic(flo ating algorithminlinedrawingbecauseitper
point operations) forms
onlyadditionandsubtractioninitscalc
ulation and uses only integer
arithmetic so it runs significantly
faster.
Accuracy DDAalgorithmisnotasaccuratea nd Bresenham’salgorithmismoreefficie
& efficient as Bresenham’s algorithm ntand much accurate than DDA
Efficiency algorithm.
Drawing DDAalgorithmcandrawcirc lesand Bresenham’salgorithmcandrawci
curvesbutthatarenotasaccur ateas rclesand
Bresenham’salgorithm curveswithmuchmoreaccuracyth
anDDA algorithm.

Round off DDAalgorithmroundoffthecoor dinates Bresenham’salgorithmdoesnotround


to integer that is nearest buttakes the incremental value in its
to the line operation.
Expensive DDAalgorithmusesanenormous Bresenham’salgorithmislessexpensi
number of floating-point vethan DDA algorithm as it uses
multiplications so it is expensive only addition and subtraction.
MidPointSubdivisionalgorithmforCircle
The mid-point circle drawing algorithm is an algorithm used to determine the points needed for
generatingacircle.Weusethemid-pointalgorithmtocalculatealltheperimeterpointsofthe circle in the first
octant and then print them along with their mirror points in the other octants. This will work because a
circle is symmetric about it’scentre.

Fig1.338waysymmetryfordrawingcircle The equation for a circle is:


x2 +y2=r2
Where r is the radius of the circle. So, we can write a direct circle drawing algorithm bysolving the
equation for y at unit x intervals using:
y= (r2–x2)1/2
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(0, 0). Then
each calculatedposition(x,y)ismovedtoitsproperscreenpositionbyaddingxctoxandyctoy.
Assuming we have just plotted point (xk, yk), we next need to determine whether the pixel at
position (xk+1, yk) or the one at position (xk+1, yk–1) is closer to the circle path. Our decision
parameter is The circle function evaluated at the midpointbetween these two pixels:
The relative position of anypoint (x, y) can be determined bychecking the sign of the ontheboundary of
the circle function:
<0 If(x,y)isinsidethecircleboundary
fcircle(x,y)=0 If(x,y)is onthecircleboundary
>0 If(x,y)isoutsidethecircleboundary
The circle function tests are performed for the midpoints positions between pixels near the circle path
at each sampling step. Thus, circle function is decision parameter in the midpoint algorithm, andwe can
setupincremental calculationsfor this function as we didin theline algorithm.

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.

If pk < 0,the next point along the circle centred on (0, 0) is


(xk+1 ,yk)and pk+1= pk + 2xk+1+ 1
Otherwisethenextpointalongthecircleis

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

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

The advantages of Mid Point Circle Drawing Algorithm are-


• It is a powerful and efficient algorithm.
• The entire algorithm is based on the simple equation ofcircleX2+Y2=R2.
• It is easy to implement from the programmer’sperspective.
• Thisalgorithmisusedtogeneratecurvesonrasterdisplays.

The disadvantages of MidPoint Circle Drawing Algorithm are-


• Accuracyofthegeneratingpointsisanissueinthisalgorithm.
• Thecirclegeneratedbythisalgorithmisnotsmooth.
• Thisalgorithmistimeconsuming.

You might also like