hexing 0.2.1

A basic Rust library to manipulate hexagonal grids.
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
<a id="readme-top"></a>
<div align="center">

[![Contributors][contributors-shield]][contributors-url]
[![Forks][forks-shield]][forks-url]
[![Stargazers][stars-shield]][stars-url]
[![Issues][issues-shield]][issues-url]
[![License][license-shield]][license-url]

</div>

<!-- PROJECT LOGO -->

<br />
<div align="center">
  <img src="https://raw.githubusercontent.com/CoCoSol007/hexing/main/logo.png" alt="Logo" width="300"></p>
  <h3 align="center">Hexing</h3>
  <p align="center">
    <br />
    <i>"Hexagons are the bestagons"</i>
    <br />
    A basic Rust library to manipulate hexagonal grids.
  </p>
</div>

---

`hexing` is a Rust library designed for manipulation and calculations on hexagonal grids. It provides tools for working with hexagonal positions and directions, as well as iterators for exploring hexagonal rings and spirals.

## Features


- **Hexagonal Coordinate Manipulation**: Represent and manipulate positions in a hexagonal grid using axial coordinates.
- **Distance Calculations**: Compute the distance between two hexagonal positions.
- **Pixel Coordinate Conversion**: Convert hexagonal positions to pixel coordinates for graphical use.
- **Reflection and Rotation**: Apply reflection and rotation to hexagonal positions.
- **Ring and Spiral Iterators**: Obtain positions in a ring or spiral around a central position.
- **Line Iterators**: Obtain positions along a line between two hexagonal positions.
- **Number Trait**: Allow generic calculations with various numeric types.
- **Serialization and Deserialization**: Serialize and deserialize hexagonal positions and directions with the `serde` feature.

### Number Trait


The library uses the `Number` trait to allow generic calculations with various numeric types. This trait is implemented for several types, including integers and floating-point numbers.

### Main Types


#### `HexPosition<T>`


Represents a position in a hexagonal grid with coordinates `T`. Coordinates are in axial format `(x, y)`.

- **Creation**:
Creates a new [HexPosition] with the given `q` and `r` coordinates in an axial format. Coordinates are explained in this [documentation]https://www.redblobgames.com/grids/hexagons/#coordinates.

  ```rust
  let pos = HexPosition::new(1, 2);
  let pos2 = HexPosition(3, 4);

  // Constant: The origin of the hexagonal grid.
  let origin = HexPosition::ORIGIN;
  ```

- **Conversion to Pixel Coordinates**:
Converts the current [HexPosition] into a pixel coordinate. Basically, it converts a position in a hexagonal grid to a position in a orthogonal grid.

  ```rust
  let pixel_coords = pos.to_pixel_coordinates();
  let pixel_coords2 = HexPosition::from_pixel_coordinates(pixel_coords);
  ```

- **Distance Calculation**:
Calculates the distance between two hexagonal positions, using the [Manhattan distance]https://en.wikipedia.org/wiki/Taxicab_geometry.

  ```rust
  let distance = pos.distance(HexPosition::new(3, 4));
  ```

- **Rotation**:
Will apply a rotation of 2 x 60 degrees around the origin.

  ```rust
  let rotated_pos = pos.rotation(2);
  ```

- **Reflection**:
Will apply a central symmetric reflection around the origin.

  ```rust
  let reflected_pos = pos.reflect();
  ```

- **Ring Iterators**:
A iterator that returns positions in a ring around a central position. The iterator will return positions in a ring with the given radius.

  ```rust
  for ring_pos in pos.ring(2) {
      println!("{:?}", ring_pos);
  }
  ```

- **Spiral Iterators**:
A iterator that returns positions in a spiral around a central position. The iterator will return positions in a spiral with the given radius.

  ```rust
  for spiral_pos in pos.spiral(2) {
      println!("{:?}", spiral_pos);
  }
  ```

- **Line Iterators**
A iterator that returns positions along a line between two hexagonal positions.

  ```rust
  let a = HexPosition(0, 0);
  let b = HexPosition(-2, -1);
  for line_pos in a.line(b) {
      println!("{:?}", line_pos);
  }
  ```

#### `HexDirection`


Enum representing the six possible directions in a hexagonal grid.

- **Available Directions**:
  - `Right` (1, 0)
  - `UpRight` (1, -1)
  - `UpLeft` (0, -1)
  - `Left` (-1, 0)
  - `DownLeft` (-1, 1)
  - `DownRight` (0, 1)

- **Convert to Vector**:
You can convert a [HexDirection] to a [HexPosition] by using the `to_vector` method.

  ```rust
  let direction = HexDirection::Right;
  let vector = direction.to_vector();
  ```

## Usage Examples


Here are some examples to illustrate the features of `hexing`.

### Creating Hexagonal Positions


```rust
use hexing::HexPosition;

let pos = HexPosition::new(1, 2);
let pos2 = HexPosition(3, 4);
let origin = HexPosition::ORIGIN;

println!("Position 1: {:?}", pos);
println!("Position 2: {:?}", pos2);
println!("Origin: {:?}", origin);
```

### Conversion to Pixel Coordinates


```rust
use hexing::HexPosition;

let position = HexPosition::new(1, 0);
let pixel_coords = position.to_pixel_coordinates();
println!("Pixel coordinates: {:?}", pixel_coords);

let new_position: HexPosition<i32> = HexPosition::from_pixel_coordinates(pixel_coords);
println!("New position: {:?}", new_position);

assert!(position == new_position);
```

### Calculating Distance Between Positions


```rust
use hexing::HexPosition;

let pos1 = HexPosition::new(0, 0);
let pos2 = HexPosition::new(-2, -1);
let dist = pos1.distance(pos2);
println!("Distance: {:?}", dist);
```

### Iterating Over Rings and Spirals


```rust
use hexing::{HexPosition, HexRing, HexSpiral};

let center = HexPosition::new(0, 0);

// Ring of radius 1
let ring = center.ring(1);
for pos in ring {
    println!("Ring position: {:?}", pos);
}

// Spiral of radius 2
let spiral = center.spiral(2);
for pos in spiral {
    println!("Spiral position: {:?}", pos);
}
```

### Rotation of Hexagonal Position


```rust
use hexing::HexPosition;
let rotation = 120;
let pos = HexPosition::new(1, 2);
let rotated_pos = pos.rotation(rotation/60); // Rotates 120 degrees
println!("Rotated Position: {:?}", rotated_pos);
```

### Reflection of Hexagonal Position


```rust
use hexing::HexPosition;

let pos = HexPosition::new(1, 2);
let reflected_pos = pos.reflect();
println!("Reflected Position: {:?}", reflected_pos);
```

### Line Iterator


```rust
use hexing::HexPosition;

let start = HexPosition::new(0, 0);
let end = HexPosition::new(3, -3);
for pos in start.line(end) {
    println!("Line Position: {:?}", pos);
}
```

### Using HexDirection


```rust
use hexing::HexDirection;

let direction = HexDirection::UpRight;
let vector = direction.to_vector();
println!("Vector for Right Direction: {:?}", vector);

let new_position = HexPosition::new(0, 0) + vector * 3;
println!("New Position after moving Right: {:?}", new_position);
```

## Feature `serde`


The `hexing` library allows for the serialization and deserialization of hexagonal data structures through the `serde` feature. This feature is enabled by the `serde` feature of the library.

### Enabling the `serde` Feature


Simply run the command `cargo add hexing --features=serde` in your project or manually add it to your `Cargo.toml`.

```toml
[dependencies]
hexing = { version = "0.2.1", features = ["serde"] }
```

### Serialization and Deserialization with `serde`


When the `serde` feature is enabled, the following data structures automatically implement the `Serialize` and `Deserialize` traits:

- **HexPosition**
- **HexDirection**
- **HexRing**
- **HexSpiral**
- **HexLine**

**Note**: To use all the examples below, you need to add the `serde_json` library to your `Cargo.toml`.

#### HexPosition


`HexPosition<T>` represents a position in a hexagonal grid. With the `serde` feature, this structure can be serialized and deserialized, making it easier to save and retrieve hexagonal positions in formats like JSON, BSON, etc.

**Example: Serialization and Deserialization**

```rust
use hexing::HexPosition;

fn main() {
    let position = HexPosition::new(1, -2);

    // Serialize to JSON
    let serialized = serde_json::to_string(&position).unwrap();
    println!("Serialized: {}", serialized);

    // Deserialize from JSON
    let deserialized: HexPosition<i32> = serde_json::from_str(&serialized).unwrap();
    println!("Deserialized: {:?}", deserialized);
}
```

#### HexDirection


`HexDirection` is an enumeration representing all possible directions in a hexagonal grid. With the `serde` feature, this enumeration can be serialized and deserialized.

**Example: Serialization and Deserialization**

```rust
use hexing::HexDirection;

fn main() {
    let direction = HexDirection::Right;

    // Serialize to JSON
    let serialized = serde_json::to_string(&direction).unwrap();
    println!("Serialized: {}", serialized);

    // Deserialize from JSON
    let deserialized: HexDirection = serde_json::from_str(&serialized).unwrap();
    println!("Deserialized: {:?}", deserialized);
}
```

#### HexRing


`HexRing<T>` is a structure that allows iteration over positions in a hexagonal ring. This structure can also be serialized and deserialized with the `serde` feature.

**Example: Serialization and Deserialization**

```rust
use hexing::{HexPosition, HexRing};

fn main() {
    let ring = HexPosition::new(0, 0).ring(2);

    // Serialize to JSON
    let serialized = serde_json::to_string(&ring).unwrap();
    println!("Serialized: {}", serialized);

    // Deserialize from JSON
    let deserialized: HexRing<i32> = serde_json::from_str(&serialized).unwrap();
    println!("Deserialized: {:?}", deserialized);
}
```

#### HexSpiral


`HexSpiral<T>` allows iteration over positions in a hexagonal spiral. Like the other structures, `HexSpiral<T>` is serializable and deserializable with `serde`.

**Example: Serialization and Deserialization**

```rust
use hexing::{HexPosition, HexSpiral};

fn main() {
    let spiral = HexPosition::new(0, 0).spiral(2);

    // Serialize to JSON
    let serialized = serde_json::to_string(&spiral).unwrap();
    println!("Serialized: {}", serialized);

    // Deserialize from JSON
    let deserialized: HexSpiral<i32> = serde_json::from_str(&serialized).unwrap();
    println!("Deserialized: {:?}", deserialized);
}
```

#### HexLine


`HexLine<T>` allows iteration over positions on a hexagonal line between two points. This structure is also serializable and deserializable.

**Example: Serialization and Deserialization**

```rust
use hexing::{HexPosition, HexLine};

fn main() {
    let line = HexPosition::new(0, 0).line_to(HexPosition::new(2, -1));

    // Serialize to JSON
    let serialized = serde_json::to_string(&line).unwrap();
    println!("Serialized: {}", serialized);

    // Deserialize from JSON
    let deserialized: HexLine<i32> = serde_json::from_str(&serialized).unwrap();
    println!("Deserialized: {:?}", deserialized);
}
```

### Full Documentation


For more detailed documentation and additional explanations about hexagonal grids, please refer to the [Red Blob Games hexagonal grid documentation](https://www.redblobgames.com/grids/hexagons/).

### Installation


Add `hexing` to your `Cargo.toml`:

```toml
[dependencies]
hexing = "0.2.1"
```

<!-- You have to change every link to the great repo -->


[contributors-shield]: https://img.shields.io/github/contributors/cocosol007/hexing.svg?style=for-the-badge
[contributors-url]: https://github.com/cocosol007/hexing/graphs/contributors
[forks-shield]: https://img.shields.io/github/forks/cocosol007/hexing.svg?style=for-the-badge
[forks-url]: https://github.com/cocosol007/hexing/network/members
[stars-shield]: https://img.shields.io/github/stars/cocosol007/hexing.svg?style=for-the-badge
[stars-url]: https://github.com/cocosol007/hexing/stargazers
[issues-shield]: https://img.shields.io/github/issues/cocosol007/hexing.svg?style=for-the-badge
[issues-url]: https://github.com/cocosol007/hexing/issues
[license-shield]: https://img.shields.io/github/license/cocosol007/hexing.svg?style=for-the-badge
[license-url]: https://github.com/cocosol007/hexing/blob/main/LICENSE