2Generates the hexegon using math.
6from dataclasses
import dataclass
11from math
import pi, cos, sin
15LED_DIAMETER = SPACE_PER_LED / 4
18SMALLEST_ANGLE = 360 / 6
30def toRads(angle: float) -> float:
31 return angle * (pi / 180)
40 def toJson(points: list[
"Point"]) -> list[dict]:
41 x_values = [p.x
for p
in points]
42 y_values = [p.y
for p
in points]
44 x_values = [round(x, 4)
for x
in x_values]
45 y_values = [round(y, 4)
for y
in y_values]
47 x_values = [-x
for x
in x_values]
49 return {
"x": x_values,
"y": y_values,
"diameter": LED_DIAMETER}
51 def copy(self) -> "Point":
54 def __repr__(self) -> str:
55 x_rounded = round(self.
x, 2)
56 y_rounded = round(self.
y, 2)
57 return f
"({x_rounded}, {y_rounded})"
60def next_point(pos: Point, angle: HexagonAngle, space: float) -> Point:
62 angle_rad = toRads(degrees)
63 x = pos.x + space * cos(angle_rad)
64 y = pos.y + space * sin(angle_rad)
69 input: list[HexagonAngle], leds_per_strip: int, startPos: Point,
70 exclude: list[int] |
None =
None,
71 add_last: bool =
False
73 points: list[Point] = []
74 if (
not input)
or (
not leds_per_strip):
76 exclude = exclude
or []
78 curr_point: Point =
Point(startPos.x, startPos.y)
81 for i,angle
in enumerate(input):
82 excluded = i
in exclude
83 values = list(range(leds_per_strip))
87 curr_point = next_point(curr_point, angle, SPACE_PER_LED)
89 points.append(curr_point)
93 curr_point = next_point(curr_point, last_angle, SPACE_PER_LED)
97 points.append(curr_point)
103 startPos =
Point(0, 0)
106 HexagonAngle.RIGHT_UP,
107 HexagonAngle.RIGHT_DOWN,
109 HexagonAngle.LEFT_DOWN,
110 HexagonAngle.LEFT_UP,
112 points = gen_points(hexagon_angles, LED_PER_STRIP, startPos)
118def simple_test() -> None:
119 startPos =
Point(0, 0)
123 points = gen_points(hexagon_angles, LED_PER_STRIP, startPos)
127def two_angle_test() -> None:
128 startPos =
Point(0, 0)
133 points = gen_points(hexagon_angles, LED_PER_STRIP, startPos)
139def two_angle_test2() -> None:
140 print(
"two_angle_test2")
141 startPos =
Point(0, 0)
146 points = gen_points(hexagon_angles, LED_PER_STRIP, startPos)
151def find_red_anchor_point() -> list[Point]:
153 HexagonAngle.LEFT_UP,
154 HexagonAngle.LEFT_UP,
156 HexagonAngle.RIGHT_UP,
158 points = gen_points(hexagon_angles, LED_PER_STRIP,
Point(0, 0), add_last=
True)
161def find_green_anchore_point() -> list[Point]:
163 HexagonAngle.RIGHT_UP,
164 HexagonAngle.RIGHT_UP,
167 points = gen_points(hexagon_angles, LED_PER_STRIP,
Point(0, 0), add_last=
True)
171RED_ANCHOR_POINT = find_red_anchor_point()[-1]
172BLACK_ANCHOR_POINT =
Point(0,0)
173GREEN_ANCHOR_POINT = find_green_anchore_point()[-1]
174BLUE_ANCHOR_POINT =
Point(0, 0)
177def generate_red_points() -> list[Point]:
178 starting_point = RED_ANCHOR_POINT.copy()
181 HexagonAngle.LEFT_UP,
182 HexagonAngle.LEFT_DOWN,
184 HexagonAngle.RIGHT_DOWN,
188 points = gen_points(hexagon_angles, LED_PER_STRIP, starting_point, exclude=[5])
192def generate_black_points() -> list[Point]:
193 starting_point = BLACK_ANCHOR_POINT.copy()
195 HexagonAngle.LEFT_UP,
196 HexagonAngle.LEFT_UP,
198 HexagonAngle.RIGHT_UP,
199 HexagonAngle.RIGHT_DOWN,
201 HexagonAngle.LEFT_DOWN,
203 HexagonAngle.LEFT_UP,
205 HexagonAngle.RIGHT_UP,
207 points = gen_points(hexagon_angles, LED_PER_STRIP, starting_point)
211def generate_green_points() -> list[Point]:
212 starting_point = GREEN_ANCHOR_POINT.copy()
214 HexagonAngle.RIGHT_UP,
216 HexagonAngle.LEFT_UP,
217 HexagonAngle.LEFT_DOWN,
219 HexagonAngle.RIGHT_DOWN,
220 HexagonAngle.LEFT_DOWN,
221 HexagonAngle.LEFT_UP,
223 HexagonAngle.RIGHT_UP,
224 HexagonAngle.LEFT_UP,
225 HexagonAngle.LEFT_DOWN,
226 HexagonAngle.RIGHT_DOWN,
227 HexagonAngle.RIGHT_UP,
228 HexagonAngle.RIGHT_DOWN,
230 points = gen_points(hexagon_angles, LED_PER_STRIP, starting_point, exclude=[5,6,13])
233def generate_blue_points() -> list[Point]:
234 starting_point = BLUE_ANCHOR_POINT.copy()
236 HexagonAngle.RIGHT_UP,
237 HexagonAngle.RIGHT_UP,
239 HexagonAngle.LEFT_UP,
240 HexagonAngle.LEFT_DOWN,
241 HexagonAngle.LEFT_DOWN,
242 HexagonAngle.RIGHT_DOWN,
243 HexagonAngle.RIGHT_DOWN,
245 HexagonAngle.RIGHT_UP,
247 HexagonAngle.RIGHT_UP,
249 points = gen_points(hexagon_angles, LED_PER_STRIP, starting_point, exclude=[6])
252def unit_test() -> None:
256 map = out.setdefault(
"map", {})
258 "red_segment": Point.toJson(generate_red_points()),
259 "back_segment": Point.toJson(generate_black_points()),
260 "green_segment": Point.toJson(generate_green_points()),
261 "blue_segment": Point.toJson(generate_blue_points()),
263 print(json.dumps(out))
265 with open(
"output.json",
"w")
as f:
266 f.write(json.dumps(out))
269if __name__ ==
"__main__":