| Crates.io | balanced-direction |
| lib.rs | balanced-direction |
| version | 0.2.1 |
| created_at | 2025-02-28 03:32:39.404718+00 |
| updated_at | 2025-03-04 02:03:49.9429+00 |
| description | A library to manipulate directions with discrete logic. |
| homepage | |
| repository | https://github.com/Trehinos/balanced-direction |
| max_upload_size | |
| id | 1572424 |
| size | 309,053 |
Balanced Directions is a Rust crate for modeling directions and movements in a 3x3 grid using a balanced ternary-inspired logic. It can be viewed as a 2D ternary digit.
It provides tools to manipulate positions, paths, and directions on a grid-based structure, making it useful in scenarios requiring discrete grid movement, coordinated navigation, or 2D spatial logic.
Balance):up, down, left, right) as well as rotations, flips, and
vector transformations.Path structure for modeling sequences of movements, offering utilities to normalize, reverse, and transform paths.balanced-ternary crate.#![no_std] Compatibility:The core Balance enum represents positions in a 3x3 grid. For example, you can easily identify positions such as
TopLeft or Center. Use movement methods to perform grid operations.
use balanced_directions::Balance;
fn example_usage() {
let position = Balance::TopLeft;
assert_eq!(position.to_vector(), (-1, -1));
let moved = position.right();
assert_eq!(moved, Balance::Top);
let rotated = moved.rotate_left();
assert_eq!(rotated, Balance::Left);
assert_eq!(rotated.to_angle(), Balance::WEST);
}
The Path structure enables you to work with sequences of grid movements.
use balanced_directions::{Balance, Path};
fn path_example() {
let movements = vec![Balance::Top, Balance::Right, Balance::Bottom];
let path = Path::new(movements);
assert_eq!(path.len(), 3);
assert_eq!(path.to_vector(), (1, 0)); // Cumulative movement: right by 1
let movements = vec![Balance::Top, Balance::Top, Balance::Top, Balance::Bottom];
let path = Path::new(movements);
assert_eq!(path.to_vector(), (0, -2)); // Cumulative movement: top by 2
let normalized_path = path.normalized();
assert_eq!(normalized_path.to_vector(), (0, -2));
assert_eq!(normalized_path.len(), 2);
}
With the feature "ternary", you can convert grid positions to and from balanced ternary representations.
use balanced_directions::Balance;
use balanced_ternary::Digit;
fn ternary_example() {
let position = Balance::Top;
let ternary_pair = position.to_ternary_pair();
assert_eq!(ternary_pair, (Digit::Zero, Digit::Neg)); // Top is represented as (0, -1)
let recreated_position = Balance::from_ternary_pair(Digit::Zero, Digit::Neg);
assert_eq!(recreated_position, Balance::Top);
let possibly = recreated_position.possibly();
assert_eq!(possibly, Balance::TopRight);
}
BalanceThe Balance enumrepresents nine positions of a 3x3 grid |
As a 2D ternary digit its cases can be viewed as False/Unknown/Trueon 2 dimensions |
|---|---|
![]() |
![]() |
Balance enum cases:
TopLeft, Top, TopRightLeft, Center, RightBottomLeft, Bottom, BottomRightA lot of operation can be performed with one (unary operations) or two (binary operations) Balance(s).
Below, the operation table of all Balance operations.
Moves with bounds (up, right, down, left) or wraps around (up_wrap, right_wrap, down_wrap, left_wrap).

Performs some useful transformations on a Balance (flip_h, neg, flip_v, not (transpose), rotate_left,
rotate_right,center_h, center_v).

Shorthands for Digit operations :
possibly, necessary, contingently,absolute_positive, positive, not_negative,not_positive, negative, absolute_negative,ht_not, post, pre.
Combines two Balances into one (add, mul, sub, and with the ternary feature : bitand, bitor, bitxor, k3_imply, ht_imply).

PathA collection of Balance movements stored as steps in a sequence. Paths can be created, traversed, and normalized for
efficient representation of cumulative movement.
new(), from_vector()iter(), iter_mut()normalized(), reversed(), each(), each_zip()The complete API documentation is available on docs.rs. It contains details on each type, method, and their use cases.
balanced-ternary: Provides balanced ternary number manipulations used
for optional integration.Copyright (c) 2025 Sébastien GELDREICH
Balanced Direction is licensed under the MIT License.