Skip to content

Instantly share code, notes, and snippets.

@samneggs
Created December 15, 2021 04:57
Show Gist options
  • Save samneggs/3f2c77bf59a413cb421e8e2949f5f656 to your computer and use it in GitHub Desktop.
Save samneggs/3f2c77bf59a413cb421e8e2949f5f656 to your computer and use it in GitHub Desktop.
Fast Circle Using Viper Pointers
@micropython.viper
def circle(x0:int, y0:int, radius:int):
# From Adafruit GFX Arduino library
# Circle drawing function. Will draw a single pixel wide circle with
# center at x0, y0 and the specified radius.
# screen is bytearray (240x240)
screen_addr=ptr16(screen)
color=0xffff
f = 1 - radius
ddF_x = 1
ddF_y = -2 * radius
x = 0
y = radius
y1=(y0+radius)*240
y2=(y0-radius)*240
screen_addr[y1+x0]=color
screen_addr[y2+x0]=color
y3=y0*240
screen_addr[y3+x0+radius]=color
screen_addr[y3+x0-radius]=color
#LCD.pixel(x0, y0 + radius,color)
#LCD.pixel(x0, y0 - radius,color)
#LCD.pixel(x0 + radius, y0,color)
#LCD.pixel(x0 - radius, y0,color)
while x < y:
if f >= 0:
y -= 1
ddF_y += 2
f += ddF_y
x += 1
ddF_x += 2
f += ddF_x
x1=x0+x
x2=x0-x
y1=y0+y
y2=y0-y
screen_addr[y1*240+x1]=color
screen_addr[y1*240+x2]=color
screen_addr[y2*240+x1]=color
screen_addr[y2*240+x2]=color
x1=x0+y
x2=x0-y
y1=y0+x
y2=y0-x
screen_addr[y1*240+x1]=color
screen_addr[y1*240+x2]=color
screen_addr[y2*240+x1]=color
screen_addr[y2*240+x2]=color
#LCD.pixel(x0 + x, y0 + y,color)
#LCD.pixel(x0 - x, y0 + y,color)
#LCD.pixel(x0 + x, y0 - y,color)
#LCD.pixel(x0 - x, y0 - y,color)
#LCD.pixel(x0 + y, y0 + x,color)
#LCD.pixel(x0 - y, y0 + x,color)
#LCD.pixel(x0 + y, y0 - x,color)
#LCD.pixel(x0 - y, y0 - x,color)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment