Bresenhams circle algorithm

mail@pastecode.io avatar
unknown
python
7 months ago
1.1 kB
31
Indexable
Never
import matplotlib.pyplot as plt

def draw_circle(center_x, center_y, radius):
    x = radius
    y = 0
    decision_param = 1 - radius

    points = []

    while x >= y:
        points.append((x + center_x, y + center_y))
        points.append((-x + center_x, y + center_y))
        points.append((x + center_x, -y + center_y))
        points.append((-x + center_x, -y + center_y))
        points.append((y + center_x, x + center_y))
        points.append((-y + center_x, x + center_y))
        points.append((y + center_x, -x + center_y))
        points.append((-y + center_x, -x + center_y))

        y += 1

        if decision_param <= 0:
            decision_param += 2 * y + 1
        else:
            x -= 1
            decision_param += 2 * (y - x) + 1

    return points

# Example usage
center_x, center_y = 0, 0
radius = 5
circle_points = draw_circle(center_x, center_y, radius)

# Plot the circle points
x_vals, y_vals = zip(*circle_points)
plt.scatter(x_vals, y_vals)
plt.gca().set_aspect('equal', adjustable='box')
plt.title('Bresenham\'s Circle Drawing Algorithm')
plt.show()
Leave a Comment