mirror of
https://github.com/Hopiu/rpi-rgb-led-matrix.git
synced 2026-03-16 22:10:27 +00:00
60 lines
2.2 KiB
Python
Executable file
60 lines
2.2 KiB
Python
Executable file
#!/usr/bin/env python
|
|
from samplebase import SampleBase
|
|
import math
|
|
|
|
|
|
class RotatingBlockGenerator(SampleBase):
|
|
def __init__(self, *args, **kwargs):
|
|
super(RotatingBlockGenerator, self).__init__(*args, **kwargs)
|
|
|
|
def rotate(self, x, y, angle):
|
|
return {
|
|
"new_x": x * math.cos(angle) - y * math.sin(angle),
|
|
"new_y": x * math.sin(angle) + y * math.cos(angle)
|
|
}
|
|
|
|
def scale_col(self, val, lo, hi):
|
|
if val < lo:
|
|
return 0
|
|
if val > hi:
|
|
return 255
|
|
return 255 * (val - lo) / (hi - lo)
|
|
|
|
def run(self):
|
|
cent_x = self.matrix.width / 2
|
|
cent_y = self.matrix.height / 2
|
|
|
|
rotate_square = min(self.matrix.width, self.matrix.height) * 1.41
|
|
min_rotate = cent_x - rotate_square / 2
|
|
max_rotate = cent_x + rotate_square / 2
|
|
|
|
display_square = min(self.matrix.width, self.matrix.height) * 0.7
|
|
min_display = cent_x - display_square / 2
|
|
max_display = cent_x + display_square / 2
|
|
|
|
deg_to_rad = 2 * 3.14159265 / 360
|
|
rotation = 0
|
|
offset_canvas = self.matrix.CreateFrameCanvas()
|
|
|
|
while True:
|
|
rotation += 1
|
|
rotation %= 360
|
|
|
|
for x in range(int(min_rotate), int(max_rotate)):
|
|
for y in range(int(min_rotate), int(max_rotate)):
|
|
ret = self.rotate(x - cent_x, y - cent_x, deg_to_rad * rotation)
|
|
rot_x = ret["new_x"]
|
|
rot_y = ret["new_y"]
|
|
|
|
if x >= min_display and x < max_display and y >= min_display and y < max_display:
|
|
offset_canvas.SetPixel(rot_x + cent_x, rot_y + cent_y, self.scale_col(x, min_display, max_display), 255 - self.scale_col(y, min_display, max_display), self.scale_col(y, min_display, max_display))
|
|
else:
|
|
offset_canvas.SetPixel(rot_x + cent_x, rot_y + cent_y, 0, 0, 0)
|
|
|
|
offset_canvas = self.matrix.SwapOnVSync(offset_canvas)
|
|
|
|
# Main function
|
|
if __name__ == "__main__":
|
|
rotating_block_generator = RotatingBlockGenerator()
|
|
if (not rotating_block_generator.process()):
|
|
rotating_block_generator.print_help()
|