Computer Graphics
جامعة الكوفة
كلية علوم الحاسوب والرياضيات
م .د علي عبدالكريم حبيب
Arbitrary Lines
Drawing lines with arbitrary slope creates several
problems, such as:
1- The display screen can be illuminated only at pixels locations;
therefore a raster scan display has a staircase effect that only
approximates the actual line as shown in figure below:
Arbitrary Lines
Although it may not be possible to choose pixels that lie on
the actual line, we want to turn on pixels lying closest to it.
For example, in previous figure, the pixel at location B is a
better choice than the one at location A.
Arbitrary Lines
2- Determining the closest (best) pixels is not easy. Different
algorithms calculate different pixels to make up the approximating
line.
The choice of algorithm depends on:
1- The speed of line generation.
2- The appearance of the line.
Therefore, to understand these criteria better let's look at several
different line generating algorithms.
1- Direct method :
If the two endpoints used to specify a line are (X1,Y1) and (X2,Y2) , then the
equation of the line is used to describe the X , Y coordinates of all the pointes
that lie between these two endpoints
The equation of the straight line is :
Y= M * X + B
Where ( M ) is the slope of the line
M= Δ Y / Δ X
and ( B ) is the Y intercept.
The Y values of the pointes of the line can be calculated using the above
equation, by incrementing X values from X1 to X2 and substitute it in the line
equation.
Note : The slope between any point (X,Y) on the line and (X1,Y1) is the same as
the slope between (X2,Y2) and (X1,X2)
Algorithm (1):
1- Input: X1,Y1, X2, Y2.
2- dx=x2-x1; dy=y2-y1;
m=dy/dx; b= y1 – m * x1 ;
for x= X1 To X2 // when x1= X2 stop
{
y=m * x + b;
plot (x , y );
} }
H.W.: Write complete program in order to draw arbitrary line using
direct method?
2. Simple DDA (Digital Differential Analysis) Algorithm
The DDA algorithm generates lines from their differential equations.
We calculate the length of the line in the X direction ( number of pointes) by the
equation :
ABS (X2-X1)
and calculate the length of the line in the Y direction ( number of pointes) by the
Equation :
ABS (Y2-Y1)
Where ABS is a function takes the positive of the arguments.
The Length estimates is equal to the larger of the magnitudes of the above two
.equations
The increment steps ( dX and dY ) are used to increment the X
and Y coordinates for the next pointes to be plotted
Algorithm (DDA) :
Start
If ABS( X2-X1) > ABS (Y2-Y1) Then
Length=ABS (X2-X1)
Else
Length=ABS (Y2-Y1)
dX = (X2-X1) / Length dY = (Y2-Y1) / Length
X=X1 Y=Y1
For I=1 to Length
Begin
Plot(Int(X) , Int (Y))
X=X+dX Y=Y+dY
End
Finish
A simple algorithm for DDA is clarified in algorithm (2), assuming
that:
1- Round is a function approximates float (real) numbers to
nearest larger number.
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
H.W / trace the Bresenham algorithm to draw a line between the
.two points (50, 65), (59, 68)
Thank you