hexx 0.24.0

Hexagonal utilities
Documentation
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
use super::{EdgeDirection, Hex, VertexDirection, iter::ExactSizeHexIterator};

impl Hex {
    #[must_use]
    #[expect(clippy::cast_possible_wrap)]
    /// Retrieves one [`Hex`] ring around `self` in a given `range`.
    /// The returned coordinates start from `start_dir` and loop counter
    /// clockwise around `self` unless `clockwise` is set to `true`.
    ///
    /// > If you only need the coordinates see [`Self::ring`]
    ///
    /// # Note
    /// The returned iterator will have `6 * range` ([`Self::ring_count`])
    /// items, unless `range` is 0 which will return `self`
    pub fn custom_ring(
        self,
        range: u32,
        start_dir: EdgeDirection,
        clockwise: bool,
    ) -> impl ExactSizeIterator<Item = Self> {
        let mut directions = vec![Self::ZERO];
        if range > 0 {
            let mut neighbors = Self::NEIGHBORS_COORDS;
            // TODO: improve code clarity
            neighbors.rotate_left(start_dir.index() as usize);
            if clockwise {
                neighbors.reverse();
                neighbors.rotate_left(1);
            } else {
                neighbors.rotate_left(2);
            }
            directions.extend(
                neighbors
                    .into_iter()
                    .flat_map(|dir| (0..range).map(move |_| dir)),
            );
        }
        let count = Self::ring_count(range);
        let point = self + start_dir * range as i32;
        let iter = (0..count).scan(point, move |point, i| {
            *point += directions[i];
            Some(*point)
        });
        ExactSizeHexIterator { iter, count }
    }

    #[must_use]
    /// Retrieves one [`Hex`] ring around `self` in a given `range`.
    /// The returned coordinates start from [`EdgeDirection::default`]
    /// and loop around `self` counter clockwise.
    ///
    /// > See [`Self::custom_ring`] for more options.
    ///
    /// # Note
    /// The returned iterator will have `6 * range` ([`Self::ring_count`])
    /// items, unless `range` is 0 which will return `self`
    pub fn ring(self, range: u32) -> impl ExactSizeIterator<Item = Self> {
        self.custom_ring(range, EdgeDirection::default(), false)
    }

    /// Retrieves `range` [`Hex`] rings around `self` in a given `range`.
    /// The returned coordinates start from [`EdgeDirection::default`]
    /// and loop around `self` counter clockwise.
    ///
    /// See [`Self::custom_rings`] for more options.
    /// If you only need the coordinates see [`Self::spiral_range`].
    ///
    /// # Example
    ///
    /// ```rust
    /// # use hexx::*;
    /// let rings: Vec<Vec<Hex>> = Hex::ZERO.rings(3..10).collect();
    /// assert_eq!(rings.len(), 7);
    /// ```
    pub fn rings(self, range: impl Iterator<Item = u32>) -> impl Iterator<Item = Vec<Self>> {
        range.map(move |r| self.ring(r).collect())
    }

    /// Retrieves `range` [`Hex`] rings around `self` in a given `range`.
    /// The returned coordinates start from `start_dir` and loop around `self`
    /// counter clockwise unless `clockwise` is set to true.
    ///
    /// If you only need the coordinates see [`Self::spiral_range`] or
    /// [`Self::rings`].
    ///
    /// # Example
    ///
    /// ```rust
    /// # use hexx::*;
    /// let rings: Vec<Vec<Hex>> = Hex::ZERO
    ///     .custom_rings(3..10, EdgeDirection::FLAT_TOP, true)
    ///     .collect();
    /// assert_eq!(rings.len(), 7);
    /// ```
    pub fn custom_rings(
        self,
        range: impl Iterator<Item = u32>,
        start_dir: EdgeDirection,
        clockwise: bool,
    ) -> impl Iterator<Item = Vec<Self>> {
        range.map(move |r| self.custom_ring(r, start_dir, clockwise).collect())
    }

    #[must_use]
    /// Retrieves one [`Hex`] ring edge around `self` in a given `radius` and
    /// `direction`. The returned coordinates are sorted counter clockwise
    /// unless `clockwise` is set to `true`.
    ///
    /// If you only need the coordinates see [`Self::ring_edge`].
    ///
    /// # Note
    /// The returned vector will be of `radius + 1` length
    pub fn custom_ring_edge(
        self,
        radius: u32,
        direction: VertexDirection,
        clockwise: bool,
    ) -> impl ExactSizeIterator<Item = Self> {
        let [start_dir, end_dir] = Self::__vertex_dir_to_edge_dir(direction, clockwise);
        self.__ring_edge(radius, radius, start_dir, end_dir)
    }

    #[inline]
    fn __vertex_dir_to_edge_dir(direction: VertexDirection, clockwise: bool) -> [EdgeDirection; 2] {
        if clockwise {
            let dir = direction.direction_ccw();
            [dir, dir >> 2]
        } else {
            let dir = direction.direction_cw();
            [dir, dir << 2]
        }
    }

    /// Computes an `origin` as `self + start_dir * dist`
    /// and computes a line between `origin` and `origin + len * end_dir`
    #[expect(clippy::cast_possible_wrap)]
    fn __ring_edge(
        self,
        dist: u32,
        len: u32,
        start_dir: EdgeDirection,
        end_dir: EdgeDirection,
    ) -> impl ExactSizeIterator<Item = Self> {
        let p = self + start_dir * dist as i32;
        ExactSizeHexIterator {
            iter: (0..=len).map(move |i| p + end_dir * i as i32),
            count: len as usize + 1,
        }
    }

    #[must_use]
    /// Retrieves one [`Hex`] ring edge around `self` in a given `radius` and
    /// `direction`. The returned coordinates are sorted counter clockwise
    /// around `self`.
    ///
    /// See [`Self::custom_ring_edge`] for more options.
    ///
    /// # Note
    /// The returned vector will be of `radius + 1` length
    pub fn ring_edge(
        self,
        radius: u32,
        direction: VertexDirection,
    ) -> impl ExactSizeIterator<Item = Self> {
        self.custom_ring_edge(radius, direction, false)
    }

    /// Retrieves all successive [`Hex`] ring edges around `self` in given
    /// `ranges` and `direction`.
    /// The returned edges coordinates are sorted counter clockwise around
    /// `self`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use hexx::*;
    /// let edges: Vec<_> = Hex::ZERO
    ///     .ring_edges(3..10, VertexDirection::FLAT_RIGHT)
    ///     .collect();
    /// assert_eq!(edges.len(), 7);
    /// ```
    ///
    /// See also [`Self::custom_ring_edges`]
    /// If you only need the coordinates see [`Self::custom_wedge`]
    pub fn ring_edges(
        self,
        ranges: impl Iterator<Item = u32>,
        direction: VertexDirection,
    ) -> impl Iterator<Item = impl ExactSizeIterator<Item = Self>> {
        let [start_dir, end_dir] = Self::__vertex_dir_to_edge_dir(direction, false);
        ranges.map(move |r| self.__ring_edge(r, r, start_dir, end_dir))
    }

    /// Retrieves all successive [`Hex`] ring edges around `self` in given
    /// `ranges` and `direction`.
    /// The returned edges coordinates are sorted counter clockwise around
    /// `self` unless `clockwise` is set to `true`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use hexx::*;
    /// let edges: Vec<_> = Hex::ZERO
    ///     .custom_ring_edges(3..10, VertexDirection::FLAT_RIGHT, true)
    ///     .collect();
    /// assert_eq!(edges.len(), 7);
    /// ```
    ///
    /// See also [`Self::ring_edges`]
    /// If you only need the coordinates see [`Self::wedge`]
    pub fn custom_ring_edges(
        self,
        ranges: impl Iterator<Item = u32>,
        direction: VertexDirection,
        clockwise: bool,
    ) -> impl Iterator<Item = impl ExactSizeIterator<Item = Self>> {
        let [start_dir, end_dir] = Self::__vertex_dir_to_edge_dir(direction, clockwise);
        ranges.map(move |r| self.__ring_edge(r, r, start_dir, end_dir))
    }

    /// Retrieves all successive [`Hex`] ring edges around `self` in given
    /// `ranges` and `direction`.
    /// The returned edges coordinates are sorted counter clockwise around
    /// `self` unless `clockwise` is set to `true`.
    ///
    /// See also [`Self::custom_ring_edges`]
    /// If you only need the coordinates see [`Self::wedge`]
    /// If you want a full wedge see [`Self::custom_full_wedge`]
    pub fn custom_wedge(
        self,
        ranges: impl Iterator<Item = u32>,
        direction: VertexDirection,
        clockwise: bool,
    ) -> impl Iterator<Item = Self> {
        self.custom_ring_edges(ranges, direction, clockwise)
            .flatten()
    }

    /// Retrieves all successive [`Hex`] ring edges from `self` to `rhs`
    /// The returned edges coordinates are sorted counter clockwise around
    /// `self` unless `clockwise` is set to `true`.
    ///
    /// See also [`Self::custom_ring_edges`] and [`Self::wedge_to`]
    #[must_use]
    pub fn custom_wedge_to(
        self,
        rhs: Self,
        clockwise: bool,
    ) -> impl ExactSizeIterator<Item = Self> {
        let range = self.unsigned_distance_to(rhs);
        let direction = self.diagonal_way_to(rhs).unwrap();
        self.custom_full_wedge(range, direction, clockwise)
    }

    /// Retrieves all successive [`Hex`] ring edges around `self` in a given
    /// `range` and `direction` The returned edges coordinates are sorted
    /// counter clockwise around `self` unless `clockwise` is set to `true`.
    ///
    /// See also [`Self::custom_wedge`] and [`Self::full_wedge`]
    #[must_use]
    pub fn custom_full_wedge(
        self,
        range: u32,
        direction: VertexDirection,
        clockwise: bool,
    ) -> impl ExactSizeIterator<Item = Self> {
        ExactSizeHexIterator {
            iter: self.custom_wedge(0..=range, direction, clockwise),
            count: Self::wedge_count(range) as usize,
        }
    }

    /// Counts how many coordinates there are in a wedge of given `range`
    ///
    /// # Example
    ///
    /// ```rust
    /// # use hexx::*;
    /// let point = Hex::new(3, -6);
    /// let wedge: Vec<Hex> = point.wedge(0..=13, VertexDirection::FLAT_RIGHT).collect();
    /// assert_eq!(wedge.len(), Hex::wedge_count(13) as usize);
    /// ```
    #[inline]
    #[must_use]
    pub const fn wedge_count(range: u32) -> u32 {
        range * (range + 3) / 2 + 1
    }

    /// Retrieves all successive [`Hex`] ring edges around `self` in a given
    /// `range` and `direction`.
    /// The returned edges coordinates are sorted counter clockwise around
    /// `self`.
    ///
    /// See also [`Self::custom_ring_edges`] and [`Self::custom_wedge`]
    pub fn wedge(
        self,
        range: impl Iterator<Item = u32>,
        direction: VertexDirection,
    ) -> impl Iterator<Item = Self> {
        self.ring_edges(range, direction).flatten()
    }

    /// Retrieves all successive [`Hex`] ring edges from `self` to `rhs`
    /// The returned edges coordinates are sorted counter clockwise.
    ///
    /// See also [`Self::custom_ring_edges`] and [`Self::custom_wedge_to`]
    #[must_use]
    pub fn wedge_to(self, rhs: Self) -> impl ExactSizeIterator<Item = Self> {
        self.custom_wedge_to(rhs, false)
    }

    /// Retrieves all successive [`Hex`] ring edges around `self` in a given
    /// `range` and `direction` The returned edges coordinates are sorted
    /// counter clockwise around `self`.
    ///
    /// See also [`Self::custom_full_wedge`] and [`Self::wedge`]
    #[must_use]
    pub fn full_wedge(
        self,
        range: u32,
        direction: VertexDirection,
    ) -> impl ExactSizeIterator<Item = Self> {
        self.custom_full_wedge(range, direction, false)
    }

    /// Retrieves all successive [`Hex`] half ring edges around `self` in a
    /// given `range` and `direction`.
    ///
    /// See also [`Self::corner_wedge_to`] and [`Self::wedge`]
    pub fn corner_wedge(
        self,
        range: impl Iterator<Item = u32>,
        direction: EdgeDirection,
    ) -> impl Iterator<Item = Self> {
        let [left, right] = [direction << 2, direction >> 2];
        range.flat_map(move |r| {
            self.__ring_edge(r, r / 2, direction, left)
                .chain(self.__ring_edge(r, r / 2, direction, right).skip(1))
        })
    }

    /// Retrieves all successive [`Hex`] half ring edges from `self` to `rhs`
    ///
    /// See also [`Self::corner_wedge_to`] and [`Self::wedge_to`]
    pub fn corner_wedge_to(self, rhs: Self) -> impl Iterator<Item = Self> {
        let range = self.unsigned_distance_to(rhs);
        self.corner_wedge(0..=range, self.way_to(rhs).unwrap())
    }

    #[expect(clippy::cast_possible_truncation)]
    #[must_use]
    /// Retrieves all successive [`Hex`] ring edges around `self` in a given
    /// `RANGE` and `direction` as an array of edges.
    /// The returned edges coordinates are sorted counter clockwise around
    /// `self` unless `clockwise` is set to `true`.
    ///
    /// See also [`Self::cached_ring_edges`]
    /// If you only need the coordinates see [`Self::ring_edges`] or
    /// [`Self::wedge`].
    ///
    /// # Usage
    ///
    /// This function's objective is to pre-compute edges around a coordinate,
    /// the returned array can be used as a cache to avoid extra
    /// computation.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use hexx::*;
    ///
    /// // We cache 10 rings around the origin
    /// let cache = Hex::ORIGIN.cached_custom_ring_edges::<10>(VertexDirection::FLAT_RIGHT, true);
    /// // We have our target center
    /// let target = Hex::new(11, 24);
    /// // We retrieve the ring of range 5 and offset it to match the target
    /// let five_ring = cache[5].iter().map(|h| *h + target);
    /// ```
    ///
    /// See this [article](https://www.redblobgames.com/grids/hexagons/directions.html) for more
    /// information
    pub fn cached_custom_ring_edges<const RANGE: usize>(
        self,
        direction: VertexDirection,
        clockwise: bool,
    ) -> [Vec<Self>; RANGE] {
        std::array::from_fn(|r| {
            self.custom_ring_edge(r as u32, direction, clockwise)
                .collect()
        })
    }

    #[expect(clippy::cast_possible_truncation)]
    #[must_use]
    /// Retrieves all successive [`Hex`] ring edges around `self` in a given
    /// `RANGE` and `direction` as an array of edges.
    /// The returned edges coordinates are sorted counter clockwise around
    /// `self`.
    ///
    /// See also [`Self::cached_custom_ring_edges`]
    /// If you only need the coordinates see [`Self::ring_edges`] or
    /// [`Self::wedge`].
    ///
    /// # Usage
    ///
    /// This function's objective is to pre-compute edges around a coordinate,
    /// the returned array can be used as a cache to avoid extra
    /// computation.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use hexx::*;
    ///
    /// // We cache 10 rings around the origin
    /// let cache = Hex::ORIGIN.cached_ring_edges::<10>(VertexDirection::FLAT_RIGHT);
    /// // We have our target center
    /// let target = Hex::new(11, 24);
    /// // We retrieve the ring of range 5 and offset it to match the target
    /// let five_ring = cache[5].iter().map(|h| *h + target);
    /// ```
    ///
    /// See this [article](https://www.redblobgames.com/grids/hexagons/directions.html) for more
    /// information
    pub fn cached_ring_edges<const RANGE: usize>(
        self,
        direction: VertexDirection,
    ) -> [Vec<Self>; RANGE] {
        std::array::from_fn(|r| self.ring_edge(r as u32, direction).collect())
    }

    #[expect(clippy::cast_possible_truncation)]
    #[must_use]
    /// Retrieves all successive [`Hex`] rings around `self` in a given `RANGE`
    /// as an array of rings.
    /// The returned rings start from [`EdgeDirection::default`] and loop
    /// around `self` counter clockwise.
    ///
    /// See also [`Self::cached_custom_rings`]
    /// If you only need the coordinates see [`Self::range`] or
    /// [`Self::spiral_range`].
    ///
    /// # Usage
    ///
    /// This function's objective is to pre-compute rings around a coordinate,
    /// the returned array can be used as a cache to avoid extra
    /// computation.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use hexx::*;
    ///
    /// // We cache 10 rings around the origin
    /// let cache = Hex::ORIGIN.cached_rings::<10>();
    /// // We have our target center
    /// let target = Hex::new(11, 24);
    /// // We retrieve the ring of range 5 and offset it to match the target
    /// let five_ring = cache[5].iter().map(|h| *h + target);
    /// ```
    ///
    /// See this [article](https://www.redblobgames.com/grids/hexagons/#rings-spiral) for more
    /// information
    pub fn cached_rings<const RANGE: usize>(self) -> [Vec<Self>; RANGE] {
        std::array::from_fn(|r| self.ring(r as u32).collect())
    }

    #[expect(clippy::cast_possible_truncation)]
    #[must_use]
    /// Retrieves all successive [`Hex`] rings around `self` in a given `RANGE`
    /// as an array of rings.
    /// The returned rings start from `start_dir`] and loop around `self`
    /// counter clockwise unless `clockwise` is set to `true`.
    ///
    /// See also [`Self::cached_rings`]
    /// If you only need the coordinates see [`Self::range`] or
    /// [`Self::custom_spiral_range`].
    ///
    /// # Usage
    ///
    /// This function's objective is to pre-compute rings around a coordinate,
    /// the returned array can be used as a cache to avoid extra
    /// computation.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use hexx::*;
    ///
    /// // We cache 10 rings around the origin
    /// let cache = Hex::ORIGIN.cached_custom_rings::<10>(EdgeDirection::FLAT_TOP, true);
    /// // We have our target center
    /// let target = Hex::new(11, 24);
    /// // We retrieve the ring of range 5 and offset it to match the target
    /// let five_ring = cache[5].iter().map(|h| *h + target);
    /// ```
    ///
    /// See this [article](https://www.redblobgames.com/grids/hexagons/#rings-spiral) for more
    /// information
    pub fn cached_custom_rings<const RANGE: usize>(
        self,
        start_dir: EdgeDirection,
        clockwise: bool,
    ) -> [Vec<Self>; RANGE] {
        std::array::from_fn(|r| self.custom_ring(r as u32, start_dir, clockwise).collect())
    }

    /// Retrieves all [`Hex`] around `self` in a given `range` but ordered as
    /// successive rings, starting from `start_dir` and looping counter
    /// clockwise unless `clockwise` is set to `true`, forming a spiral
    ///
    /// If you only need the coordinates see [`Self::spiral_range`].
    ///
    /// See this [article](https://www.redblobgames.com/grids/hexagons/#rings-spiral) for more
    /// information
    pub fn custom_spiral_range(
        self,
        range: impl Iterator<Item = u32>,
        start_dir: EdgeDirection,
        clockwise: bool,
    ) -> impl Iterator<Item = Self> {
        self.custom_rings(range, start_dir, clockwise).flatten()
    }

    /// Retrieves all [`Hex`] around `self` in a given `range` but ordered as
    /// successive rings, starting from [`EdgeDirection::FLAT_TOP_RIGHT`] and
    /// looping counter clockwise, forming a spiral.
    ///
    /// See [`Self::custom_spiral_range`] for more options
    ///
    /// See this [article](https://www.redblobgames.com/grids/hexagons/#rings-spiral) for more
    /// information
    pub fn spiral_range(self, range: impl Iterator<Item = u32>) -> impl Iterator<Item = Self> {
        self.custom_spiral_range(range, EdgeDirection::default(), false)
    }

    #[inline]
    #[must_use]
    /// Counts how many coordinates there are in a ring at the given `range`
    pub const fn ring_count(range: u32) -> usize {
        if range == 0 { 1 } else { 6 * range as usize }
    }
}