1 # Copyright 2023 highstreet technologies GmbH
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from network_generation.model.python.hexagon import (
21 import network_generation.model.python.hexagon as Hexagon
22 from network_generation.model.python.point import Point
27 def complain(name: str) -> None:
28 print("FAIL {0}".format(name))
31 def equal_hex(name: str, a: Hex, b: Hex) -> None:
32 if not (a.q == b.q and a.s == b.s and a.r == b.r):
36 def equal_offsetcoord(name: str, a: OffsetCoord, b: OffsetCoord) -> None:
37 if not (a.col == b.col and a.row == b.row):
41 def equal_doubledcoord(name: str, a: DoubledCoord, b: DoubledCoord) -> None:
42 if not (a.col == b.col and a.row == b.row):
46 def equal_int(name: str, a: int, b: int) -> None:
51 def equal_hex_array(name: str, a: list[Hex], b: list[Hex]) -> None:
52 equal_int(name, len(a), len(b))
53 for i in range(0, len(a)):
54 equal_hex(name, a[i], b[i])
57 def test_hex_arithmetic() -> None:
61 Hexagon.hex_add(Hex(1, -3, 2), Hex(3, -7, 4)),
66 Hexagon.hex_subtract(Hex(1, -3, 2), Hex(3, -7, 4)),
70 def test_hex_direction() -> None:
71 equal_hex("hex_direction", Hex(0, -1, 1), Hexagon.hex_direction(2))
74 def test_hex_neighbor() -> None:
76 "hex_neighbor", Hex(1, -3, 2), Hexagon.hex_neighbor(Hex(1, -2, 1), 2)
80 def test_hex_diagonal() -> None:
84 Hexagon.hex_diagonal_neighbor(Hex(1, -2, 1), 3),
88 def test_hex_distance() -> None:
92 int(Hexagon.hex_distance(Hex(3, -7, 4), Hex(0, 0, 0))),
96 def test_hex_rotate_right() -> None:
99 Hexagon.hex_rotate_right(Hex(1, -3, 2)),
104 def test_hex_rotate_left() -> None:
107 Hexagon.hex_rotate_left(Hex(1, -3, 2)),
112 def test_hex_round() -> None:
120 Hexagon.hex_lerp(Hex(0, 0, 0), Hex(10, -20, 10), 0.5)
125 Hexagon.hex_round(a),
126 Hexagon.hex_round(Hexagon.hex_lerp(a, b, 0.499)),
130 Hexagon.hex_round(b),
131 Hexagon.hex_round(Hexagon.hex_lerp(a, b, 0.501)),
135 Hexagon.hex_round(a),
138 a.q * 0.4 + b.q * 0.3 + c.q * 0.3,
139 a.r * 0.4 + b.r * 0.3 + c.r * 0.3,
140 a.s * 0.4 + b.s * 0.3 + c.s * 0.3,
146 Hexagon.hex_round(c),
149 a.q * 0.3 + b.q * 0.3 + c.q * 0.4,
150 a.r * 0.3 + b.r * 0.3 + c.r * 0.4,
151 a.s * 0.3 + b.s * 0.3 + c.s * 0.4,
157 def test_hex_linedraw() -> None:
168 Hexagon.hex_linedraw(Hex(0, 0, 0), Hex(1, -5, 4)),
172 def test_layout() -> None:
174 flat = Layout(Hexagon.layout_flat, Point(10.0, 15.0), Point(35.0, 71.0))
179 Hexagon.pixel_to_hex(flat, Hexagon.hex_to_pixel(flat, h))
183 Hexagon.layout_pointy, Point(10.0, 15.0), Point(35.0, 71.0)
189 Hexagon.pixel_to_hex(pointy, Hexagon.hex_to_pixel(pointy, h))
194 def test_offset_roundtrip() -> None:
196 b = OffsetCoord(1, -3)
198 "conversion_roundtrip even-q",
200 Hexagon.qoffset_to_cube(
202 Hexagon.qoffset_from_cube(Hexagon.EVEN, a),
206 "conversion_roundtrip even-q",
208 Hexagon.qoffset_from_cube(
210 Hexagon.qoffset_to_cube(Hexagon.EVEN, b),
214 "conversion_roundtrip odd-q",
216 Hexagon.qoffset_to_cube(
218 Hexagon.qoffset_from_cube(Hexagon.ODD, a),
222 "conversion_roundtrip odd-q",
224 Hexagon.qoffset_from_cube(
225 Hexagon.ODD, Hexagon.qoffset_to_cube(Hexagon.ODD, b)
229 "conversion_roundtrip even-r",
231 Hexagon.roffset_to_cube(
232 Hexagon.EVEN, Hexagon.roffset_from_cube(Hexagon.EVEN, a)
236 "conversion_roundtrip even-r",
238 Hexagon.roffset_from_cube(
239 Hexagon.EVEN, Hexagon.roffset_to_cube(Hexagon.EVEN, b)
243 "conversion_roundtrip odd-r",
245 Hexagon.roffset_to_cube(
246 Hexagon.ODD, Hexagon.roffset_from_cube(Hexagon.ODD, a)
250 "conversion_roundtrip odd-r",
252 Hexagon.roffset_from_cube(
253 Hexagon.ODD, Hexagon.roffset_to_cube(Hexagon.ODD, b)
258 def test_offset_from_cube() -> None:
260 "offset_from_cube even-q",
262 Hexagon.qoffset_from_cube(Hexagon.EVEN, Hex(1, 2, -3)),
265 "offset_from_cube odd-q",
267 Hexagon.qoffset_from_cube(Hexagon.ODD, Hex(1, 2, -3)),
271 def test_offset_to_cube() -> None:
273 "offset_to_cube even-",
275 Hexagon.qoffset_to_cube(Hexagon.EVEN, OffsetCoord(1, 3)),
278 "offset_to_cube odd-q",
280 Hexagon.qoffset_to_cube(Hexagon.ODD, OffsetCoord(1, 2)),
284 def test_doubled_roundtrip() -> None:
286 b = DoubledCoord(1, -3)
288 "conversion_roundtrip doubled-q",
290 Hexagon.qdoubled_to_cube(Hexagon.qdoubled_from_cube(a)),
293 "conversion_roundtrip doubled-q",
295 Hexagon.qdoubled_from_cube(Hexagon.qdoubled_to_cube(b)),
298 "conversion_roundtrip doubled-r",
300 Hexagon.rdoubled_to_cube(Hexagon.rdoubled_from_cube(a)),
303 "conversion_roundtrip doubled-r",
305 Hexagon.rdoubled_from_cube(Hexagon.rdoubled_to_cube(b)),
309 def test_doubled_from_cube() -> None:
311 "doubled_from_cube doubled-q",
313 Hexagon.qdoubled_from_cube(Hex(1, 2, -3)),
316 "doubled_from_cube doubled-r",
318 Hexagon.rdoubled_from_cube(Hex(1, 2, -3)),
322 def test_doubled_to_cube() -> None:
324 "doubled_to_cube doubled-q",
326 Hexagon.qdoubled_to_cube(DoubledCoord(1, 5)),
329 "doubled_to_cube doubled-r",
331 Hexagon.rdoubled_to_cube(DoubledCoord(4, 2)),
335 def test_hexagon() -> None:
336 test_hex_arithmetic()
341 test_hex_rotate_right()
342 test_hex_rotate_left()
346 test_offset_roundtrip()
347 test_offset_from_cube()
348 test_offset_to_cube()
349 test_doubled_roundtrip()
350 test_doubled_from_cube()
351 test_doubled_to_cube()