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

Chapter 3_ Two Dimensional Algorithms (1)

Chapter 3 discusses two-dimensional algorithms in geometry and computer graphics, focusing on points and lines. It explains the definitions and representations of points in both 2D and 3D, the properties of line drawing algorithms, and details various algorithms such as DDA and Bresenham's for drawing lines efficiently. The chapter emphasizes the importance of accuracy, density, and speed in line drawing, along with the advantages and disadvantages of the DDA algorithm.

Uploaded by

Sara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter 3_ Two Dimensional Algorithms (1)

Chapter 3 discusses two-dimensional algorithms in geometry and computer graphics, focusing on points and lines. It explains the definitions and representations of points in both 2D and 3D, the properties of line drawing algorithms, and details various algorithms such as DDA and Bresenham's for drawing lines efficiently. The chapter emphasizes the importance of accuracy, density, and speed in line drawing, along with the advantages and disadvantages of the DDA algorithm.

Uploaded by

Sara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Chapter 3: Two Dimensional Algorithms

Points and Lines


Points in geometry:
●​ A Point in geometry is defined as a location in the space that is uniquely determined
by an ordered triplet (x, y, z) where x, y, & z are the distances of the point from the
X-axis, Y-axis, and Z-axis respectively in the 3-Dimensions and is defined by ordered
pair (x, y) in the 2-Dimensions where, x and y are the distances of the point from the
X-axis, and Y-axis, respectively.
●​ It is represented using the dot and is named using capital English alphabets.
●​ The figure added below shows a point P in the 3-D at a distance of x, y, and z from
the X-axis, Y-axis, and Z-axis respectively.

Fig: Geometrical Illustration of Points

Points in Computer Graphics


●​ A point is the most basic and fundamental element in computer graphics.
●​ It represents a single location in a coordinate system, without any dimensions (no
width, height, or depth).
●​ Points are used to build and manipulate graphical objects and are essential in
defining shapes, lines, and surfaces in both 2D and 3D graphics.

Fig: Points In Computer Graphics.


[Note: A point is shown by illuminating a pixel on the screen].

AI/ML Engineer Thomas Basyal


Line
●​ A line segment is completely defined in terms of its two endpoints.
●​ A line segment is thus defined as:
Line_Seg = { (x1 , y1 ), (x2 , y2 ) }

●​ A line is produced by means of illuminating a set of intermediary pixels


between the two endpoints

●​ 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)”.

AI/ML Engineer Thomas Basyal


Scan Converting a Straight Line
A straight line may be defined by two endpoints & an equation. In fig the two
endpoints are described by (x1,y1) and (x2,y2). The equation of the line is used to
determine the x, y coordinates of all the points that lie between these two endpoints.

Using the equation of a straight line, y = mx + b where m = & b = the y interrupt,


we can find values of y by incrementing x from x =x1, to x = x2. By scan-converting
these calculated x, y values, we represent the line as a sequence of pixels.

Properties of Good Line Drawing Algorithm:


1. Line should appear Straight: We must appropriate the line by choosing
addressable points close to it. If we choose well, the line will appear straight, if not,
we shall produce crossed lines.

AI/ML Engineer Thomas Basyal


The lines must be generated parallel or at 45° to the x and y-axes. Other lines cause
a problem: a line segment through it that starts and finishes at addressable points,
may happen to pass through no other addressable points in between.

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.

To maintain constant density, dots should be equally spaced.

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.

5. Line should be drawn rapidly: This computation should be performed by


special-purpose hardware.

AI/ML Engineer Thomas Basyal


Algorithm for line Drawing:
1.​ Direct use of line equation
2.​ DDA (Digital Differential Analyzer)
3.​ Bresenham's Algorithm

Direct use of line equation:


It is the simplest form of conversion. First of all scan P1 and P2 points. P1 has
coordinates (x1',y1') and (x2' y2' ).

Then m = (y2',y1')/( x2',x1') and b =

If value of |m|≤1 for each integer value of x. But do not consider

If value of |m|>1 for each integer value of y. But do not consider

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.

Solution: P1 (0,0) P7 (6,18)

x1=0​
y1=0​
x2=6​
y2=18​

We know equation of line is​


y =m x + b​
y = 3x + b..............equation (1)

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

put b = 0 in equation (1)​


y = 3x + 0​
y = 3x

Now calculate intermediate points​


Let x = 1 ⟹ y = 3 x 1 ⟹ y = 3​
Let x = 2 ⟹ y = 3 x 2 ⟹ y = 6​
Let x = 3 ⟹ y = 3 x 3 ⟹ y = 9​
Let x = 4 ⟹ y = 3 x 4 ⟹ y = 12​

AI/ML Engineer Thomas Basyal


Let x = 5 ⟹ y = 3 x 5 ⟹ y = 15​
Let x = 6 ⟹ y = 3 x 6 ⟹ y = 18

So points are P1 (0,0)​


P2 (1,3)​
P3 (2,6)​
P4 (3,9)​
P5 (4,12)​
P6 (5,15)​
P7 (6,18)

Algorithm for drawing line using equation:


Step1: Start Algorithm

Step2: Declare variables x1,x2,y1,y2,dx,dy,m,b,

Step3: Enter values of x1,x2,y1,y2.​


The (x1,y1) are coordinates of a starting point of the line.​
The (x2,y2) are coordinates of an ending point of the line.

Step4: Calculate dx = x2- x1

Step5: Calculate dy = y2-y1

Step6: Calculate m =

Step7: Calculate b = y1-m* x1

Step8: Set (x, y) equal to starting point, i.e., lowest point and xend equal to largest
value of x.

AI/ML Engineer Thomas Basyal


If dx < 0​
then x = x2​
y = y2​
xend= x1​
If dx > 0​
then x = x1​
y = y1​
xend= x2

Step9: Check whether the complete line has been drawn if x=xend, stop

Step10: Plot a point at current (x, y) coordinates

Step11: Increment value of x, i.e., x = x+1

Step12: Compute next value of y from equation y = mx + b

Step13: Go to Step9.

Incremental Line drawing Methods

An "incremental line drawing algorithm" is a method in computer graphics that calculates


the coordinates of pixels along a line by progressively adding small increments to the
current position, essentially "stepping" along the line one pixel at a time, using the previous
calculation as a basis for the next, rather than recalculating everything from scratch each
step; the most well-known examples of this are the Digital Differential Analyzer (DDA) and
Bresenham's line algorithms.

Key points about incremental line drawing algorithms:

​ 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.

Examples of incremental line drawing algorithms:

​ DDA (Digital Differential Analyzer):​


This is a basic incremental algorithm that directly calculates the next pixel position using
the slope of the line, but can be susceptible to rounding errors due to floating-point
arithmetic.
​ Bresenham's algorithm:​
Considered a more optimized version of DDA, it uses an error term to decide which pixel
to plot at each step, minimizing rounding errors and only requiring integer arithmetic.

Line Drawing Algorithms:

DDA algorithm (Digital Difference And Analyzer)

Consider the equation of line

Y=mx+b

Where m is the slope of line and b is the intercept of the line.

We Know
𝑦2−𝑦1
Slope(m) = 𝑥2−𝑥1

∆𝑦
m= ∆𝑥

AI/ML Engineer Thomas Basyal


Here (x1, y1) and (x2, y2) are the endpoints of a line. Further, We can derive the
value of Δy

∆y=m∆x -------------equation (i)


∆𝑦
Similarly ∆x= 𝑚
----------------- equation (ii)

We sample the line by incrementing or decrementing by 1 in one coordinate. The


corresponding nearest integer value along the line path for the other coordinate.
Calculating successive values for y.
We increment ∆x by the unit and we will calculate successive values of y with the
help of the following equation.
Yi+1 = yi+∆y
Where i is the initial value of y in any step of the calculating points for the straight
line.
Further , substitute the value of ∆y from equation –(i)
​ Yi+1 = yi+m∆x -------(iii)
Its mean we increment ∆x by one unit the successive values of y will be calculates
Put ∆x=1 in equation (iii)
Yi+1 = yi+m
∆𝑦
Or, yi +1= yi+ ∆𝑥

Calculating the successive values of x.


On the other hand,
If we increment ∆y by the one unit then we can calculate the successive value of x
with the help of the following equation.
Xi+1= xi + ∆x
Where xi is the initial value of x in any step of calculating points for the straight line
for instance.
Further , we substitute value of ∆x in equation (iv)
∆𝑦
Xi+1= xi + 𝑚

Put ∆y =1 in equation (iv) we get


1
Or Xi+1 = xi + 𝑚

AI/ML Engineer Thomas Basyal


∆𝑥
= xi + ∆𝑦

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.

Advantages of DDA Algorithm:

1.​ Simplicity: The DDA algorithm is straightforward to understand and


implement. It follows a simple step-by-step process for drawing a line.
2.​ Ease of Implementation: It doesn't require complex mathematical
operations, making it suitable for quick implementations in graphical systems.
3.​ No Multiplication Required: The algorithm avoids multiplication operations,
which are generally more computationally expensive. Instead, it relies on
addition and incrementing values, making it faster in comparison.
4.​ Efficient for Raster Displays: DDA works well for raster graphics, as it
produces a sequence of pixel coordinates that can be directly mapped to
pixels on a screen.

Disadvantages of DDA Algorithm:

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.

Applications of DDA Algorithm:

AI/ML Engineer Thomas Basyal


1.​ Raster Graphics Systems: DDA is used in raster-based displays for drawing
lines and curves where pixel-level accuracy is not as critical.
2.​ Computer Graphics: The algorithm is frequently used in computer graphics
for rendering lines in applications where performance is prioritized over
absolute precision.
3.​ Basic Drawing Tools: It can be used in basic 2D graphics applications such
as simple drawing programs or educational tools that aim to demonstrate the
principles of line drawing.
4.​ Simulation and Visualization: DDA is suitable for quick prototyping in
simulation systems, where line generation is needed without worrying about
the exact appearance of the lines.

Algorithms of DDA
Step 1: Initial start (x0,y0) , final (xn,yn) calculate ∆x, ∆y and m
∆x=xn-x0
∆y=yn-y0
∆𝑦
M= ∆𝑥

Step 2: Find number of points between start and end coordinates


If (abs(∆x) >abs(∆y))
Length= p=abs (∆x)
Else
p= abs (∆y)
Step 3: Current point (xn+1,yn+1) find the next point by following three cases.

Case 1: Case 2: Case 3:

If m<1 If m=1 If m>1

Xp+1=(1+xp) Xp+1=1+xp 1
Xp+1= 𝑚 + xp

Yp=m+yp Yp+1=(1+yp) Yp+1=1+yp

AI/ML Engineer Thomas Basyal


Example :
Digitize the line from point (5,6) to (8,12) using the DDA Algorithm.
Solution
Starting point (x0,y0) =(5,6)

Ending point (xn,yn) =(8,12)

now,

∆x=xn-x0=8-5=3

∆y=yn-y0=12-6=6
∆𝑦 6
Slope m = ∆𝑥
= 3
=2

So we have m>1 and ∆x<∆y

Length k = |∆y| = 6

So we followed case m>1


1
Xp+1= 𝑚 + xp

Yp+1=1+y

Calculation of table

X0 Y0 Xp+1 Yp+1 Round off


5 6 5.5 7 (6,7)
6 8 (6,8)
6.5 9 (7,9)
7 10 (7,10)
7.5 11 (8,11)
8 12 (8,12)

AI/ML Engineer Thomas Basyal


BRESENHAM’S Line Drawing Algorithm
Derivation
Assumptions:
1)​ It has two points: Starting Points(x1,y1), Ending Points(x1,y1)
2)​ x1<x2
3)​ Slope: <=450 ; 0<m<=1
4)​ X coordinate is incremented in steps of 1, y coordinate is
computed.
5)​ Generic line equation: y=mx+b.

So, we have an equation of line y=mx+b.


Assumption; that we already have a location of the pixel(xi, yi) and have
plotted it.
The question is, what is the location next to the pixel.

Geometric location of line at x-coordinate xi+1=xi+1


Or, y=mx+b
Where,
​ m=slope=𝛥y/ 𝛥x
​ 𝛥y=y2 -y1
​ 𝛥x=x2-x1 (from the above assumption that x1<x2 )
AI/ML Engineer Thomas Basyal
​ b=intercept
or, y=m(xi+1)+b
or,y=mxi+m+b
Now to define,
​ d1=y-yi
​ = mxi+m+b-yi
​ d2=(yi+1)-y
​ = yi+1- (mxi+m+b)
​ = yi+1- mxi – m – b
Now,
​ d1 – d2 <0 then yi+1 ← yi
​ d1 – d2 >0 then yi+1←yi +1

d1 – d2= (mxi+m+b-yi) – (yi+1- mxi – m – b)


​ =2 mxi + 2m + 2b - 2yi – 1
​ =2m(xi + 1) + 2b – 2yi – 1
We want integer calculations in a loop, but m is not an integer. Looking
at the definition of m where M=slope=𝛥y/ 𝛥x. So, we see that if we
multiply m by 𝛥x we shall remove the denominator and hence the
floating point member.
For this purpose, let us multiply; the difference(d1 – d2) by 𝛥x, store it in
the variable Pi.
​ Pi = 𝛥x(d1 – d2)
​ Pi = 𝛥x (2 mxi + 2m + 2b - 2yi – 1)
​ = 𝛥x * 2 * 𝛥y/ 𝛥x * xi + 2 * 𝛥x * 𝛥y/ 𝛥x + 2b 𝛥x – 2yi 𝛥x – 𝛥x
​ = 2𝛥yxi + 2𝛥y + 2b𝛥x – 2yi 𝛥x – 𝛥x
​ =2𝛥yxi – 2yi 𝛥x + 2𝛥y + 2b𝛥x – 𝛥x
​ =2𝛥yxi – 2yi 𝛥x +c ​ ​ [ c = 2𝛥y + 2b𝛥x – 𝛥x]

AI/ML Engineer Thomas Basyal


Because the sign of Pi is the same as the sign of d1 – d2 we could use it
inside the loop to decide whether to select the pixel at (xi + 1, yi) or at (xi
+ 1, yi + 1). Note that the loop will only include arithmetic integers. There
are now six multiplications, two additions and one selection in each turn
of the loop. However, we can do better than this by defining Pi
recursively.

​ Pi+1 = 2𝛥yxi+1 – 2yi+1 𝛥x +c


​ Pi+1 - Pi = (2𝛥yxi+1 – 2yi+1 𝛥x +c) – (2𝛥yxi – 2yi 𝛥x +c)
​ ​ = 2𝛥yxi+1 – 2yi+1 𝛥x +c - 2𝛥yxi + 2yi 𝛥x - c
​ ​ =2𝛥y (xi+1 – xi) - 2𝛥x (yi+1 – yi)​ [xi+1 – xi = 1]
​ ​ =2𝛥y.1 - 2𝛥x (yi+1 – yi)
​ ​ =2𝛥y - 2𝛥x (yi+1 – yi)
Recursive definition of Pi is,
​ Pi+1 = Pi + 2𝛥y - 2𝛥x (yi+1 – yi)
If we now recall the way we can construct the line pixel by pixel, you will
realize that the underline expressions (yi+1 – yi) can be either 0 (when the
next pixel is plotted at the same y coordinate i.e. d1 – d2<0 ) or, 1 (when
the next pixel is plotted on the y coordinate i.e. d1 – d2>0 ).
Therefore, the final recursive definition for the Pi on the following choice
is as follows ( remember that the sign of Pi is same as d1 – d2).
●​ If Pi < 0, Pi+1 = P + 2𝛥y​ [ 2𝛥x.( yi+1 – yi ) = 0]
●​ If Pi > 0 Pi + 2𝛥y-2𝛥x​ ​ [yi+1 – yi = 1]
At this stage the basic algorithm is defined, we only need to calculate the
initial value for parameter P0,
​ Pi = 2𝛥yxi – 2yi 𝛥x + 2𝛥y + 2b𝛥x – 𝛥x
​ P0 = 2𝛥yx0 – 2y0 𝛥x + 2𝛥y + 2b𝛥x – 𝛥x (equation *)
For the initial point on the line,
​ y0 = mx0 + b
Therefore,
​ b= (y0 - 𝛥y/ 𝛥x) . x0

AI/ML Engineer Thomas Basyal


Substituting the value of b in equation *, we get,
​ P0 = 2𝛥yx0 – 2y0 𝛥x + 2𝛥y + 2𝛥x[(y0 - 𝛥y/ 𝛥x) . x0] – 𝛥x
= 2𝛥yx0 – 2y0 𝛥x + 2𝛥y + 2𝛥xy0 - 2𝛥yx0 – 𝛥x
= 2𝛥y – 𝛥x
Final initial decision parameter, P0 = 2𝛥y – 𝛥x.

BRESENHAM’S Line Drawing Algorithm


1.​ Let's denote two starting points( x1, y1) and ending points (x2, y2).
2.​ Plot the initial points (x1, y1).
3.​ Find 𝛥x, 𝛥y, 2 𝛥x, 2 𝛥y and 2 𝛥y – 𝛥x
Where,
| 𝛥x| = |x2 – x1|
| 𝛥y| = |y2 – y1|
4.​ Calculate first decision parameters P0 = 2 𝛥y – 𝛥x
5.​ Start at k = 0 iteration in each case.

CASE 1 : m < 1 CASE 2 : m >= 1


P<0 P >= 0 P<0 P >= 0
xi+1 = xi +1 xi+1 = xi +1 xi+1 = No change (xi) xi+1 = xi +1
Yi+1 = No change(yi) yi+1 = yi +1 yi+1 = yi +1 yi+1 = yi +1
P1 = P0 + 2 𝛥y P1 = P0 + 2 𝛥y-2𝛥x P1 = P0 + 2𝛥x P1 = P0 + 2𝛥x - 2𝛥y

Advantages of Bresenham's Line Drawing Algorithm:


1.​ Efficient: Bresenham's algorithm is more efficient than DDA because it uses
only integer arithmetic (addition and subtraction) rather than floating-point
operations or rounding functions, making it faster in terms of computational
resources.
2.​ Higher Precision: It generates lines with more accuracy compared to DDA
because it minimizes rounding errors. This results in smoother lines and fewer
pixel inaccuracies.
3.​ No Floating-Point Arithmetic: Since the algorithm only uses integer values
to determine pixel positions, it avoids the need for expensive floating-point
operations, improving performance and reducing errors.

AI/ML Engineer Thomas Basyal


4.​ Smoothness of Lines: The output lines are smoother and more visually
appealing compared to the stair-stepping effects seen in DDA. This is
especially true for lines at various slopes.
5.​ Lower Computational Overhead: Because it avoids costly operations like
multiplication and division, Bresenham’s algorithm is ideal for systems with
limited processing power, such as embedded systems or low-end graphics
hardware.

Disadvantages of Bresenham's Line Drawing Algorithm:

1.​ Complexity: Bresenham's algorithm is more complex to implement than DDA.


It requires more detailed steps and conditions, which can make the code
harder to understand and implement, especially for beginners.
2.​ Limited to Line Drawing: The algorithm is specifically designed for drawing
straight lines and doesn’t directly support drawing curves or more complex
shapes. Other algorithms must be used for such tasks.
3.​ More Conditions to Handle: Bresenham’s algorithm requires additional logic
to handle different line slopes (e.g., steep lines), which increases the
complexity of the implementation.
4.​ No Direct Support for Anti-Aliasing: While it produces smooth lines, the
algorithm doesn’t inherently handle anti-aliasing, which can result in jagged
edges in some cases, especially when rendering lines at certain angles.

Applications of Bresenham's Line Drawing Algorithm:

1.​ Raster Graphics: Similar to DDA, Bresenham’s algorithm is commonly used


for drawing lines on raster-based graphics systems, but it is preferred when
higher precision and smoother lines are required.
2.​ 2D Graphics: It is widely used in 2D graphics rendering applications such as
video games, CAD software, and any system requiring efficient and precise
line rendering.
3.​ Plotters and Printers: In devices like plotters and printers that generate lines
pixel by pixel, Bresenham’s algorithm helps create precise and high-quality
lines without excessive computational overhead.
4.​ Embedded Systems: Bresenham's algorithm is highly efficient for embedded
systems with limited computational power, such as microcontrollers in
embedded graphics applications.
5.​ Computer-Aided Design (CAD): Used in CAD software for drawing precise
lines with minimal computational requirements, making it ideal for designing
accurate technical drawings.
6.​ Graphics Hardware: Many graphics processors (GPUs) or hardware
accelerators for 2D graphics use modified versions of Bresenham’s
line-drawing algorithm to render lines quickly and efficiently.

AI/ML Engineer Thomas Basyal


Numerical Problem
Plot the point (1,1) to (5,3) using BRESENHAM’S algorithm
= Solution,
Starting Points(x1, y1) = (1,1)
Ending Points( x2, y2) = (5,3)
Now,
​ 𝛥y = y2 – y1 = 3 - 1 = 2
​ 𝛥x = x2 – x1 = 5 – 1 = 4
​ m = 𝛥y / 𝛥x = 2 / 4 = 1 / 2 = 0.5 < 1
​ 2 𝛥x = 8, 2 𝛥y = 4
​ 2 𝛥y - 2 𝛥x = 4 – 8 = -4
​ 2 𝛥y – 𝛥x = 4 – 4 = 0
Pi xi yi Xi+1 Yi+1
0 1 1 2 2
-4 2 2 3 2
0 3 2 4 3
-4 4 3 5 3

AI/ML Engineer Thomas Basyal


AI/ML Engineer Thomas Basyal
AI/ML Engineer Thomas Basyal
AI/ML Engineer Thomas Basyal
The midpoint circle drawing algorithm is advantageous due to its efficiency in generating
circle points with minimal calculations, making it easy to implement, but it can suffer from
accuracy issues, potentially resulting in a slightly jagged circle appearance, and is not ideal
for scenarios requiring high precision circles; it's commonly used in basic graphics
applications where fast circle rendering is needed.

AI/ML Engineer Thomas Basyal


Advantages:
●​ Computational Efficiency:​
The algorithm uses simple arithmetic operations, making it fast to calculate points on
a circle, especially when considering the symmetry of a circle and only calculating
points in one octant.
●​ Ease of Implementation:​
Due to its straightforward logic and minimal calculations, the midpoint circle algorithm
is considered easy to code and understand.
●​ Symmetry Exploitation:​
The algorithm leverages the 8-fold symmetry of a circle, only computing points in
one octant and then mirroring them to generate the full circle.

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


Given the centre point and radius of circle,

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.

AI/ML Engineer Thomas Basyal


Procedure-

Given-

●​ Centre point of Circle = (X0, Y0)


●​ Radius of Circle = R

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.

Follow the below two cases-

AI/ML Engineer Thomas Basyal


Step-04:
If the given centre point (X0, Y0) is not (0, 0), then do the following and plot the point-

●​ Xplot = Xc + X0
●​ Yplot = Yc + Y0

Here, (Xc, Yc) denotes the current value of X and Y coordinates.

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.

This is depicted by the following figure-

AI/ML Engineer Thomas Basyal


PRACTICE PROBLEMS BASED ON MID POINT CIRCLE
DRAWING ALGORITHM-

Problem-01:
Given the centre point coordinates (0, 0) and radius as 10, generate all the
points to form a circle.

Solution-
Given-

●​ Centre Coordinates of Circle (X0, Y0) = (0, 0)


●​ Radius of Circle = 10

Step-01:
Assign the starting point coordinates (X0, Y0) as-
AI/ML Engineer Thomas Basyal
●​ X0 = 0
●​ Y0 = R = 10

Step-02:

Calculate the value of initial decision parameter P0 as-

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-

Compute Octant-1 Points Until X ≥ Y

We iterate and update Pk while ensuring X < Y.

Pk X Y (Xk+1, Yk+1)

AI/ML Engineer Thomas Basyal


-9 0 10 (1,10)

-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

At this point, X ≥ Y, so we stop further iterations.

AI/ML Engineer Thomas Basyal


Each iteration calculates points for Octant-1 and Octant-2, then reflects them into all eight
octants using symmetry.

Pk X Y Octant Octant Octant Octant Octant Octant Octant Octant


-1 (X, -2 (Y, -3 (Y, -4 (X, -5 (-X, -6 (-Y, -7 (-Y, -8 (-X,
Y) X) -X) -Y) -Y) -X) X) Y)

-9 0 1 (0,10) (10,0) (10,0) (0,-10) (0,-10) (-10,0) (-10,0) (0,10)


0

-6 1 1 (1,10) (10,1) (10,-1) (1,-10) (-1,-10) (-10,-1) (-10,1) (-1,10)


0

-1 2 1 (2,10) (10,2) (10,-2) (2,-10) (-2,-10) (-10,-2) (-10,2) (-2,10)


0

6 3 1 (3,10) (10,3) (10,-3) (3,-10) (-3,-10) (-10,-3) (-10,3) (-3,10)


0

-3 4 9 (4,9) (9,4) (9,-4) (4,-9) (-4,-9) (-9,-4) (-9,4) (-4,9)

8 5 9 (5,9) (9,5) (9,-5) (5,-9) (-5,-9) (-9,-5) (-9,5) (-5,9)

5 6 8 (6,8) (8,6) (8,-6) (6,-8) (-6,-8) (-8,-6) (-8,6) (-6,8)

-4 7 7 (7,7) (7,7) (7,-7) (7,-7) (-7,-7) (-7,-7) (-7,7) (-7,7)

St 7 7 Algorit Algorit Algorit Algorit Algorit Algorit Algorit Algorit


op hm hm hm hm hm hm hm hm
s Termin Termin Termin Termin Termin Termin Termin Termin
ates ates ates ates ates ates ates ates

Advantages of Mid Point Circle Drawing Algorithm-

The advantages of Mid Point Circle Drawing Algorithm are-

●​ It is a powerful and efficient algorithm.


●​ The entire algorithm is based on the simple equation of circle X2 + Y2 = R2.
●​ It is easy to implement from the programmer’s perspective.
●​ This algorithm is used to generate curves on raster displays.

Disadvantages of Mid Point Circle Drawing Algorithm-


The disadvantages of Mid Point Circle Drawing Algorithm are-

●​ Accuracy of the generating points is an issue in this algorithm.


AI/ML Engineer Thomas Basyal
●​ The circle generated by this algorithm is not smooth.
●​ This algorithm is time consuming.

Important Points

●​ Circle drawing algorithms take advantage of the 8 symmetry property of a


circle.
●​ Every circle has 8 octants and the circle drawing algorithm generates all the
points for one octant.
●​ The points for other 7 octants are generated by changing the sign towards X
and Y coordinates.
●​ To take advantage of the 8 symmetry property, the circle must be formed
assuming that the centre point coordinates are (0, 0).
●​ If the centre coordinates are other than (0, 0), then we add the X and Y
coordinate values with each point of the circle with the coordinate values
generated by assuming (0, 0) as centre point.

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.

Step 2: Compute Points for Octants & Translate to Center

AI/ML Engineer Thomas Basyal


Pk X Y Octant Octant Octant Octant Octant Octant Octant Octant
-1 (X, -2 (Y, -3 (Y, -4 (X, -5 (-X, -6 (-Y, -7 (-Y, -8 (-X,
Y) X) -X) -Y) -Y) -X) X) Y)

-9 0 1 (4,6) (14,-4) (14,-4) (4,-14) (4,-14) (-6,-4) (-6,-4) (4,6)


0

-6 1 1 (5,6) (14,-3) (14,-5) (5,-14) (3,-14) (-6,-5) (-6,-3) (3,6)


0

-1 2 1 (6,6) (14,-2) (14,-6) (6,-14) (2,-14) (-6,-6) (-6,-2) (2,6)


0

6 3 1 (7,6) (14,-1) (14,-7) (7,-14) (1,-14) (-6,-7) (-6,-1) (1,6)


0

-3 4 9 (8,5) (13,-0) (13,-8) (8,-13) (0,-13) (-5,-8) (-5,0) (0,5)

8 5 9 (9,5) (13,1) (13,-9) (9,-13) (-1,-13) (-5,-9) (-5,1) (-1,5)

5 6 8 (10,4) (12,2) (12,-10 (10,-12 (-2,-12) (-4,-10) (-4,2) (-2,4)


) )

-4 7 7 (11,3) (11,3) (11,-11) (11,-11) (-3,-11) (-3,-11) (-3,3) (-3,3)

St 7 7 Algorit Algorit Algorit Algorit Algorit Algorit Algorit Algorit


op hm hm hm hm hm hm hm hm
s Termin Termin Termin Termin Termin Termin Termin Termin
ates ates ates ates ates ates ates ates

AI/ML Engineer Thomas Basyal


AI/ML Engineer Thomas Basyal
AI/ML Engineer Thomas Basyal
AI/ML Engineer Thomas Basyal
AI/ML Engineer Thomas Basyal
Draw an ellipse having a semi-major axis of 8, a semi-mirror axis of 6,

and a center at the origin.

For Region 1: Po = -332

For Region 2: Po = -23

AI/ML Engineer Thomas Basyal


Mid-Point Ellipse Drawing Algorithm

AI/ML Engineer Thomas Basyal

You might also like