Midpoint Circle Drawing Algorithm

mail@pastecode.io avatar
unknown
python
7 months ago
779 B
8
Indexable
Never
def midpoint_circle(xc, yc, r):
    x, y = 0, r
    p = 1 - r  # Initial decision parameter
    x_coords, y_coords = [], []

    while x <= y:
        # 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 p < 0:
            p += 2 * x + 1
        else:
            y -= 1
            p += 2 * (x - y) + 1

    return x_coords, y_coords

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

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