| 1 | package Math::Geometry::HexGrid; | ||
| 2 | |||
| 3 | 2 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use Exporter 'import'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 2 | our $VERSION = '0.01'; | |
| 9 | |||
| 10 | 2 | our @EXPORT_OK = qw( | |
| 11 | oddr_to_cube | ||
| 12 | cube_to_oddr | ||
| 13 | hex_distance | ||
| 14 | hex_is_collinear | ||
| 15 | hex_neighbors | ||
| 16 | hex_collinear_ray | ||
| 17 | ); | ||
| 18 | |||
| 19 | 2 | our %EXPORT_TAGS = ( | |
| 20 | all => \@EXPORT_OK, | ||
| 21 | ); | ||
| 22 | |||
| 23 | 2 | require XSLoader; | |
| 24 | 2 | XSLoader::load('Math::Geometry::HexGrid', $VERSION); | |
| 25 | |||
| 26 | # Load OO coordinate class (which in turn requires Math::Geometry::HexGrid) | ||
| 27 | 2 | ▶ | use Math::Geometry::HexGrid::Hex; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | |||
| 29 | 3 | ▶ | sub hex { |
|
Subroutine
hex: called CC=1 SCAR=0.0
Pod
documented
| |||
| 30 | 3 | my ($class, $col, $row) = @_; | |
| 31 | 3 | return Math::Geometry::HexGrid::Hex->new($col, $row); | |
| 32 | } | ||
| 33 | |||
| 34 | 2 | 1; | |
| 35 | |||
| 36 | =pod | ||
| 37 | |||
| 38 | =encoding utf8 | ||
| 39 | |||
| 40 | =head1 NAME | ||
| 41 | |||
| 42 | Math::Geometry::HexGrid - Fast, XS-accelerated hexagonal grid mathematics | ||
| 43 | |||
| 44 | =head1 SYNOPSIS | ||
| 45 | |||
| 46 | use Math::Geometry::HexGrid qw(:all); | ||
| 47 | |||
| 48 | # 1. High-Performance Functional Interface (Pure C Speed) | ||
| 49 | # Convert odd-r offset coordinates to 3D cube coordinates: | ||
| 50 | my ($q, $r, $s) = oddr_to_cube(15, 12); # (9, 12, -21) | ||
| 51 | my ($col, $row) = cube_to_oddr($q, $r, $s); | ||
| 52 | |||
| 53 | # Calculate exact Manhattan hex distance: | ||
| 54 | my $dist = hex_distance(0, 0, 15, 12); | ||
| 55 | |||
| 56 | # Check if two coordinates lie along a common cardinal hex axis: | ||
| 57 | if (hex_is_collinear($c1, $r1, $c2, $r2)) { | ||
| 58 | my @ray = hex_collinear_ray($c1, $r1, $c2, $r2); | ||
| 59 | for my $step (@ray) { | ||
| 60 | say "Step: $step->[0], $step->[1]"; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | # Fetch 6 immediate neighbors (respects odd-r row parity): | ||
| 65 | my @neighbors = hex_neighbors(4, 4); | ||
| 66 | |||
| 67 | # 2. Object-Oriented Interface (Ergonomic Value Objects) | ||
| 68 | my $h1 = Math::Geometry::HexGrid->hex(4, 4); | ||
| 69 | my $h2 = Math::Geometry::HexGrid->hex(10, 4); | ||
| 70 | |||
| 71 | say $h1->col; # 4 | ||
| 72 | say $h1->row; # 4 | ||
| 73 | say $h1->distance_to($h2); # 6 | ||
| 74 | say "$h1"; # Overloaded stringification: "4,4" | ||
| 75 | |||
| 76 | if ($h1 == $h2) { | ||
| 77 | # Overloaded equality in C | ||
| 78 | } | ||
| 79 | |||
| 80 | my @nbs = $h1->neighbors; # Returns Math::Geometry::HexGrid::Hex objects | ||
| 81 | |||
| 82 | =head1 DESCRIPTION | ||
| 83 | |||
| 84 | C<Math::Geometry::HexGrid> provides high-performance, exact hexagonal grid | ||
| 85 | mathematics implemented in ANSI C with Perl XS bindings. | ||
| 86 | |||
| 87 | Working with hexagonal grids in standard Cartesian C<($col, $row)> offset | ||
| 88 | coordinates is notoriously error-prone because movement vectors change depending | ||
| 89 | on row or column parity. By mapping offset coordinates to symmetric 3-axis | ||
| 90 | B<Cube Coordinates> C<($q, $r, s)> where C<$q + $r + $s == 0>, all hexagonal | ||
| 91 | operations (distance, direction, adjacency, and line-drawing) become simple, | ||
| 92 | branch-free linear algebra. | ||
| 93 | |||
| 94 | All mathematical operations use pure integer arithmetic, completely avoiding | ||
| 95 | floating-point roundoff errors and precision drift. | ||
| 96 | |||
| 97 | =head1 MATHEMATICAL FOUNDATIONS & ALGORITHMS | ||
| 98 | |||
| 99 | This module implements pointy-topped hexagonal grids with B<odd-r offset> | ||
| 100 | layout (where odd-numbered rows are shifted to the right by half a hex width). | ||
| 101 | |||
| 102 | =head2 1. Coordinate Systems | ||
| 103 | |||
| 104 | =over 4 | ||
| 105 | |||
| 106 | =item * B<Odd-R Offset Coordinates> C<($col, $row)>: | ||
| 107 | |||
| 108 | Standard 2D array coordinates convenient for map storage and display. | ||
| 109 | |||
| 110 | =item * B<Cube Coordinates> C<($q, $r, $s)>: | ||
| 111 | |||
| 112 | A 3D coordinate system where each axis corresponds to one of the three | ||
| 113 | primary hexagonal diagonal directions, constrained to the diagonal plane: | ||
| 114 | |||
| 115 | q + r + s = 0 | ||
| 116 | |||
| 117 | =back | ||
| 118 | |||
| 119 | =head2 2. Conversion Formulas | ||
| 120 | |||
| 121 | Conversion between odd-r offset and cube coordinates is an exact, reversible integer bijection: | ||
| 122 | |||
| 123 | =head3 Offset to Cube | ||
| 124 | |||
| 125 | q = col - floor((row - (row & 1)) / 2) | ||
| 126 | r = row | ||
| 127 | s = -q - r | ||
| 128 | |||
| 129 | Because C<row - (row & 1)> is always an even integer, dividing by 2 yields an | ||
| 130 | exact integer without fractional truncation. | ||
| 131 | |||
| 132 | =head3 Cube to Offset | ||
| 133 | |||
| 134 | col = q + floor((r - (r & 1)) / 2) | ||
| 135 | row = r | ||
| 136 | |||
| 137 | =head2 3. Hexagonal Distance | ||
| 138 | |||
| 139 | The shortest path distance between two hexes in a regular grid is defined by the | ||
| 140 | Manhattan metric in cube coordinates: | ||
| 141 | |||
| 142 | distance = max(|q1 - q2|, |r1 - r2|, |s1 - s2|) | ||
| 143 | |||
| 144 | Equivalently: | ||
| 145 | |||
| 146 | distance = (|q1 - q2| + |r1 - r2| + |s1 - s2|) / 2 | ||
| 147 | |||
| 148 | =head2 4. Adjacency & Direction Offsets | ||
| 149 | |||
| 150 | Every hexagon has exactly 6 neighbors. In cube coordinates, neighbor directions | ||
| 151 | are unit permutations of C<(+1, -1, 0)>: | ||
| 152 | |||
| 153 | Direction 0 (East): (+1, 0, -1) | ||
| 154 | Direction 1 (Northeast): (+1, -1, 0) [or (+1, -1) in odd rows] | ||
| 155 | Direction 2 (Northwest): ( 0, -1, +1) | ||
| 156 | Direction 3 (West): (-1, 0, +1) | ||
| 157 | Direction 4 (Southwest): (-1, +1, 0) | ||
| 158 | Direction 5 (Southeast): ( 0, +1, -1) | ||
| 159 | |||
| 160 | C<hex_neighbors> maps these offsets directly to odd-r coordinates taking row | ||
| 161 | parity into account. | ||
| 162 | |||
| 163 | =head2 5. Collinear Line-Drawing & Raycasting | ||
| 164 | |||
| 165 | Two hexes are B<collinear> if and only if they lie along a common straight line | ||
| 166 | parallel to one of the 3 cube axes: | ||
| 167 | |||
| 168 | q1 == q2 OR r1 == r2 OR s1 == s2 | ||
| 169 | |||
| 170 | C<hex_collinear_ray> traces intermediate hexes by interpolating along the shared | ||
| 171 | axis, rounding each step to integer coordinates. | ||
| 172 | |||
| 173 | =head1 FUNCTIONAL API | ||
| 174 | |||
| 175 | The following subroutines are available for export. Use the tag C<:all> to | ||
| 176 | import all of them: | ||
| 177 | |||
| 178 | use Math::Geometry::HexGrid qw(:all); | ||
| 179 | |||
| 180 | =head2 oddr_to_cube($col, $row) | ||
| 181 | |||
| 182 | my ($q, $r, $s) = oddr_to_cube($col, $row); | ||
| 183 | |||
| 184 | Converts 2D odd-r offset coordinates C<($col, $row)> to 3D cube coordinates | ||
| 185 | C<($q, $r, $s)>. Returns a 3-element list of integers. | ||
| 186 | |||
| 187 | =head2 cube_to_oddr($q, $r, $s) | ||
| 188 | |||
| 189 | my ($col, $row) = cube_to_oddr($q, $r, $s); | ||
| 190 | |||
| 191 | Converts 3D cube coordinates C<($q, $r, $s)> back to 2D odd-r offset coordinates. | ||
| 192 | Returns a 2-element list of integers C<($col, $row)>. | ||
| 193 | |||
| 194 | =head2 hex_distance($col1, $row1, $col2, $row2) | ||
| 195 | |||
| 196 | my $dist = hex_distance($col1, $row1, $col2, $row2); | ||
| 197 | |||
| 198 | Calculates the exact integer Manhattan hex distance between two hexes. | ||
| 199 | |||
| 200 | =head2 hex_is_collinear($col1, $row1, $col2, $row2) | ||
| 201 | |||
| 202 | if (hex_is_collinear($c1, $r1, $c2, $r2)) { ... } | ||
| 203 | |||
| 204 | Returns a boolean indicating whether the two hex coordinates share one of the 6 | ||
| 205 | cardinal hexagonal axes. Returns false if the coordinates are identical. | ||
| 206 | |||
| 207 | =head2 hex_neighbors($col, $row) | ||
| 208 | |||
| 209 | my @nbs = hex_neighbors($col, $row); | ||
| 210 | |||
| 211 | Returns a 6-element list of array references C<([$c, $r], ...)> representing | ||
| 212 | the immediate neighboring coordinates in clockwise order starting from East (direction 0). | ||
| 213 | |||
| 214 | =head2 hex_collinear_ray($col1, $row1, $col2, $row2, [$max_length = 100]) | ||
| 215 | |||
| 216 | my @ray = hex_collinear_ray($col1, $row1, $col2, $row2, $max_len); | ||
| 217 | |||
| 218 | Traces a straight line from C<($col1, $row1)> toward C<($col2, $row2)>. | ||
| 219 | Returns an empty list if the two points are not collinear. | ||
| 220 | Otherwise, returns a list of array references C<([$c, $r], ...)> beginning with | ||
| 221 | the first step past the origin, up to C<$max_length> steps or reaching the destination. | ||
| 222 | |||
| 223 | =head1 OBJECT-ORIENTED API | ||
| 224 | |||
| 225 | For higher-level application logic, C<Math::Geometry::HexGrid> provides a | ||
| 226 | lightweight value object class C<Math::Geometry::HexGrid::Hex>. | ||
| 227 | |||
| 228 | All OO methods are implemented in native C/XS with zero Perl method-call | ||
| 229 | overhead. | ||
| 230 | |||
| 231 | =head2 Math::Geometry::HexGrid->hex($col, $row) | ||
| 232 | |||
| 233 | =head2 Math::Geometry::HexGrid::Hex->new($col, $row) | ||
| 234 | |||
| 235 | my $hex = Math::Geometry::HexGrid->hex($col, $row); | ||
| 236 | |||
| 237 | Constructs and returns a new hex coordinate value object. | ||
| 238 | |||
| 239 | =head2 $hex->col | ||
| 240 | |||
| 241 | Returns the integer column coordinate. | ||
| 242 | |||
| 243 | =head2 $hex->row | ||
| 244 | |||
| 245 | Returns the integer row coordinate. | ||
| 246 | |||
| 247 | =head2 $hex->to_cube | ||
| 248 | |||
| 249 | my ($q, $r, $s) = $hex->to_cube; | ||
| 250 | |||
| 251 | Returns the 3D cube coordinates C<($q, $r, $s)> as a list. | ||
| 252 | |||
| 253 | =head2 $hex->distance_to($other) | ||
| 254 | |||
| 255 | my $d = $hex->distance_to($other_hex); | ||
| 256 | |||
| 257 | Returns the integer hex distance to another C<Math::Geometry::HexGrid::Hex> object. | ||
| 258 | |||
| 259 | =head2 $hex->is_collinear_with($other) | ||
| 260 | |||
| 261 | if ($hex->is_collinear_with($other)) { ... } | ||
| 262 | |||
| 263 | Returns true if both hex objects are collinear. | ||
| 264 | |||
| 265 | =head2 $hex->neighbors | ||
| 266 | |||
| 267 | my @neighbors = $hex->neighbors; | ||
| 268 | |||
| 269 | Returns a list of 6 new C<Math::Geometry::HexGrid::Hex> objects representing | ||
| 270 | the adjacent neighbors. | ||
| 271 | |||
| 272 | =head2 $hex->ray_to($other, [$max_length = 100]) | ||
| 273 | |||
| 274 | my @ray = $hex->ray_to($other, 5); | ||
| 275 | |||
| 276 | Returns a list of C<Math::Geometry::HexGrid::Hex> objects representing the | ||
| 277 | collinear ray toward C<$other>. | ||
| 278 | |||
| 279 | =head2 $hex->equals($other) | ||
| 280 | |||
| 281 | Returns true if both coordinates have identical column and row values. | ||
| 282 | |||
| 283 | =head2 Overloaded Operators | ||
| 284 | |||
| 285 | The C<Math::Geometry::HexGrid::Hex> class overloads the following operators: | ||
| 286 | |||
| 287 | =over 4 | ||
| 288 | |||
| 289 | =item * B<Stringification> (C<"">): Formats as C<"col,row"> (e.g. C<"4,7">), making hex objects natural hash keys. | ||
| 290 | |||
| 291 | =item * B<Equality> (C<==>): Checks exact integer equality in native C. | ||
| 292 | |||
| 293 | =item * B<Inequality> (C<!=>): Inverted equality check. | ||
| 294 | |||
| 295 | =back | ||
| 296 | |||
| 297 | =head1 SEE ALSO | ||
| 298 | |||
| 299 | =over 4 | ||
| 300 | |||
| 301 | =item * Red Blob Games Hexagonal Grids Guide: L<https://www.redblobgames.com/grids/hexagons/> | ||
| 302 | |||
| 303 | The canonical reference for hexagonal grid mathematics and algorithms. | ||
| 304 | |||
| 305 | =back | ||
| 306 | |||
| 307 | =head1 AUTHOR | ||
| 308 | |||
| 309 | Steffen Mueller E<lt>cpan@steffen-mueller.netE<gt> | ||
| 310 | |||
| 311 | =head1 COPYRIGHT AND LICENSE | ||
| 312 | |||
| 313 | Copyright (C) 2026 Steffen Mueller | ||
| 314 | |||
| 315 | This software is released under the MIT License. See the F<LICENSE> file included | ||
| 316 | with this distribution for full license details. | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||