Features
- Cardinal Directions: Complete representation of the four primary directions (North, East, South, West)
- 2D Transformations: Full implementation of the dihedral group D4 (rotations and reflections)
- Type Conversions: Convert between string representations, enum values, and numeric types
- Mathematical Operations: Compose transformations and apply them to directions
- Array Transformations (optional): Apply transformations to
ndarray2D arrays - No Standard Library Requirement: Core-only implementation for embedded systems compatibility
- Comprehensive Testing: 100% test coverage
Installation
Add this to your Cargo.toml:
[]
= "0.2.0"
To enable array transformation support (adds ndarray dependency):
[]
= { = "0.2.0", = ["array"] }
Usage
Direction
The Direction enum represents the four cardinal directions: North, East, South, and West.
use Direction;
use FromStr;
// Create directions
let north = North;
let east = East;
// Check direction properties
assert!;
assert!;
// Get opposite direction
assert_eq!;
// Convert to/from strings
assert_eq!;
assert_eq!;
assert_eq!;
// Convert to/from numeric representation
let dir_value: u8 = north.into; // 0
assert_eq!;
Transform
The Transform enum represents the eight possible transformations in the dihedral group D4:
use Transform;
use FromStr;
// Create transforms
let identity = Identity;
let rotate90 = Rotate90;
let flip_h = FlipHorizontal;
// Check transform properties
assert!;
assert!;
assert!;
// Compose transforms
let rotate180 = rotate90 * rotate90;
assert_eq!;
// Applying inverse transformations returns to identity
assert_eq!;
// Convert to/from strings
assert_eq!;
assert_eq!;
Applying Transformations to Directions
You can apply transformations to directions using the multiplication operator:
use ;
// Apply rotations
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// Apply reflections
assert_eq!;
assert_eq!;
Array Transformation (requires array feature)
Apply geometric transformations to 2D arrays using the ndarray crate:
use Transform;
use ;
// Enable the "array" feature in Cargo.toml:
// nav = { version = "0.1.5", features = ["array"] }
// Create a 2D array
let arr = arr2;
// Apply transformations
let rotated = Rotate90 * &arr;
let flipped = FlipHorizontal * &arr;
assert_eq!;
assert_eq!;
Working with All Values
The library provides constants for all directions and transforms:
use ;
// Iterate through all directions
for dir in ALL_DIRECTIONS
// Iterate through all transformations
for transform in ALL_TRANSFORMS
Transform Symbols
The Transform enum uses single character symbols for string representation:
| Transform | Symbol | Description | Effect on (x,y) |
|---|---|---|---|
| Identity | I | No transformation | (x, y) |
| Rotate90 | R | 90° clockwise rotation | (y, -x) |
| Rotate180 | U | 180° rotation | (-x, -y) |
| Rotate270 | L | 270° clockwise (90° counter-clockwise) | (-y, x) |
| FlipHorizontal | | | Reflection about vertical axis | (-x, y) |
| FlipDiagonal | / | Reflection about main diagonal | (y, x) |
| FlipVertical | - | Reflection about horizontal axis | (x, -y) |
| FlipAntiDiagonal | \ | Reflection about anti-diagonal | (-y, -x) |
Features
This crate provides the following features:
- array: Enables array transformation functionality using the
ndarraycrate
Applications
This library is useful for:
- Game development (movement, rotation, grid-based operations)
- Cellular automata and grid-based simulations
- Procedural generation
- Puzzle solving algorithms
- Geometry processing
License
This project is licensed under the MIT License - see the LICENSE file for details.