| Crates.io | inner-space |
| lib.rs | inner-space |
| version | 0.1.0 |
| created_at | 2026-01-16 23:06:01.45361+00 |
| updated_at | 2026-01-16 23:06:01.45361+00 |
| description | Provides the dot product trait and auto implements the inner space trait, which contains a bunch of useful functions for working with vectors |
| homepage | https://gitlab.com/porky11/inner-space |
| repository | https://gitlab.com/porky11/inner-space |
| max_upload_size | |
| id | 2049457 |
| size | 13,192 |
A Rust crate providing essential traits for vector operations, building on vector-space.
DotProduct trait: Defines dot product with flexible output typeInnerSpace trait: Provides common vector operations like normalize, project, reflect...distance and distance2 for point operationsuse std::ops::{Add, Div, Mul, Neg, Sub};
use inner_space::{DotProduct, VectorSpace, distance};
use num_traits::Zero;
#[derive(Clone, Copy, PartialEq, Debug)]
struct Vector {
x: f32,
y: f32,
}
impl Vector {
fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
impl Add for Vector {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
impl Sub for Vector {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self {
x: self.x - other.x,
y: self.y - other.y,
}
}
}
impl Mul<f32> for Vector {
type Output = Self;
fn mul(self, other: f32) -> Self {
Self {
x: self.x * other,
y: self.y * other,
}
}
}
impl Div<f32> for Vector {
type Output = Self;
fn div(self, other: f32) -> Self {
Self {
x: self.x / other,
y: self.y / other,
}
}
}
impl Neg for Vector {
type Output = Self;
fn neg(self) -> Self {
Self {
x: -self.x,
y: -self.y,
}
}
}
impl Zero for Vector {
fn zero() -> Self {
Self { x: 0.0, y: 0.0 }
}
fn is_zero(&self) -> bool {
self.x == 0.0 && self.y == 0.0
}
}
impl VectorSpace for Vector {
type Scalar = f32;
}
impl DotProduct for Vector {
type Output = f32;
fn dot(&self, other: &Self) -> f32 {
self.x * other.x + self.y * other.y
}
}
// distance
let a = Vector::new(-1.0, 2.0);
let b = Vector::new(2.0, 6.0);
assert_eq!(distance(a, b), 5.0);
// projection, rejection, reflection
let a = Vector::new(-1.0, 3.0);
let b = Vector::new(0.0, 6.0);
assert_eq!(a.project(b), Vector::new(0.0, 3.0));
assert_eq!(a.reject(b), Vector::new(-1.0, 0.0));
assert_eq!(a.reflect(b), Vector::new(1.0, 3.0));
DotProduct Traitpub trait DotProduct<T = Self>: VectorSpace {
type Output;
fn dot(&self, other: &T) -> Self::Output;
}
InnerSpace TraitAutomatically implemented for types that implement DotProduct<Output = Scalar>. Provides:
magnitude() and magnitude2()normalize()project() and reject()reflect()angle()with_magnitude() and with_direction()distance(a, b): Distance between two pointsdistance2(a, b): Squared distance between two pointslet v = Vec2(3.0, 4.0);
let unit = v.normalize();
assert_eq!(unit, Vec2(0.6, 0.8));
let reflected = v.reflect(Vec2(1.0, 0.0)); // Reflect over x-axis
assert_eq!(reflected, Vec2(3.0, -4.0));
VectorSpace from vector-space crate (also reexported here)DotProduct with Output = VectorSpace::ScalarInnerSpace methods automaticallyWorks with any type that implements VectorSpace and the required operations. Perfect for: