Raster Scan Graphics
(Marks
18)
Basic concepts in line drawing:
We say these computers have vector graphics capability (the line is a vector). The
process is turning on pixels for a line segment is called vector generation and
algorithm for them is known as vector generation algorithm.
Problems of vector generation:
Line is straight but width is no constant.
Brightness of line dependent on orientation of the line.
Calculate only an approximate linelength.
Reduce the calculations using simple integer arithmetic.
Line Drawing Algorithms
1. DDA Algorithm (Digital Differential Analyzer)
Step1: Start Algorithm
Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.
Step3: Enter value of x1,y1,x2,y2.
Step4: Calculate dx = x2-x1
Step5: Calculate dy = y2-y1
Step6: If ABS (dx) > ABS (dy)
Then step = abs (dx)
Else
Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
Step8: Set pixel (x, y)
Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x2
Raster Scan Graphics
(Marks
18)
C Program:
#include<stdio.h
>
#include<conio.h
>
#include<graphics.
h> void main()
int
x1,y1,x2,y2,dx,dy,length,i
; float x,y,xinc,yinc;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter thestarting coordinates");
scanf("%d%d",&x1,&y1);
printf("Enter the ending coordinates");
scanf("%d%d",&x2,&y2);
dx=x2-
x1;
dy=y2-
y1;
if(abs(dx)>abs(dy))
length=abs(dx)
; else
length=abs(dy)
Raster Scan Graphics
(Marks
18)
;
xinc=dx/(float)length;
Raster Scan Graphics
(Marks
18)
yinc=dy/
(float)length; x=x1
y=y1
putpixel(x,y,10);
for(i=0;i<length;i+
+)
putpixel(x,y,10
); x=x+xinc;
y=y+yinc;
delay(10);
getch();
closegraph();
Advantages of DDA Algorithm
1. It is the simplest algorithm and it does not require special skills for
implementation.
[Link] is a faster method for calculating pixel positions .
Disadvantages of DDA Algorithm
1. Floating point arithmetic in DDA algorithm is stilltime-consuming.
2. The algorithm is orientation dependent. Hence end point accuracy ispoor.
Example Consider the line from (0, 0) to (-6, -6). Use the simple DDA algorithm line.
Sol. Evaluating steps 1 to 5 in the DDA
algorithm we have Xl = 0 Y1 = 0
Raster Scan Graphics
(Marks
18)
X2 = - 6 Y2 = - 6
Length = l X2-X1l |Y2-Y1l = 6
Raster Scan Graphics
(Marks
18)
∆X = ∆Y = -1
Initial values for
X = 0 + 0.5 * Sign (-1) =-0.5
Y = 0 + 0.5 * Sign (-1) =-0.5
Tabulating the results of each iteration in the step 6 we get,
2. Bresenham's Algorithm.
It’s an accurate and efficient raster line generating Algorithm. Bresenham's line-
drawing algorithm uses an iterative scheme. A key feature of the algorithm is
that it requires only integer data and simple arithmetic. This makes the
algorithm very efficient andfast.
Procedure for Bresenham’s algorithm:
[Link] line end points (x1,y1) and (x2,y2) such that they are not equal.
2. dx=|x2-x1| and dy=|
y2-y1| 3.x=x1 and y=y1
4.e=2*dy-
dx 5.i=1
Raster Scan Graphics
(Marks
18)
[Link] (x,y)
[Link]
(e>0)
x=y+1
e=e-
2*dx
x=x+1
e=e+2*dy
8.i=i+1
9. if(i<dx) then goto step 6.
10. Stop.
Program- Bresenham Line Drawing Algorithm -:
#include<stdio.h
>
#include<conio.h
>
#include<graphics.h
>
#include<math.h
> void main()
{
int
gd=DETECT,gm=DETECT,length,dx,dy,x1,y1,x2,y2,
e; float x,y;
clrscr();
printf("\n enter coordinates of
x1,y1,x2,y2"); scanf("%d%d%d
%d",&x1,&y1,&x2,&y2); dx=x2-x1;
dy=y2-y1;
Raster Scan Graphics
(Marks
18)
e=2*(dy-
dx); x=x1;
y=y1;
initgraph(&gd,&gm,"c://Turboc3//B
GI"); while(x<=x2)
{
if(e<0)
Raster Scan Graphics
(Marks
18)
{
x=x+1;
y=y;
e=e+2*d
y;
}
else
{
x=x+1;
y=y+1;
e=e+2*(dy-dx);
}
putpixel(x,y,15);
}
getch();
closegraph();
}
Problem: - Consider the line from(5, 5) to (13, 9). Use the Bresenham's algorithm
to rasterize the line.
Solution :-
Evaluating steps 1 through 4 in the Bresenham’s algorithm we have,
Given: -
step 1 :- x1 = 5 , y1 = 5 and x2 =
13 , y2 = 9 . step 2 :- Δx = | 13 - 5|
=8 Δy = | 9 - 5 | = 4 step 3 :- x = 5
,y=5
step 4 :- e = 2 * Δy - Δx = 2 * 4 - 8 = 0 .
Tabulating the results of each iteration in the step 5 through 10.
Raster Scan Graphics
(Marks
18)
DDA Line Drawing Algorithm vs Bresenhams Line Drawing
Algorithm
Arithmetic uses floating points Integer points
Operations Uses multiplication anddivision in its Usesonlysubtractionandadditi
operations. on in its operations.
Speed slowly than Bresenhams algorithm faster than DDA
Accuracy not as accurate and efficient more efficient &much accurate
&
Efficienc
y
Drawing DDA algorithm can drawcircles and Draw circles and curves with
curves but that are not as accurate much more accuracy than DDA
as Bresenhams algorithm. algorithm.
Round Off DDA algorithm round off the Bresenhams algorithm does
coordinates to not round off but takes the
Raster Scan Graphics
(Marks
18)
integer that is nearest to the line. incremental value in its
operation.
Expensive expensive. less expensive
Circle generating algorithm:
Symmetry of circle:
A circle is a geometric figure which is round, and can be divided into 360 degrees. A
circle is a symmetrical figure which follows 8-way symmetry.
8- Way symmetry: Any circle follows 8-way symmetry. This means that for every
point (x,y) 8 points can be plotted. These (x,y), (y,x), (-y,x), (-x,y), (-x,-y), (-y,-x),
(y,-x), (x,-y).
Drawing a circle: To draw a circle we need two things, the coordinates of the
centre and the radius of the circle.
Here r is the radius of the circle. If the circle has origin (0,0) as its centre then the
above equation can be reduced to x2 + y2 =r2
Bresenham’s Algorithm
We cannot display a continuous arc on the raster display. Instead, we have to choose
the nearest pixel position to complete the arc.
From the following illustration, you can see that we have put the pixel at (X, Y)
location and now need to decide where to put the next pixel − at N (X+1, Y) or at S
Raster Scan Graphics
(Marks
18)
(X+1, Y-1).
Raster Scan Graphics
(Marks
18)
This can be decided by the decision parameter d.
If d <= 0, then N(X+1, Y) is to be chosen as next pixel.
If d > 0, then S(X+1, Y-1) is to be chosen as the next pixel.
Breshenham’s Algorithm For Circle Drawing
1. Read the radius ofcircle.
2. Calculate initial decision parameter di=3-2r
3. x=0 and y=r [initialize the starting point]
4. Do
{
5. Plot(x,y)//plot point(x,y) and other 7 points in
all octant if(di<0)then
di=di+4x=6
else
{
di=di+4(x-y)
+10 y=y-1
}
x=x+1
}while(x<y);
[Link]
Raster Scan Graphics
(Marks
18)
Program : bresenhams circle drawing
algorihm #include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h
> void
bressn(int,int,int); void
plot(int,int,int,int);
void plot(int xc,int yc,int x,int y)
putpixel(xc+x,yc+y,WHIT
E); putpixel(xc-
x,yc+y,RED);
putpixel(xc-x,yc-
y,GREEN);
putpixel(xc+x,yc-
y,YELLOW);
putpixel(xc+y,yc+x,WHIT
E); putpixel(xc-
y,yc+x,GREEN);
putpixel(xc-y,yc-
x,YELLOW);
putpixel(xc+y,yc-
x,RED);
Raster Scan Graphics
(Marks
18)
}
void bressn(int r,int xc,int yc)
int d,x,y;
d=3-(2*r);
x=0;
y=r;
Raster Scan Graphics
(Marks
18)
while(x<y)
plot(xc,yc,x,y)
; x=x+1;
if(d<0)
d=d+(4*x)+6;
else
y=y-1;
d=d+4*(x-y)+10;
plot(xc,yc,x,y);
void main()
int gd,gm;
int xc,yc,radius;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\
bgi"); printf("\n enter
xc,yc,radius"); scanf("%d%d
%d",&xc,&yc,&radius);
Raster Scan Graphics
(Marks
18)
bressn(xc,yc,radius);
Raster Scan Graphics
(Marks
18)
getch();
closegraph();
Example :
Calculate the pixel position along the circle path with radius r=10 centered on the
origin using Bresenham’s circle drawing algorithm from point (0,10) to point x=y.
Solution:
The value of d
is d=3-2r=3-
2*10=-17
Tabulating results of step 4
x y d
0 1 -
0 17
1 1 -
0 11
2 1 -1
0
3 1 13
0
4 9 -5
5 9 17
6 8 11
7 7 13