1

I have tried different implementations of Bresenham’s circle drawing algorithm (attempt 1, attempt 2, attempt 3) but it keeps generating a rectangle instead of a circle.

What am I doing wrong?

Here is the code of my last attempt:

class bresenham_circle:
    def __init__(self, radius):
        self.radius = int(radius)

        self.xc = self.radius
        self.yc = self.radius

        self.data_size = self.radius * 2 + 1
        self.data = [[False] * self.data_size] * self.data_size

        self.print_data()
        self.draw_circle()
        self.print_data()

    def print_data(self):
        print()

        for y in range(0, self.data_size):
            print(self.data[y])

        print()

    def draw_circle(self):
        x = 0
        y = self.radius
        d = 3 - 2 * self.radius

        self.put_pixel(x, y)

        while y >= x:
            x += 1

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

            self.put_pixel(x, y)

    def put_pixel(self, x, y):
        self.data[self.yc + y][self.xc + x] = True
        self.data[self.yc + y][self.xc - x] = True
        self.data[self.yc - y][self.xc + x] = True
        self.data[self.yc - y][self.xc - x] = True

        self.data[self.yc + x][self.xc + y] = True
        self.data[self.yc + x][self.xc - y] = True
        self.data[self.yc - x][self.xc + y] = True
        self.data[self.yc - x][self.xc - y] = True

What am I trying to achieve:

The function "draw_circle" should set all pixel needed to draw a circle to "True". Unfortunately all pixel are always set to "True" (like a rectangle).

0 Answers0