3 unstable releases
Uses new Rust 2024
| 0.2.1 | Sep 18, 2025 |
|---|---|
| 0.2.0 | Jan 9, 2022 |
| 0.1.0 | Apr 12, 2021 |
#863 in Math
120 downloads per month
Used in collide-capsule
14KB
240 lines
simple-vectors
A simple, dimension-generic vector mathematics library for Rust.
Features
- Dimension and type generic: Works with vectors of any compile-time dimension and scalar type
- Trait integration: Implements
VectorSpace,DotProduct, andInnerSpacefrom thevector-spaceecosystem - Optional parsing: Parsing support via the
parsablefeature
Installation
Add this to your Cargo.toml:
[dependencies]
simple-vectors = "0.2"
Quick Start
use simple_vectors::Vector;
use vector_space::InnerSpace;
// Create vectors
let v = Vector::new([1.0, 2.0, 3.0]);
let w = Vector::new([4.0, 5.0, 6.0]);
// Basic arithmetic
let sum = v + w;
let scaled = v * 2.0;
// Vector operations (require `InnerSpace` trait)
let magnitude = v.magnitude();
let normalized = v.normalize();
Examples
2D Physics Simulation
use simple_vectors::Vector;
use vector_space::InnerSpace;
struct Particle {
position: Vector<f64, 2>,
velocity: Vector<f64, 2>,
acceleration: Vector<f64, 2>,
}
impl Particle {
fn update(&mut self, dt: f64) {
self.velocity += self.acceleration * dt;
self.position += self.velocity * dt;
self.acceleration = Vector::zero();
}
}
Generic Dimension Mathematics
use simple_vectors::Vector;
use vector_space::InnerSpace;
fn angle_between<T: num_traits::real::Real, const N: usize>(
v: Vector<T, N>,
w: Vector<T, N>,
) -> T {
(v.dot(w) / (v.magnitude() * w.magnitude())).acos()
}
let v = Vector::new([1.0, 0.0]);
let w = Vector::new([0.0, 1.0]);
let angle = angle_between(v, w);
Integration with vector-space Ecosystem
This crate implements standard vector space traits, making it compatible with other libraries:
use simple_vectors::Vector;
use vector_space::{distance, InnerSpace};
let point_a = Vector::new([1.0, 2.0]);
let point_b = Vector::new([4.0, 6.0]);
// Calculate distance between points
let dist = distance(point_a, point_b); // 5.0
// Or manually
let diff = point_b - point_a;
let manual_dist = diff.magnitude(); // Also 5.0
Dependencies
~245KB