Bresenham’s Circle Algorithm

mail@pastecode.io avatar
unknown
python
7 months ago
799 B
6
Indexable
Never
import matplotlib.pyplot as plt

def bresenham_circle(xc, yc, r):
    x, y = 0, r
    d = 3 - 2 * r
    x_coords, y_coords = [], []

    while y >= x:
        # Reflecting the points in all octants
        x_coords.extend([xc+x, xc-x, xc+x, xc-x, xc+y, xc-y, xc+y, xc-y])
        y_coords.extend([yc+y, yc+y, yc-y, yc-y, yc+x, yc+x, yc-x, yc-x])
        x += 1

        if d > 0:
            y -= 1
            d = d + 4 * (x - y) + 10
        else:
            d = d + 4 * x + 6

    return x_coords, y_coords

# Example usage
xc, yc, r = 50, 50, 30
x_coords, y_coords = bresenham_circle(xc, yc, r)

plt.scatter(x_coords, y_coords)
plt.title("Bresenham's Midpoint Circle Algorithm")
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.grid(True)
plt.gca().set_aspect('equal', adjustable='box')
plt.show()
Leave a Comment