Article

Bresenham Circle Algorithm in Python

Last updated Dec. 11, 2022

...

The Bresenham algorithm can also be used to generate a digital representation of a circle on a two-dimensional grid. In Python, you can use the following code to implement the Bresenham circle generation algorithm:

Here is a simple implementation of the Bresenham's circle algorithm in Python:

def bresenham_circle(x0, y0, radius):
  x = radius
  y = 0
  err = 0

  while x >= y:
    print(x0 + x, y0 + y)
    print(x0 + y, y0 + x)
    print(x0 - y, y0 + x)
    print(x0 - x, y0 + y)
    print(x0 - x, y0 - y)
    print(x0 - y, y0 - x)
    print(x0 + y, y0 - x)
    print(x0 + x, y0 - y)

    y += 1
    err += 1 + 2*y
    if 2*(err-x) + 1 > 0:
      x -= 1
      err += 1 - 2*x

This algorithm takes the center coordinates (x0, y0) and the radius of the circle as input, and it prints the coordinates of each point on the circle. You can then use these coordinates to draw the circle on a canvas.

Note that this is a simple implementation for educational purposes, and it may not be suitable for use in production code. For example, it doesn't check for integer overflows and it doesn't use the midpoint optimization.

Here is an example of how you could visualize the Bresenham's circle algorithm using Python's matplotlib library:

import matplotlib.pyplot as plt

def bresenham_circle(x0, y0, radius):
  x = radius
  y = 0
  err = 0

  points = []

  while x >= y:
    points.append((x0 + x, y0 + y))
    points.append((x0 + y, y0 + x))
    points.append((x0 - y, y0 + x))
    points.append((x0 - x, y0 + y))
    points.append((x0 - x, y0 - y))
    points.append((x0 - y, y0 - x))
    points.append((x0 + y, y0 - x))
    points.append((x0 + x, y0 - y))

    y += 1
    err += 1 + 2*y
    if 2*(err-x) + 1 > 0:
      x -= 1
      err += 1 - 2*x

  return points

points = bresenham_circle(50, 50, 30)

# plot the points
plt.scatter([x for x, y in points], [y for x, y in points])

# draw the circle
circle = plt.Circle((50, 50), 30, fill=False)
plt.gcf().gca().add_artist(circle)

plt.show()

This code generates a scatter plot of the points generated by the Bresenham's circle algorithm, and it also draws the circle using matplotlib's Circle object for comparison.

You can see that the points generated by the Bresenham's algorithm closely approximate the circle, with some minor deviations. This is because the Bresenham's algorithm uses only integer arithmetic, which can cause some rounding errors.

You can play around with the code and change the center coordinates and radius of the circle to see how the algorithm works for different inputs.

Post a Comment

To leave a comment, click the button below to sign in with Google.

Signup To My Newsletter

By subscribing, you will get one email every month on tips, tutorials, and resources to improve your skills as a developer. You will get early access to my courses and videos and also access to special bonus of the time. No spam, unsubscribe at any time





Subscribe
Contact
Contact form