Chapter Two Graphics Output Primitives
Chapter Two Graphics Output Primitives
Example: A line has a starting point (1,7) and ending point (11,17). Apply the Digital Differential Analyzer
algorithm to plot a line.
Δx = x2 – x1 = 11-1 = 10
Δy = y2 – y1 = 17-7 = 10
m = Δy/Δx = 10/10 = 1
Step 2: Now, we calculate the number of steps.
Δx = Δy = 10
3 9 (3, 9)
4 10 (4, 10)
5 11 (5, 11)
6 12 (6, 12)
7 13 (7, 13)
8 14 (8, 14)
9 15 (9, 15)
10 16 (10, 16)
11 17 (11, 17)
Step 4: We will repeat step 3 until we get the endpoints of the line.
Step 5: Stop.
This algorithm was introduced by “Jack Elton Bresenham” in 1962. This algorithm helps us to perform
scan conversion of a line. It is a powerful, useful, and accurate method. We use incremental integer
calculations to draw a line. The integer calculations include addition, subtraction, and multiplication.
In Bresenham’s Line Drawing algorithm, we have to calculate the slope (m) between the starting point and
the ending point.
As shown in the above figure let, we have initial coordinates of a line = (xk, yk) The next coordinates of a
line = (xk+1, yk+1)
The intersection point between yk and yk+1 = y Let we assume that the distance between y and yk = d1
The distance between y and yk+1 = d2 ,Now, we have to decide which point is nearest to the intersection
point.
xk+1 = xk +1
yk+1 = yk
Case 2: If
pk >= 0
Then
pk+1 =pk +2Δy-2Δx
xk+1 =xk +1
yk+1 =yk +1
Step 6: We will repeat step 5 until we found the ending point of the line and the total
number of iterations =Δx-1.
Step 7: Stop.
Example: A line has a starting point (9,18) and ending point (14,22). Apply the
Bresenham’s Line Drawing algorithm to plot a line.
Solution: We have two coordinates,
Starting Point = (x1, y1) = (9,18)
Ending Point = (x2, y2) = (14,22)
Step 1: First, we calculate Δx, Δy.
Δx = x2 – x1 = 14-9 = 5
Δy = y2 – y1 = 22-18 = 4
Step 2: Now, we are going to calculate the decision parameter (pk)
pk = 2Δy-Δx
=2x4–5=3
The value of pk = 3
Step 3: Now, we will check both the cases.
If
pk >= 0
Then
Case 2 is satisfied. Thus
pk+1 = pk +2Δy-2Δx =3+ (2 x 4) - (2 x 5) = 1
xk+1 =xk +1 = 9 + 1 = 10
yk+1 =yk +1 = 18 +1 = 19
Step 4: Now move to next step. We will calculate the coordinates until we reach the end
point of the line.
Δx -1 = 5 – 1 = 4
9 18
3 1 10 19
1 -1 11 20
-1 7 12 20
7 5 13 21
5 3 14 22
Step 5: Stop.
The Mid-point Subdivision algorithm is the extension of the Cyrus-Beck algorithm. The Mid-Point
line plotting algorithm was introduced by “Pitway and Van Aken.” It is an incremental line
drawing algorithm. In this algorithm, we perform incremental calculations. The calculations are
based on the previous step to find the value of the next point. We perform the same process for
each step. By using the mid-point subdivision algorithm, we can draw a line with close
approximation between two points. The mid-point subdivision line drawing algorithm subdivides
the line at its midpoints continuously.
The Mid-point Subdivision algorithm helps to compute or calculate the visible areas of lines that
appear in the window. This line plotting algorithm follows the bisection method to divide the line
into equal partitions.
• Step 1: Start.
• Step 2: Consider the starting point as (x1, y1) and ending point as (x2, y2).
• Step 3: Now, we will calculate Δd.
Δd = 2 (Δy -Δx)
• Step 4: Now, we will calculate the decision parameter di with the following formula.
di = 2Δy - Δx
• Step 6: We will repeat step 5 until we found the ending point of the line.
• Step 7: Stop.
Example: A line has a starting point (6,10) and ending point (13,17). Apply the Mid-point
Line Drawing algorithm to draw a line. (Exercise). The final answers, Coordinates of drawn
lines are- P1 = (6, 10), P2 = (7, 11), P3 = (8, 12), P4 = (9, 13), P5 = (10, 14), P6 = (11, 15), P7 =
(12, 16), P8 = (13, 17).
A circle is an eight-way symmetric shape. All quadrants of a circle are the same. There are two octants in
each quadrant of a circle. If we know the value of any point, then we can easily calculate the values of the
remaining seven points by using the eight-way symmetry method. A circle is a shape consist of a line called
the circumference. All the straight lines drawn from a particular point within the circle to the circumference
are always equal. A circle is a round shape that has no corner or sides.
If we want to draw a circle, then we will consider it by its origin point. Let us assume a point
P1(R, S) then we can represent the other seven points as follows-P2(R, -S), P3(-R, -S), P4(-R, S),
P5(S, R), P6(S, -R), P7(-S, -R), P8(-S, R).
Example: Let, we have taken a point (4, 6) of a circle. We will calculate the remaining seven
points by using of reflection method as follows- The seven points are – (4, -6), (-4, -6), (-4, 6),
(4, 6), (4, -6), (-4, -6), (-4, 6)
dk+1 = dk + 4xk+1 + 6
Case 2: If
dk >= 0
then
xk+1 =xk + 1
yk+1 =yk –1
dk+1 = dk + 4(xk+1 – yk+1)+ 10
Step 6: If the center coordinates (x1, y1) is not at the origin (0, 0), then we will draw the
points as follow-
X coordinate = xc + x1
y coordinate = yc + y1 {xc and yc represents the current value of x and y coordinate}
Step 7: We repeat step 5 and 6 until we get x>=y.
Step 8: Stop.
Example: The radius of a circle is 8, and center point coordinates are (0, 0). Apply Bresenham’s
circle drawing algorithm to plot all points of the circle.
Solution:
Step 1: The given stating points of the circle (x1, y1) = (0, 0)
Radius of the circle (r) = 8
Step 2: Now, we will assign the starting point (x1, y1) as follows-
x1 = 0
y1 = r (radius) = 8
Step 3: Now, we will calculate the initial decision parameter (d0)
d0 = 3 – 2 x r
d0 = 3 – 2 x 8
d0 = -13
Step 4: The value of initial parameter d0 < 0. So, case 1 is satisfied.
Thus,
xk+1 =xk + 1 = 0 + 1 = 1
yk+1 =yk = 8
dk+1 = dk + 4xk+1 + 6 = -13 + (4 x 1) + 6 = -3
Step 5: The center coordinates are already (0, 0) so we will move to next step.
Step 6: Follow step 4 until we get x >= y.
dk dk+1 (xk+1,yk+1)
(0, 8)
-13 -3 (1, 8)
-3 11 (2, 8)
11 5 (3, 7)
5 7 (4, 6)
7 (5, 5)
Now, we will calculate the coordinates of the octant 2 by swapping the x and y coordinates.
(0, 8) (5, 5)
(1, 8) (6, 4)
(2, 8) (7, 3)
(3, 7) (8, 2)
(4, 6) (8, 1)
(5, 5) (8, 0)
Thus, we will calculate all points of the circle with respect to octant 1.
Quadrant 1 (x, y) Quadrant 2 (-x, y) Quadrant 3 (-x, -y) Quadrant 4 (x, -y)
• The plotted points are less accurate than the midpoint circle drawing.
• It is not so good for complex images and high graphics image
dk+1 = dk + 2 pk+1 + 1
Case 2: If
dk >= 0
then
pk+1 =pk + 1
qk+1 =qk –1
dk+1 = dk - 2 (qk+1 + 2 pk+1)+ 1
Step 6: If the center coordinate point (p0, q0) is not at the origin (0, 0) then we will
draw the points as follow-
For x coordinate = xc + p0
For y coordinate = yc + q0 {xc and yc contains he current value of x and y
coordinate}
Step 7: We repeat step 5 and 6 until we get x>=y.
Step 8: Stop.
Example: The center coordinates are (0, 0), and the radius of the circle is 10. Find all
points of the circle by using the midpoint circle drawing algorithm?
Solution:
Step 1: The given center coordinates of the circle (p0, q0) = (0, 0)
Radius of the circle (r) = 10
Step 2: Now, we will determine the starting coordinates (p0, q0) as follows-
P0 = 0
q0 = r (radius) = 10
Step 3: Now, we will determine the initial decision parameter (d0)
d0 = 1 –r
d0 = 1 –10
d0 = -9
Step 4: The initial parameter d0 < 0 then, case 1 is satisfied.
pk+1 =pk + 1 = 0 + 1 = 1
qk+1 =qk = 10
dk+1 = dk + 2 pk+1 + 1 = -9 + 2(1) + 1 = -6
Step 5: The center coordinates of circle are already (0, 0). So, move to next step.
(0, 10)
-9 -6 (1, 10)
-6 -1 (2, 10)
-1 6 (3, 10)
6 -3 (4, 9)
-3 8 (5, 9)
8 5 (6, 8)
Now, we will determine the coordinates of the octant 2 by swapping the p and q
coordinates.
Points of Octant 1 Points of Octant 2
(4, 9) (10, 2)
(5, 9) (10, 1)
(6, 8) (10, 0)
Thus, we can find all the coordinates of the circle for all quadrants.
Quadrant 1 (p, q) Quadrant 2 (-p, q) Quadrant 3 (-p, -q) Quadrant 4 (p, -q)
• It is a time-consuming algorithm.
• Sometimes the points of the circle are not accurate.
We can understand ellipse as an elongated circle. In the midpoint ellipse drawing algorithm, we
will determine the plotting point (p, q). Let us suppose that the center point of the ellipse is (0,
0).
p = 0 and q =rq
Step 3: Wecalculate the initial value and decision parameter for region 1 as follow-
dp = 2r2qp
dq = 2r2pq
Do
{plot (p, q)
If (dk1 < 0)
dp = dp + 2r2q
Else
dp = dp + 2r2q
dq = dq - 2r2p
Step 6: Now ,we calculate the initial value and decision parameter for region 2 as follow-
Do
{plot (p, q)
If (dk2 < 0)
(p= p and q = q - 1)
dq = dp - 2r2p
Else
dq = dq - 2r2p
dp = dp + 2r2q
} while (q < 0)
Step 9: Stop.