#vector3

vec3-rs

A simple 3D vector math library

7 releases

0.1.6 Jan 28, 2024
0.1.5 Aug 16, 2023

#388 in Math

Download history 31/week @ 2024-07-26 2/week @ 2024-08-02 15/week @ 2024-09-20 48/week @ 2024-09-27 62/week @ 2024-10-04 41/week @ 2024-10-11 69/week @ 2024-10-18 8/week @ 2024-10-25

191 downloads per month

Custom license

21KB
459 lines

vec3-rs

https://crates.io/crates/vec3-rs

This crate provides a simple and efficient implementation of 3D vectors in Rust, with various utility functions for vector manipulation and operations.

Features

  • Represents 3D vectors in Cartesian space.
  • Includes predefined constant vectors for X, Y, and Z axes.
  • Supports basic vector operations such as addition, subtraction, dot product, cross product, etc.
  • Supports all primitive number types.
  • Provides methods for vector normalization, linear interpolation, and angle calculations.
  • Allows fuzzy equality comparison within a specified epsilon.

Example

use vec3_rs::Vector3;

fn main() {
    let mut v1: Vector3<f64> = Vector3::new(1.0, 2.0, 3.0);
    let mut v2: Vector3<f64> = Vector3::new(3.0, 1.0, 2.0);

    // Basic operations
    let sum = v1 + v2;
    let difference = v1 - v2;
    let dot_product = v1.dot(&v2);
    let cross_product = v1.cross(&v2);

    // Other methods
    let lerp_result = v1.lerp(&v2, 0.5);
    let angle = v1.angle(&v2);
    let fuzzy_equal = v1.fuzzy_equal(&v2, 0.001);

    println!("Sum: {sum}");
    println!("Difference: {difference}");
    println!("Dot product: {dot_product}");
    println!("Cross product: {cross_product}");
    println!("Lerp 50%: {lerp_result}");
    println!("Angle: {angle}");
    print!("Are they close enough?: {fuzzy_equal}");

    v1.normalize();
    v2.normalize();

    println!("v1 normalized: {v1}");
    println!("v2 normalized: {v2}");
}

Dependencies

~0.9–1.5MB
~31K SLoC