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 # inspired by http://www.redblobgames.com/grids/hexagons/
18 from network_generation.model.python.hexagon import Hex
23 def direction_vectors() -> list[Hex]:
35 def direction(direction: int) -> Hex:
36 if direction < 0 or direction > 5:
38 "Invalid direction. The direction value must be"
39 + " in the range of [0..5]."
41 return Cube.direction_vectors()[direction]
44 def add(hex: Hex, vec: Hex) -> Hex:
45 return Hex(hex.q + vec.q, hex.r + vec.r, hex.s + vec.s)
48 def neighbor(cube: Hex, direction: int) -> Hex:
49 return Cube.add(cube, Cube.direction(direction))
52 def scale(hex: Hex, factor: int) -> Hex:
53 return Hex(hex.q * factor, hex.r * factor, hex.s * factor)
56 def ring(center: Hex, radius: int) -> list[Hex]:
59 "Invalid radius. The radius around the hex center must"
60 + " be greater than 0 rings."
62 results: list[Hex] = []
63 hex: Hex = Cube.add(center, Cube.scale(Cube.direction(4), radius))
65 for j in range(radius):
67 hex = Cube.neighbor(hex, i)
71 def spiral(center: Hex, radius: int) -> list[Hex]:
72 result: list[Hex] = [center]
73 for k in range(1, radius + 1):
74 result.extend(Cube.ring(center, k))