Balanced Directions
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.
Features
- Enum Representation (
Balance):
Represents positions in a 3x3 grid with easy access to their 2D vector representation. - Grid Navigation:
Convenient methods for grid-based movement operations (up,down,left,right) as well as rotations, flips, and vector transformations. - Path Manipulation:
APathstructure for modeling sequences of movements, offering utilities to normalize, reverse, and transform paths. - Ternary Integration (Optional, active by default):
Balanced-ternary-based coordinates when integrating with thebalanced-ternarycrate.
Adds double balanced-ternary logic operations. #![no_std]Compatibility:
A lightweight design for use in embedded or low-level systems.
Examples
Position Representation and Movement
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 Balance;
Path Representation and Manipulation
The Path structure enables you to work with sequences of grid movements.
use ;
Integration with Balanced Ternary
With the feature "ternary", you can convert grid positions to and from balanced ternary representations.
use Balance;
use Digit;
API Overview
Balance
The Balance enumrepresents nine positionsof a 3x3 grid |
As a 2D ternary digitits cases can be viewedas False/Unknown/Trueon 2 dimensions |
|---|---|
![]() |
![]() |
Balance enum cases:
TopLeft,Top,TopRightLeft,Center,RightBottomLeft,Bottom,BottomRight
Operations:
A lot of operation can be performed with one (unary operations) or two (binary operations) Balance(s).
Below, the operation table of all Balance operations.
Unary directions
Moves with bounds (up, right, down, left) or wraps around (up_wrap, right_wrap, down_wrap, left_wrap).

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

Double balanced-ternary logic
Shorthands for Digit operations :
possibly,necessary,contingently,absolute_positive,positive,not_negative,not_positive,negative,absolute_negative,ht_not,post,pre.

Binary operations
Combines two Balances into one (add, mul, sub, and with the ternary feature : bitand, bitor, bitxor, k3_imply, ht_imply).

Path
A collection of Balance movements stored as steps in a sequence. Paths can be created, traversed, and normalized for
efficient representation of cumulative movement.
Key Methods:
- Construction:
new(),from_vector() - Traversal:
iter(),iter_mut() - Transformation:
normalized(),reversed(),each(),each_zip()
Cases
- Grid-based Movement in Games Simulate and manipulate movements within 3x3 environments for character navigation, AI paths, or puzzle logic.
- Embedded Robotics Applications Represent grid movements for sensor or actuator navigation without relying on the standard library.
- Spatial Modeling or Math-based Algorithms Perform 2D spatial computations with a high-level abstraction for vector-based movements.
Documentation
The complete API documentation is available on docs.rs. It contains details on each type, method, and their use cases.
Related Libraries
balanced-ternary: Provides balanced ternary number manipulations used for optional integration.
License
Copyright (c) 2025 Sébastien GELDREICH
Balanced Direction is licensed under the MIT License.

