Crate ndmath[−][src]
Expand description
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);
Traits
Trait for axis-aligned bounding boxes
Trait for floating-point scalar numbers
Trait for real-valued vector math operations
Trait for math with scalar numbers
Trait for basic vector math operations
Trait for vectors with a W dimension
Trait for axis-aligned bounding boxes with a width
Trait for vectors with an X dimension
Trait for axis-aligned bounding boxes with a height
Trait for vectors with a Y dimension
Trait for axis-aligned bounding boxes with a depth
Trait for vectors with a Z dimension