math2

Crates.iomath2
lib.rsmath2
version0.0.2
created_at2025-06-09 11:11:39.874991+00
updated_at2025-06-10 09:29:13.930367+00
descriptionGeometry, layout and rasterization utilities powering the Grida canvas
homepagehttps://grida.co
repositoryhttps://github.com/gridaco/grida
max_upload_size
id1705790
size133,996
Universe (softmarshmallow)

documentation

README

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:

[dependencies]
math2 = "0.0.1"

Example

use math2::{Rectangle, vector2, rect_transform};
use math2::transform::AffineTransform;

let rect = Rectangle { x: 0.0, y: 0.0, width: 100.0, height: 50.0 };
let transform = AffineTransform::translate(10.0, 20.0);
let moved = rect_transform(rect, &transform);
assert_eq!(moved.x, 10.0);
assert_eq!(moved.y, 20.0);

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 Vector2 and Rectangle.
  • Implementing standard traits (Add, Sub, etc.) for math types.
  • Splitting optional algorithms (e.g. rasterization) behind crate features for no_std builds.

License

Licensed under the MIT license.

Commit count: 7377

cargo fmt