computer graphics 2
computer graphics 2
جامعة الكوفة
كلية علوم الحاسوب والرياضيات
m=dy/dx; b= y1 – m * x1 ;
for x= X1 To X2 // when x1= X2 stop
{
y=m * x + b;
plot (x , y );
} }
Ex/: trace the simple DDA algorithm to draw a line between the
two points (23,33), (29,40). X Y Plot(X) Plot(y)
Sol/: dx=29-23=6
23 33 23 33
dy=40-33=7
Length=7 23.857 34 24 34
Xinc=6/7 =0.857
24.714 35 25 35
Yinc=7/7=1.
25.571 36 26 36
26.429 37 26 37
27.286 38 27 38
28.143 39 28 39
NOTE:
The DDA algorithm is faster than the direct use of the line
equation since it calculates points on the line without any
floating point multiplication. However, a floating point addition
is still needed in determining each successive point.
Furthermore, cumulative error due to limited precision in the
floating point representation may cause calculated points to
drift away from their true position when the line relatively
long.
H.W. Example 1 : Consider the line from (0,0) to (5,5) Use DDA to
rasterize the line.
3.Bresenham’s algorithm
- Developed by Bresenham J.E.
Bresenham algorithm seeks to select the optimum raster locations to
represent a straight line. To accomplish this the algorithm always increment by
one unit in either X or Y depending on the slop of the line. The slop of the line
represent the error between the location of the real line and the location of the
drawn line.
The algorithm is cleverly constructed so that only the sign of this error need be
.examined
3.Bresenham’s
Algorithm (3):
algorithm
Start
X=X1 Y=Y1
DX=X2-X1 DY=Y2-Y1
E= (DY / DX) – 0.5
For I=1 to DX
Begin
Plot (X,Y)
While (E ≥ 0 )
Y=Y+1 E=E-1
End While
X=X+1 E=E + ( DY / DX )
End
Finish
Example 3 : Consider the line from (0,0) to (5,5) Rasterize the line with
Bresenham algorithm Sol 3