06. Circle Drawing Techniques
Dated: 15-04-2025
Circle
Imagine a point \(O\) called the center of the circle
, then the circle
is collection of all points, equidistant from \(O\).
This distance is called the radius
\(r\) and \(2 \times r = d\) is called the diameter
.
The angle is \(360^\circ\) or \(2 \pi\).
The perimeter of a circle
is called the circumference
\(C\).
Circle Drawing Techniques
We need 2 inputs
- The coordinates of the
center
. - The
radius
\(r\).
Circle Drawing Using Cartesian Coordinates
If a circle is centered at \((x_c, y_c)\) then equation of the circle is
Algorithm
def circle1(x_c, y_c, r):
for x in range(r - x_c, r + x_c):
y = y_c + sqrt(r ** 2 - (x - x_c) ** 2)
drawPixel(x, y)
y = y_c - sqrt(r ** 2 - (x - x_c) ** 2)
drawPixel(x, y)
This isn't good enough because of the heavy operations like squaring
and square roots
.
Also, it leaves some gaps in the circle as the slope
1 increases.
Circle Drawing Using Polar Coordinates
For more continuous circle
def circle2(x_c, y_c, r):
for theta in range(0, 360, 1 / r):
x = x_c + r * cos(theta)
y = y_c + r * sin(theta)
drawPixel(x, y)
There are still some problems here
- Floating calculations are expensive
- We are calculating the whole shape even though it is symmetric
We can solve the symmetric problem by computing only 4th of the circle and then reflecting it across \(x\) and \(y\) axis.
Midpoint Circle Algorithm
We start by plotting \(p(r, 0)\) till \(x < y\) to draw the first octant.
Imagine we plot \((x_k, y_k)\), now we need to know if next point is \((x_k + 1, y_k)\) or \((x_k + 1, y_k - 1)\)
If this midpoint, that is \(y_k - \frac 1 2\) is inside the circle then that means point is closer to \(y_k\), otherwise \(y_k - 1\).
Midpoint (green) inside the circle suggesting that the actual point (blue) is near \(y_k\) instead of \(y_k - 1\).
Now doing \(P_{k + 1} - P_k\) gives us
Value of \(y_{k + 1}\) depends upon \(P_k\)
- \(P_k < 0 \implies y_{k + 1} = y_k \implies P_{k + 1} = P_k + 2(x_k + 1 ) + 1\)
- \(P_k > 0 \implies y_{k + 1} = y_k - 1 \implies P_{k + 1} = P_k + 2(x_k + 1) - 2 (y_k – 1 ) + 1\)
The starting pixel
2 starts from \((0, r)\).
We can round it off to \(1\) for integer arithmetics
Algorithm
#include <stdio.h>
void drawSymmetricPoints(int xcenter, int ycenter, int x, int y) {
// Placeholder: print the symmetric points
printf("(%d, %d)\n", xcenter + x, ycenter + y);
printf("(%d, %d)\n", xcenter - x, ycenter + y);
printf("(%d, %d)\n", xcenter + x, ycenter - y);
printf("(%d, %d)\n", xcenter - x, ycenter - y);
printf("(%d, %d)\n", xcenter + y, ycenter + x);
printf("(%d, %d)\n", xcenter - y, ycenter + x);
printf("(%d, %d)\n", xcenter + y, ycenter - x);
printf("(%d, %d)\n", xcenter - y, ycenter - x);
}
void midpointCircle(int xcenter, int ycenter, int radius) {
int x = 0;
int y = radius;
int p = 1 - radius;
do {
drawSymmetricPoints(xcenter, ycenter, x, y);
x = x + 1;
if (p < 0) {
p = p + 2 * x + 1;
} else {
y = y - 1;
p = p + 2 * (x + 1) - 2 * (y - 1) + 1
}
} while (x < y);
}
int main() {
midpointCircle(0, 0, 5);
return 0;
}