math2
math2 is a collection of lightweight geometry and math utilities used across the Grida canvas engine. It includes helpers for working with vectors, rectangles, affine transforms, rasterization and many other 2D operations.
This crate is a direct port of the original TypeScript grida-cmath library. The APIs mostly follow the functional style of the source project.
Features
- Vector and rectangle primitives
- Affine transform helpers
- Bézier conversion and bounding box computation
- Rasterization algorithms (bresenham, circle/ellipse, flood fill, Gaussian blur, etc.)
- Snapping, layout and packing utilities
- Color conversions (HEX, RGBA)
Installation
Add the crate to your Cargo.toml:
[]
= "0.0.1"
Example
use ;
use AffineTransform;
let rect = Rectangle ;
let transform = translate;
let moved = rect_transform;
assert_eq!;
assert_eq!;
Suggestions for future improvements
The current code mirrors the functional API of the original TypeScript version. Some potential refinements for a more idiomatic Rust API include:
- Exposing common operations as methods on types like
Vector2andRectangle. - Implementing standard traits (
Add,Sub, etc.) for math types. - Splitting optional algorithms (e.g. rasterization) behind crate features for
no_stdbuilds.
License
Licensed under the MIT license.