Chapter 3_ Two Dimensional Algorithms (1)
Chapter 3_ Two Dimensional Algorithms (1)
● Lines are digitized into a set of discrete integer positions that approximate the
actual line path.
● Example: A computed line position of (10.48, 20.51) is converted to pixel
position (10, 21).
● The rounding of coordinate values to integers causes all but horizontal and vertical
lines to be displayed with a stair step appearance “the jaggies(informal name for
artifacts in raster images)”.
2. Lines should terminate accurately: Unless lines are plotted accurately, they
may terminate at the wrong place.
3. Lines should have constant density: Line density is proportional to the no. of
dots displayed divided by the length of the line.
4. Line density should be independent of line length and angle: This can be
done by computing an approximating line-length estimate and to use a
line-generation algorithm that keeps line density constant to within the accuracy of
this estimate.
Example: A line with starting point as (0, 0) and ending point (6, 18) is given.
Calculate value of intermediate points and slope of line.
x1=0
y1=0
x2=6
y2=18
put value of x from initial point in equation (1), i.e., (0, 0) x =0, y=0
0 = 3 x 0 + b
0 = b ⟹ b=0
Step6: Calculate m =
Step8: Set (x, y) equal to starting point, i.e., lowest point and xend equal to largest
value of x.
Step9: Check whether the complete line has been drawn if x=xend, stop
Step13: Go to Step9.
Progressive calculation:
Each new pixel position is calculated based on the previous pixel's position, using
simple arithmetic operations like addition and subtraction, which are computationally
efficient.
No complex calculations:
Unlike direct line equation methods, incremental algorithms avoid costly operations like
division or trigonometric functions by relying on small increments and decision variables.
Suitable for raster graphics:
These algorithms are well-suited for rendering lines on a pixel grid, as they directly
calculate the pixel coordinates needed to draw the line.
How it works:
● Input: The starting and ending points of the line are provided as coordinates.
● Calculate differences: The difference in x-coordinates (dx) and y-coordinates (dy)
between the endpoints are calculated.
● Step size: Based on the slope of the line, a step size is determined, which is often
set to 1 for each coordinate (x or y).
AI/ML Engineer Thomas Basyal
● Iteration: Loop through the steps, incrementing the appropriate coordinate (x or y)
by the step size and calculating the corresponding other coordinate using the line
equation.
● Pixel plotting: At each step, plot the calculated pixel on the screen.
Y=mx+b
We Know
𝑦2−𝑦1
Slope(m) = 𝑥2−𝑥1
∆𝑦
m= ∆𝑥
So as you have seen both possibilities we will decide weather to put ∆x =1 or ∆y=1
so based on these derived cases we get such as
Case 1: If the slope (m) is less than or equal to 1 (|m| <=1) then the coordinates x
are sampled unit internals (∆x=1) While each successive value of y is computed .
then the value of y is calculated and is rounded off to the nearest integer value.
Case 2: If the slope (m) is greater than unit internals (∆y=1) and x values are
calculated with derived equations.
Case 3: However for negative values of slope (m<0) we follow the same procedures.
So, only the sampling unit ∆x and ∆y become -1.
1. Overhead from Rounding: The algorithm uses a round-off function (round())
to convert the floating-point coordinates to integer pixel locations. This
introduces extra overhead, as rounding is an additional computational step.
2. Increased Time Complexity: Due to the rounding operation, the overall time
complexity of the algorithm can be affected, especially for long lines or
high-resolution displays.
3. Pixelation and Lack of Smoothness: The use of rounding can lead to less
precise results, producing jagged or "stair-stepping" lines. This results in lines
that are not as smooth as those produced by other algorithms, such as the
Bresenham line algorithm.
4. Inaccuracy: DDA may generate lines that are not entirely accurate, especially
when drawing at steep angles or for lines of very high resolution. The
rounding errors accumulate and cause deviations from the ideal line.
Algorithms of DDA
Step 1: Initial start (x0,y0) , final (xn,yn) calculate ∆x, ∆y and m
∆x=xn-x0
∆y=yn-y0
∆𝑦
M= ∆𝑥
Xp+1=(1+xp) Xp+1=1+xp 1
Xp+1= 𝑚 + xp
now,
∆x=xn-x0=8-5=3
∆y=yn-y0=12-6=6
∆𝑦 6
Slope m = ∆𝑥
= 3
=2
Length k = |∆y| = 6
Yp+1=1+y
Calculation of table
Disadvantages:
● Accuracy Issues:
While efficient, the midpoint algorithm can sometimes generate points that are slightly
off the true circle path, leading to a slightly "jagged" appearance, especially at higher
zoom levels.
● Pixel Dependency:
The quality of the circle depends on the pixel grid, which can lead to visible
stair-stepping effects on low-resolution displays.
● Not Suitable for High Precision:
For applications requiring very smooth, accurate circles, more complex algorithms
might be preferred.
Applications:
● Basic Graphics Rendering:
Drawing circles in simple applications like games, user interfaces, and basic CAD
software where fast rendering is prioritized.
● Educational Demonstrations:
Due to its simplicity, the midpoint circle algorithm is often used as a teaching tool to
understand basic circle drawing concepts.
● Quick Prototyping:
When developing a graphical interface, the midpoint algorithm can be used to quickly
visualize circles during the initial design stages.
Mid Point Circle Drawing Algorithm attempts to generate the points of one octant.
The points for other octacts are generated using the eight symmetry property.
Given-
The points generation using Mid Point Circle Drawing Algorithm involves the following steps-
Step-01:
Assign the starting point coordinates (X0, Y0) as-
● X0 = 0
● Y0 = R
Step-02:
Calculate the value of initial decision parameter P0 as-
P0 = 1 – R
Step-03:
Suppose the current point is (Xk, Yk) and the next point is (Xk+1, Yk+1).
Find the next point of the first octant depending on the value of decision parameter Pk.
● Xplot = Xc + X0
● Yplot = Yc + Y0
Step-05:
Keep repeating Step-03 and Step-04 until Xplot >= Yplot.
Step-06:
Step-05 generates all the points for one octant.
To find the points for the other seven octants, follow the eight symmetry property of the
circle.
Problem-01:
Given the centre point coordinates (0, 0) and radius as 10, generate all the
points to form a circle.
Solution-
Given-
Step-01:
Assign the starting point coordinates (X0, Y0) as-
AI/ML Engineer Thomas Basyal
● X0 = 0
● Y0 = R = 10
Step-02:
P0 = 1 – R
P0 = 1 – 10
P0 = -9
Step-03:
As Pinitial < 0, so case-01 is satisfied.
Thus,
● Xk+1 = Xk + 1 = 0 + 1 = 1
● Yk+1 = Yk = 10
● Pk+1 = Pk + 2 x Xk+1 + 1 = -9 + (2 x 1) + 1 = -6
Step-04:
This step is not applicable here as the given centre point coordinates are (0, 0).
Step-05:
Step-03 is executed similarly until Xk+1 >= Yk+1 as follows-
Pk X Y (Xk+1, Yk+1)
-6 1 10 (2,10)
-1 2 10 (3,10)
6 3 10 (4,9)
-3 4 9 (5,9)
8 5 9 (6,8)
5 6 8 (7,7)
Stops 7 7 Algorithm
Terminates
Important Points
Midpoint Circle Drawing Algorithm with a center at (4, -4) and radius = 10
while maintaining the stopping condition X ≥ Y and calculating all eight
octants.