Graphics Output Primitive ch2-2021
Graphics Output Primitive ch2-2021
Part one
2-1 Plotting Point
If the two points used to specify a line are (x1,y1) and (x2,y2), then an
equation for the line is given as
Where
and
The above equation is called the slop intercept form of the line. The slope
m is the change in height (y2-y1) divided by the change in the width (x2-
x1) for two points on the line. The intercept b is the height at which the
line crosses the y-axis.
We can determine whether two lines are crossing or parallel. When two
lines cross, they share some point in common. Of course, that point
satisfies equations for the two lines. Let us see how to obtain this point.
Consider two lines having slops m1 and m2 and y-intercepts b1 and b2
respectively. Then we can write the equations for these two lines as,
Line 1 :
Line 2:
If point is shared by both lines, then it should satisfy the last two
equations
and
31 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
Substituting this into equation for line 1 or for line 2 gives,
The process of ‘turning on’ the pixels for a line segment is called vector
generation or line generation, and the algorithms for them are known as
vector generation algorithms or line drawing algorithms.
Before discussing specific line drawing algorithms it is useful to note the
general requirements for such algorithms. These requirements specify the
desired characteristics of line.
The line should appear as a straight line and it should start and end
accurately.
31 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
The line should be displayed with constant brightness along its
length independent of its length and orientation.
The line should be drawn rapidly.
Figure (2.1)
As shown in the figure, horizontal and vertical lines are straight and
have same width. The 45° line is straight, but its width is not constant.
31 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
On the other hand, the line with any other orientation is neither
straight nor has same width. Such cases are due to the finite resolution
of display and we have to accept approximate pixels in such situation.
The brightness of the line is dependent on the orientation of the line.
We can observe that the effective spacing between pixels for the 45°
line is greater than for the vertical and horizontal lines. This will make
the vertical and horizontal lines appear brighter than the 45° line.
Complex calculations are required to provide equal brightness along
lines of varying length and orientation.
31 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
Algorithm DDA
1. Read the line end points (𝑥1, y1) and (𝑥2, y2) such that they are not
equal. [If equal then plot that point and exit]
Length=Δ𝑥
Else
Length=Δ𝑦
End if
4. Δ𝑥 = (𝑥2 − 𝑥1)/𝑙𝑒𝑛𝑔𝑡ℎ
Δ𝑦 = (𝑦2 − 𝑦1)/𝑙𝑒𝑛𝑔𝑡ℎ
[This makes either Δ𝑥 or Δ𝑦 equal to 1 because length is either 𝑥2 − 𝑥1
or 𝑦2− 𝑦1 .Therefore, the incremental value for either x or y is one.]
Using the Sign function makes the algorithm work in all quadrants.
Example 2.1: consider the line from (0,0) to (6,6). Use the simple DDA
algorithm to rasterize this line
Solution:
i plot x y
0.5 0.5
1 (0,0)
1.5 1.5
2 (1,1)
2.5 2.5
31 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
3 (2,2)
3.5 3.5
4 (3,3)
4.5 4.5
5 (4,4)
5.5 5.5
6 (5,5)
6.5 6.5
Example 2.2: consider the line from (23,33) to (29,40) Use the simple
DDA algorithm to rasterize this line
Solution:
i x y Plot(x,y)
1. 23.5 33.5 (23,33)
2. 24.3 34.5 (24,34)
3. 25.1 35.5 (25,35)
4. 25.9 36.5 (25, 36)
5. 26.7 37.5 (26, 37)
6. 27.5 38.5 (27, 38)
7. 28.3 39.5 (28, 39)
Example 2.3: consider the line from (0,0) to (4,6). Use the simple DDA
algorithm to rasterize this line
Solution:
02 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
Advantages of DDA Algorithm
1. It is the simplest algorithm and it does not require special skills for
implementation.
2. It is a faster method for calculating pixel positions than the direct use of
equation y = mx + b. It eliminates the multiplication in the equation by
03 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
making use of raster characteristics, so that appropriate increments are
applied in the x or y direction to find the pixel positions along the line path.
Figure (2.2)
00 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
As shown in the Fig2.2, the line does not pass through all raster points (pixels).
It passes through raster point (0, 0) and subsequently crosses three pixels. It is
seen that the intercept of line with the line x = 1 is closer to the line y = 0, i.e.
pixel (1, 0) than to the line y = 1 i.e. pixel (1, 1). Hence, in this case, the raster
point at (1, 0) better represents the path of the line than that at (1, 1). The
intercept of the line with the line x = 2 is close to the line y = 1, i.e. pixel (2, 1)
than to the line y = 0, i.e.: pixel (2, 0).
Hence, the raster point at (2, 1) better represents the path of the line, as shown
in the Fig.2.2.
OR
Where
, and
Then according to value of e following actions are taken.
While ( )
{
y=y+1
01 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
x=x+1
2. And
}
01 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
x=x+1
8. i=i+1
9. If (i≤ ) then go to step 6
10. Stop
Example2.2:- Consider the line from (5, 5) to (13, 9), use the Bresenham's
algorithm to rasterize the line.
i plot x y e
5 5 0
1 (5,5) 6 6 -16
-8
2 (6,6) 7 6 0
3 (7,6) 8 7 -16
-8
4 (8,7) 9 7 0
5 (9,7) 10 8 -16
-8
6 (10,8) 11 8 0
01 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
7 (11,8) 12 9 -16
-8
8 (12,9) 13 9 0
9 (13,9) 14 10 -16
-8
The Bresenham’s algorithm only works for the first octant. The
generalized Bresenhm’s algorithm requires modification for lines lying in
the other octants. Such algorithm can be easily developed by considering
the quadrant in which the line lies and it slope. When the absolute
magnitude of the slope of the line is greater than 1, y is incremented by
one and Bresenham’s decision variable or error is used to determine when
to increment x. The x and y incremental vales depend on the quadrant in
which the line exists. This is illustrated in Fig 2.4.
01 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
Fig. 2.4. Conditions for Generalized Bresenham's algorithm
1. Read the line end points (x1, y1) and (x2, y2) such that they are not
equal.
[If equal then plot that point and exit]
2. and
4. )
)
5. If then
Begin
01 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
T= : = : =T : Interchange=1
End
Else
Interchange =0
End If
6.
[Initialize value of decision variable or error to compensate for non
zero intercepts]
7. i=1
[initialize counter ]
8. Plot (x,y)
9. While ( )
Begin
If (Interchange=1) Then x=x + s1
Else y= y + s2
End If
End while
11. i=i+1
12. if (i≤ ) then go to step 8
13. Stop
01 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
Example 2.3 : Draw the line from (0,0) to (-8,-4) using General
Bresenham algorithm
I Plot E x y
0 0 0
1 (0,0)
-16 0 -1
-8 -1 -1
2 (-1,-1)
0 -2 -1
3 (-2,-1)
-16 -2 -2
-8 -3 -2
4 (-3,-2)
0 -4 -2
5 (-4,-2)
-16 -4 -3
-8 -5
6 (-5,-3)
0 -6 -3
7 (-6,-3)
-16 -6 -4
-8 -7 -4
8 (-7,-4)
0 -8 -4
01 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
Part two
Circle ,Ellipse
Drawing
Algorithms
12 قسم علوم الحاسبات ( (2019-2020 مدرسة المادة -:د .فرح قيس عبد هللا
2-1 Circle –Generation Algorithms
A circle is define as the set of points that are all at a given distance r from
center position . This distance relationship is expressed by the
Cartesian coordinates as:
+ = Circle equation
) Eq.1
Firstly, the resulting circle has large gaps; the spacing between plotted
pixel positions is not uniform.
For x= :
Begin
Plot(x,y )
Plot(x,y )
End
2- Polar representation
10 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
Polar algorithm
(xc, yc) =center of the circle (integer);
r is the radius of the circle (integer);
r=30;
1- t = 0 : 1/r : 2*pi;
2- x = r * cos(t) + xc;
3- y = r * sin(t) + yc;
4- plot(x, y) ; grid Fig(2.5).
5- End
3-Eight-Way Symmetry
The first thing we can notice to make our circle drawing algorithm
more efficient is that circles centred at (0, 0 ) have eight-way
symmetry . If the point is (x,y)is on the circle , then we can trivially
compute seven other points on the circle , as shown in figure above.
Therfore, we need to compute only 45 segment to detrmine the circle
completely .
get point's symmetric complement about these lines by permuting the
indices :
{(x,-y), (-x,y), (-x,-y), (y,x), (y,-x), (-y,x),(-y,-x)}
11 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
Eight- way –symmetry circle Algorithm
(xc, yc ) =center of the circle( integer);
r is the radius of the circle (integer);
r=8;
th = 0 : 1/r : pi/4;
x = r * cos(th);
y = r * sin(th) ;
Plot (round( );
Plot (round( );
Plot (round( );
Plot (round( );
Plot (round( );
Plot (round( );
Plot (round( );
Plot (round( );
End
Let us assume point P in Fig. 2.7 as a last scan converted pixel. Now we have two
options either to choose pixel A or pixel B. The closer pixel amongst these two can be
determined as follows:
11 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
The distances of pixels A and B from the origin are given as
Now, the distances of pixels A and B from the true circle are given as
However, to avoid square root term in derivation of decision variable, i.e. to simplify
the computation and to make algorithm more efficient the defined as
From Fig.2.7, we can observe that is always positive and always negative.
Therefore, we can define decision variable as
and we can say that if < 0 , i.e. then only x is incremented; otherwise x
is incremented in positive direction and y is incremented in negative direction. In
other words we can write,
For <0
For 0 and
11 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
Algorithm to plot 1/8 of the circle
1- Read the radius (r) of the circle.
2- d=3-2r
[Initialize the decision variable]
3- x=0 , y = r
[Initialize starting point ]
4- do
{
Plot (x, y)
If (d < 0) then
11 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
d=d+ 4x+6
}
Else
{ d=d+4(x-y) +10
y=y-1
}
x=x+1
} While (x < y)
5- stop
The remaining part of circle can be drawn by reflecting point about y-
axis, x-axis and about origin as shown in Fig-2.7.
Therefore, by adding seven more plot commands after the plot command
in the step4 of the algorithm, the circle can be plotted. The remaining
seven plot commands are:
Plot (y, x)
Plot (y, - x)
Plot (x, - y)
Plot (-x, - y)
Plot (- y, - x)
Plot (- y, x) and
Plot (- x, y)
Example 2.4 :
11 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
2-5 Ellipse Drawing Algorithms
An ellipse is defined as the set of points such that the sum of the
distances from two fixed positions (foci) is the same for all points . If the
distances to the two foci from any point P = (x,y) on the ellipse are
labeled and , then the general equation of an ellipse can be stated as
+ = + = constant
Ellipse equations are greatly simplified if the major and minor axes are
oriented to align with the coordinate axes. In Fig.2.8, we show an ellipse
in "standard position" with major and minor axes oriented parallel to the
x and y-axes. Parameter for this example labels the semi major axis,
and parameter labels the semi minor axis. The equation of the ellipse
( =1
Using polar coordinates r and θ,we can also describe the ellipse in standard position
with the parametric equations:
x= + cos θ
y= + sin θ
11 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
1. START
2. Get the centre ( , ),x radius ( ) and y radius ( ) of the ellipse.
3. The polar co-ordinates on an ellipse are
x= + cos θ
y= + sin θ
4. Plot the point(x,y) corresponding to the values of θ where 0< = θ < =360
see Fig.(2.9)
5. STOP
12 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
Display of these curves can be generated with methods similar to
those discussed for the circle and ellipse functions.
13 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة
t = pi/4 : 1/30 : 3*pi/4;
x= * cos(t) + xc;
3- y = * sin(t) + yc;
4- plot(x, y) ;
5- End
10 (2019-2020 ( قسم علوم الحاسبات فرح قيس عبد هللا. د-: مدرسة المادة