#!/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 offsetCanvas = 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: offsetCanvas.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: offsetCanvas.SetPixel(rot_x + cent_x, rot_y + cent_y, 0, 0, 0) offsetCanvas = self.matrix.SwapOnVSync(offsetCanvas) # Main function if __name__ == "__main__": parser = RotatingBlockGenerator() if (not parser.process()): parser.print_help()