Mid-Point Circle Drawing Algorithm

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

def draw_circle_midpoint(radius):
    x = radius
    y = 0
    p = 1 - radius

    # Lists to store points for plotting
    x_points = []
    y_points = []

    while x >= y:
        x_points.extend([x, -x, x, -x, y, -y, y, -y])
        y_points.extend([y, y, -y, -y, x, x, -x, -x])

        y += 1
        if p <= 0:
            p = p + 2 * y + 1
        else:
            x -= 1
            p = p + 2 * y - 2 * x + 1

        if x < y:
            break

    # Plotting the circle
    plt.scatter(x_points, y_points, color='blue', marker='o')
    plt.axhline(0, color='black', linewidth=0.5)
    plt.axvline(0, color='black', linewidth=0.5)
    plt.grid(color='gray', linestyle='--', linewidth=0.5)
    plt.title('Midpoint Circle Drawing Algorithm')
    plt.show()

# Example usage:
draw_circle_midpoint(10)
Leave a Comment