-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex_symmetry.py
More file actions
55 lines (35 loc) · 1.28 KB
/
hex_symmetry.py
File metadata and controls
55 lines (35 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from __future__ import annotations
from typing import Callable
Point = tuple[int, int]
Cube = tuple[int, int, int]
def ax_to_cube(p: Point) -> Cube:
q, r = p
return (q, -q - r, r)
def cube_to_ax(c: Cube) -> Point:
x, _y, z = c
return (x, z)
def _refl(c: Cube) -> Cube:
x, y, z = c
return (x, z, y)
def _rot_k(k: int) -> Callable[[Cube], Cube]:
def f(v: Cube) -> Cube:
x, y, z = v
for _ in range(k):
x, y, z = (-z, -x, -y)
return (x, y, z)
return f
def _compose(f: Callable[[Cube], Cube], g: Callable[[Cube], Cube]) -> Callable[[Cube], Cube]:
return lambda v: f(g(v))
ROTATIONS: tuple[Callable[[Cube], Cube], ...] = tuple(_rot_k(k) for k in range(6))
D6: tuple[Callable[[Cube], Cube], ...] = ROTATIONS + tuple(_compose(_refl, r) for r in ROTATIONS)
def inverse_transform_id(transform_id: int) -> int:
if 0 <= transform_id < 6:
return (6 - transform_id) % 6
if 6 <= transform_id < 12:
return transform_id
raise ValueError(f"Bad transform id: {transform_id}")
def apply_transform_ax(p: Point, transform_id: int) -> Point:
if not (0 <= transform_id < len(D6)):
raise ValueError(f"Bad transform id: {transform_id}")
t = D6[transform_id]
return cube_to_ax(t(ax_to_cube(p)))