Skip to content

Instantly share code, notes, and snippets.

@follower46
Created July 28, 2019 02:09
Show Gist options
  • Save follower46/dd4c0c1ee774d02c2f65d84b5084c21e to your computer and use it in GitHub Desktop.
Save follower46/dd4c0c1ee774d02c2f65d84b5084c21e to your computer and use it in GitHub Desktop.
import math
class infinite_bar():
def __init__(self, x, y, width, height, oled):
self.inited = False
self.x = x
self.y = y
self.height = height
self.width = width
self.oled = oled
self.phase = 0
self.band_width = 20
self.update()
self.inited = True
def update(self):
if not self.inited:
x_range = range(0, self.width + 1)
y_range = range(0, self.height + 1)
else:
x_range = range(1, self.width)
y_range = range(1, self.height)
for i in x_range:
for j in y_range:
if i == 0 or j == 0 or i == self.width or j == self.height:
if not self.inited:
# draw outline
self.oled.pixel(self.x + i, self.y + j, 1)
else:
# draw barbershop poll
self.oled.pixel(
self.x + i,
self.y + j,
self.get_band_color(i, j, self.phase)
)
# increase phase
self.phase = (self.phase + 1) % (self.band_width - 1)
def get_band_color(self, x, y, phase):
return ((phase + x + y) % self.band_width * 2) < self.band_width
class optimized_infinite_bar(infinite_bar):
def __init__(self, x, y, width, height, oled):
super().__init__(x, y, width, height, oled)
self.inited = True
def update(self):
if not self.inited:
# update unoptimized the first time (paint all pixels)
return super().update()
bar_count = math.ceil(self.width / self.band_width * 2)
for i in range(bar_count):
for j in range(0, self.height - 1):
x = i * self.band_width - j - self.phase
if 0 < x < self.width:
# draw left side
self.oled.pixel(
self.x + x,
self.y + j + 1,
1
)
x = x + math.floor(self.band_width / 2)
if 0 < x < self.width:
# draw right side
self.oled.pixel(
self.x + x,
self.y + j + 1,
0
)
# increase phase
self.phase = (self.phase + 1) % (self.band_width)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment