0% found this document useful (0 votes)
5 views

Computer Graphics Assignment Rahin (Batch 59)

The document presents an assignment on the Mid-Point Line Drawing Algorithm, a key technique in computer graphics for rendering straight lines on raster displays. It outlines the algorithm's principles, steps, advantages, applications, and comparisons with other algorithms like Bresenham's and DDA. The conclusion emphasizes the algorithm's efficiency and importance for understanding computer graphics programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Computer Graphics Assignment Rahin (Batch 59)

The document presents an assignment on the Mid-Point Line Drawing Algorithm, a key technique in computer graphics for rendering straight lines on raster displays. It outlines the algorithm's principles, steps, advantages, applications, and comparisons with other algorithms like Bresenham's and DDA. The conclusion emphasizes the algorithm's efficiency and importance for understanding computer graphics programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

The People’s university of Bangladesh

Dept. of CSE

Assignment on :- Computer Graphics


Course Code :- CSE-405
Program :- B.Sc In Computer Science & Engineering

Submitted by ​ :- N H Rahin Chowdhury


ID ​ :- 22559202022
Batch ​ :- 59 (ND)
Submission Date ​ :- 21 Mar 2025

Submitted to :- Md. Tofazzal Hosen


Lecturer Dept. of CSE
1. Midpoint line drawing algorithm with explanation.

The Mid-Point Line Drawing Algorithm is a foundational technique in computer graphics,


widely used for rendering straight lines on raster displays. It is known for its efficiency,
simplicity, and ability to produce visually accurate lines with minimal computational
overhead.

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.

Overview of the Algorithm:

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

(𝑥1,𝑦1) and (𝑥2,𝑦2)

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.

●​ Integer Arithmetic: It uses only integer arithmetic, avoiding floating-point


operations, which makes it efficient for execution on integer-only hardware.
●​ Decision Parameter: The decision parameter determines whether to increment the
y- coordinate while iterating through 𝑥-coordinates.

Steps of the Mid-Point Line Drawing Algorithm:

1.​Initialization
●​ Define the endpoints: Identify the starting point (𝑥1,𝑦1) and the ending point (𝑥2,𝑦2)
of the line.

●​ Calculate the differences:

𝑑𝑥=𝑥2−𝑥1

𝑑y=y2−y1

Determine the initial decision parameter:

𝑝=2𝑑𝑦−𝑑𝑥

2.​Decision Making
●​ For each x-coordinate from 𝑥1 to 𝑥2:

Plot the pixel at (𝑥,𝑦).


●​ Update the decision parameter:

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 x from 𝑥1 to 𝑥2.

For 𝑑𝑦>𝑑𝑥: Iterate over y from 𝑦1 to 𝑦2, swapping the roles of x and y in the decision
process.

4.​Plotting the Line

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:

Here's a basic implementation of the Mid-Point Line Drawing Algorithm in Python:

def mid_point_line_drawing(x1, y1, x2, y2):

dx = x2 - x1

dy = y2 - y1

d = dy - (dx / 2)
x = x1

y = y1

points = []

points.append((x, y))

while x < x2:

x += 1

if d < 0:

d = d + dy

else:

d = d + (dy - dx)

y += 1

points.append((x, y))

return points

Example usage:

points = mid_point_line_drawing(2, 2, 10, 5)


for point in points:

print(point)

Example:

Let’s say we need to draw a line from (2, 3) to (10, 6).

1.​ Initial Setup:


ο​ dx = 10 - 2 = 8
ο​ dy = 6 - 3 = 3
ο​ p0 = 2 * dy - dx = 2 * 3 - 8 = -2
2.​ First Pixel (x = 2, y = 3):

ο​ Since p0 < 0, the next pixel will be at (x + 1, y).

ο​ Update p: p = p + 2 * dy = -2 + 2 * 3 = 4
3.​ Second Pixel (x = 3, y = 3):

ο​ Now, p > 0, so the next pixel will be at (x + 1, y + 1).

ο​ 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: Line from (0, 0) to (6, 4) (Shallow line)

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:

o​Plot (0,0)

o d= 2, d > 0 ⇒ y = 1, d=2+2×(4−6) =−2

o​Plot (1,1)

o d = −2, d < 0 ⇒ x = 2, d= −2+2×4 = 6

o​Plot (2,1)

o d= 6, d > 0 ⇒ y = 2, d = 6+2×(4−6) = 2

o​Plot (3,2)

o d=2, d > 0 ⇒ y=3, d = 2+2×(4−6) = −2

o​Plot (4,3)

o d= −2, d < 0 ⇒ x= 5, d = −2+2×4 = 6

o​Plot (5,3)

o d=6, d > 0 ⇒ y=4, d = 6+2×(4−6) = 2

o​Plot (6,4)

Result: Points plotted: (0,0), (1,1), (2,1), (3,2), (4,3), (5,3), (6,4)

2: Line from (0, 0) to (4, 6) (Steep line)

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:

o​Plot (0,0)

o d=8, d>0 ⇒ y=1, d=8+2× (6−4) =12

o​Plot (0,1)

o d=12, d>0 ⇒ y=2, d=12+2× (6−4) =16

o​Plot (0,2)

o d=16, d>0 ⇒ y=3, d=16+2× (6−4) =20

o​Plot (0,3)

o d=20, d>0 ⇒ y=4, d=20+2× (6−4) =24

o​Plot (0,4)

o d=24, d>0 ⇒ y=5, d=24+2× (6−4) =28

o​Plot (0,5)

o d=28, d>0 ⇒ y=6, d=28+2× (6−4) =32

o​Plot (0,6)

Result: Points plotted: (0,0), (0,1), (0,2), (0,3), (0,4), (0,5), (0,6)

3: Line from (0, 0) to (5, 5) (Diagonal line)


1.​Initial values:

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:

o​Plot (0,0)

o d=5, d>0 ⇒ y=1, d=5+2× (5−5) =5

o​Plot (1,1)

o d=5, d>0 ⇒ y=2, d=5+2× (5−5) =5

o​Plot (2,2)

o d=5, d>0d ⇒ y=3, d=5+2× (5−5) =5

o​Plot (3,3)

o d=5, d>0 ⇒ y=4, d=5+2× (5−5) =5

o​Plot (4,4)

o d=5, d>0 ⇒ y=5, d=5+2× (5−5) =5

o​Plot (5,5)

Result: Points plotted: (0,0), (1,1), (2,2), (3,3), (4,4), (5,5)

4: Line from (0, 0) to (7, 3) (Shallow line)


1.​Initial values:

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:

o​Plot (0,0)

o d= −1, d<0 ⇒ x=1, d=−1+2×3=5

o​Plot (1,0)

o d=5, d>0 ⇒ y=1, d=5+2× (3−7) =−3

o​Plot (2,1)

o d= −3, d<0 ⇒ x=3, d=−3+2×3=3

o​Plot (3,1)

o d=3, d>0 ⇒ y=2, d=3+2× (3−7) =−1

o​Plot (4,2)

o d= −1, d <0 ⇒ x=5, d= −1+2×3=5

o​Plot (5,2)

o d=5, d>0 ⇒ y= 3, d=5+2× (3−7) =−3

o​Plot (6,3)

o d=−3, d<0 ⇒ x=7, d=−3+2×3=3


o​Plot (7,3)

Result: Points plotted: (0,0), (1,0), (2,1), (3,1), (4,2), (5,2), (6,3), (7,3)

5: Line from (1, 1) to (5, 8) (Steep line)

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:

o​Plot (1,1)

o d=10, d>0 ⇒ y=2, d=10+2×(7−4)=16

o​Plot (1,2)

o d=16, d>0 ⇒ y=3, d=16+2×(7−4)=22

o​Plot (1,3)

o d=22, d>0 ⇒ y=4, d=22+2×(7−4)=28

o​Plot (1,4)

o​Plot remaining points similarly

Advantages of the Mid-Point Line Drawing Algorithm:

●​ Uses only integer arithmetic, making it computationally efficient.


●​ Eliminates floating-point calculations, reducing processing time.
●​ Works well for all types of slopes, ensuring accuracy and consistency.
●​ Provides an optimal balance between performance and visual quality.

Applications:

●​ Used in computer-aided design (CAD) software for rendering lines.


●​ Employed in game development for drawing geometric shapes and outlines.
●​ Essential for raster graphics rendering in various graphical applications.
●​ Used in digital image processing for line segmentation and boundary tracing.
●​ Applied in geographical information systems (GIS) for map visualization.

Comparison with Other Algorithms:

1.​Bresenham's Line Algorithm

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.

2.​Digital Differential Analyzer (DDA) Algorithm

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.

You might also like