Computer Graphics Assignment Rahin (Batch 59)
Computer Graphics Assignment Rahin (Batch 59)
Dept. of CSE
Introduction:
Drawing straight lines on digital screens is a basic yet critical operation in computer
graphics. While modern graphics libraries and hardware can handle line drawing efficiently,
understanding the underlying algorithms is essential for those interested in graphics
programming and algorithm design. The Mid-Point Line Drawing Algorithm, an extension of
Bresenham's Line Algorithm, is one such method that balances efficiency and accuracy.
The primary goal of the Mid-Point Line Drawing Algorithm is to determine the closest pixels
to an ideal line between two points, usually defined as
The algorithm works incrementally, deciding at each step which pixel is closer to the ideal
line.
Basic Principles:
● Incremental Error Calculation: The algorithm calculates the error incrementally to
decide the next pixel to plot.
1.Initialization
● Define the endpoints: Identify the starting point (𝑥1,𝑦1) and the ending point (𝑥2,𝑦2)
of the line.
𝑑𝑥=𝑥2−𝑥1
𝑑y=y2−y1
𝑝=2𝑑𝑦−𝑑𝑥
2.Decision Making
● For each x-coordinate from 𝑥1 to 𝑥2:
If 𝑝<0:
then, 𝑝=𝑝+2𝑑𝑦
Else:
𝑝=𝑝+2(𝑑𝑦−𝑑𝑥)
● Increment the y-coordinate: 𝑦=𝑦+1
3.Handling Slopes
The algorithm handles different slopes of the line by ensuring the correct incrementation of 𝑥
and y coordinates:
For 𝑑𝑦>𝑑𝑥: Iterate over y from 𝑦1 to 𝑦2, swapping the roles of x and y in the decision
process.
The algorithm determines which pixel to light up based on the decision parameter, ensuring
that the line is as close as possible to the true line.
Implementation Example:
dx = x2 - x1
dy = y2 - y1
d = dy - (dx / 2)
x = x1
y = y1
points = []
points.append((x, y))
x += 1
if d < 0:
d = d + dy
else:
d = d + (dy - dx)
y += 1
points.append((x, y))
return points
Example usage:
print(point)
Example:
ο Update p: p = p + 2 * dy = -2 + 2 * 3 = 4
3. Second Pixel (x = 3, y = 3):
ο Update p: p = p - 2 * dx + 2 * dy = 4 - 2 * 8 + 2 * 3 = -2
4. Repeat the steps until the line reaches the point (10, 6).
By following these steps, we can draw a straight line from (2, 3) to (10, 6) efficiently.
1.Initial values:
o P0 (0,0) , P1 (6,4)
o Δx=6−0=6
o Δy=4−0=4
o d=2×4−6=2
2.Steps:
oPlot (0,0)
oPlot (1,1)
oPlot (2,1)
o d= 6, d > 0 ⇒ y = 2, d = 6+2×(4−6) = 2
oPlot (3,2)
oPlot (4,3)
oPlot (5,3)
oPlot (6,4)
Result: Points plotted: (0,0), (1,1), (2,1), (3,2), (4,3), (5,3), (6,4)
1.Initial values:
o P0(0,0), P1(4,6)
o Δx=4−0=4
o Δy=6−0=6
o d=2×6−4=8
2.Steps:
oPlot (0,0)
oPlot (0,1)
oPlot (0,2)
oPlot (0,3)
oPlot (0,4)
oPlot (0,5)
oPlot (0,6)
Result: Points plotted: (0,0), (0,1), (0,2), (0,3), (0,4), (0,5), (0,6)
o P0 (0,0), P1 (5,5)
o Δx=5−0=5
o Δy=5−0=5
o d=2×5−5=5
2.Steps:
oPlot (0,0)
oPlot (1,1)
oPlot (2,2)
oPlot (3,3)
oPlot (4,4)
oPlot (5,5)
o P0 (0,0), P1 (7,3)
o Δx=7−0=7
o Δy=3−0=3
o d=2×3−7= −1
2.Steps:
oPlot (0,0)
oPlot (1,0)
oPlot (2,1)
oPlot (3,1)
oPlot (4,2)
oPlot (5,2)
oPlot (6,3)
Result: Points plotted: (0,0), (1,0), (2,1), (3,1), (4,2), (5,2), (6,3), (7,3)
1.Initial values:
o P0 (1,1) , P1 (5,8)
o Δx=5−1=4
o Δy=8−1=7
o d=2×7−4=10
2.Steps:
oPlot (1,1)
oPlot (1,2)
oPlot (1,3)
oPlot (1,4)
Applications:
The Mid-Point Line Drawing Algorithm is closely related to Bresenham's Line Algorithm.
Both use integer arithmetic and incremental error calculation, but the Mid-Point Algorithm
provides a more intuitive approach to decision-making based on the midpoint between
potential pixel locations.
The DDA Algorithm uses floating-point arithmetic, which can be less efficient than the
integer arithmetic used in the Mid-Point and Bresenham's Algorithms. However, the DDA
Algorithm is simpler to understand and implement for those new to graphics programming.
Conclusion:
The Mid-Point Line Drawing Algorithm is a powerful tool in the arsenal of computer
graphics algorithms. Its efficiency, simplicity, and accuracy make it an excellent choice for
rendering straight lines on raster displays. By understanding and implementing this
algorithm, programmers can gain deeper insights into the workings of computer graphics and
the principles of incremental error calculation. Whether you're developing graphics software
or simply exploring the field of computer graphics, the Mid-Point Line Drawing Algorithm is
an essential technique to master.