3 releases
0.1.2 | Dec 22, 2021 |
---|---|
0.1.1 | Dec 20, 2021 |
0.1.0 | Dec 20, 2021 |
#1361 in Math
23KB
511 lines
Description
ndmath is a Rust library for doing vector arithmetic on built-in types.
You can find usage information in the documentation;
lib.rs
:
This crate provides traits for working with builtin Rust types as geometric primitives.
Usage
Vectors
VecN
provides basic vector math operations. FloatingVecN
adds some extra methods that only apply to real-valued vectors.
These traits are implemented for all applicable array types.
Example
use ndmath::*;
let a = [2, 5];
let b = [3, -7];
let c = a.add(b);
let d = a.neg();
assert_eq!(c, [5, -2]);
assert_eq!(d, [-2, -5]);
let a = [1, 2, 3, 4, 5, 6, 7];
let b = [9, 8, 7, 6, 5, 4, 3];
let c = a.add(b);
let d = a.sub(b);
let e = a.mul(2);
assert_eq!(c, [10; 7]);
assert_eq!(d, [-8, -6, -4, -2, 0, 2, 4]);
assert_eq!(e, [2, 4, 6, 8, 10, 12, 14]);
let a = [3.0, 4.0];
let b = [3.0, 6.0];
assert_eq!(a.mag(), 5.0);
assert_eq!(a.dist(b), 2.0);
Axis-aligned bounding boxes
Aabb
provides operations for axis-aligned bounding boxes. They consist of an origin and a size.
This trait is implemented for all even-sized scalar arrays up to size 16 and all size 2 arrays of scalar arrays.
Example
use ndmath::*;
let aabb = [1, 0, 4, 5];
assert!(aabb.contains([2, 2]));
assert!(aabb.contains([1, 0]));
assert!(aabb.contains([5, 5]));
assert!(!aabb.contains([5, 6]));
Named dimension traits
There are traits to provide accessors for named dimensional values.
There are 4 traits for vector dimensions:
There are 3 traits for axis-aligned bounding box dimensions:
Example
use ndmath::*;
let a = [1, 2];
let b = [3, 4, 5];
let c = [6, 7, 8, 9];
assert_eq!(a.x(), 1);
assert_eq!(a.y(), 2);
assert_eq!(b.z(), 5);
assert_eq!(c.w(), 9);
let aabb = [[0, 1, 2], [3, 4, 5]];
assert_eq!(aabb.left(), 0);
assert_eq!(aabb.top(), 1);
assert_eq!(aabb.back(), 2);
assert_eq!(aabb.right(), 3);
assert_eq!(aabb.bottom(), 5);
assert_eq!(aabb.front(), 7);
assert_eq!(aabb.width(), 3);
assert_eq!(aabb.height(), 4);
assert_eq!(aabb.depth(), 5);