Module pix_engine::vector
source · Expand description
A Euclidean Vector
in N-dimensional space.
Each Vector represents a N-dimensional Euclidean (or geometric) vector with a magnitude
and a direction. The Vector struct
, however, contains N values for each dimensional
coordinate. The magnitude and direction are retrieved with the Vector::mag
and
Vector::heading
methods.
Some example uses of a Vector include modeling a position, velocity, or acceleration of an object or particle in 2D or 3D space.
Examples
You can create a Vector using Vector::new
:
let v = Vector::new([10.0, 20.0, 15.0]);
…or by using the vector! macro:
let v: Vector<f64, 3> = vector!(); // vector at the origin (0, 0, 0) with no direction or magnitude
assert_eq!(v.coords(), [0.0, 0.0, 0.0]);
let v = vector!(5.0); // 1D vector on the x-axis with magnitude 5
assert_eq!(v.coords(), [5.0]);
let v = vector!(5.0, 10.0); // 2D vector in the x/y-plane
assert_eq!(v.coords(), [5.0, 10.0]);
let v = vector!(-1.5, 3.0, 2.2); // 3D vector
assert_eq!(v.coords(), [-1.5, 3.0, 2.2]);
You can also create random Vector
s using Vector::random
which create unit vectors with
magnitudes in the range -1.0..=1.0
.
use pix_engine::prelude::*;
let v: Vector<f64, 1> = Vector::random();
// `v.coords()` will return something like:
// [-0.9993116191591512, 0.03709835324533284, 0.0]
assert!(v.x() >= -1.0 && v.x() <= 1.0);
let v: Vector<f64, 2> = Vector::random();
// `v.coords()` will return something like:
// [-0.9993116191591512, 0.03709835324533284, 0.0]
assert!(v.x() >= -1.0 && v.x() <= 1.0);
assert!(v.y() >= -1.0 && v.y() <= 1.0);
let v: Vector<f64, 3> = Vector::random();
// `v.coords()` will return something like:
// [-0.40038099206441835, 0.8985763512414204, 0.17959844705110184]
assert!(v.x() >= -1.0 && v.x() <= 1.0);
assert!(v.y() >= -1.0 && v.y() <= 1.0);
assert!(v.z() >= -1.0 && v.z() <= 1.0);
Structs
- A Euclidean
Vector
in N-dimensional space.