simple_3d_vector

Crates.iosimple_3d_vector
lib.rssimple_3d_vector
version0.1.0
sourcesrc
created_at2024-04-03 12:42:03.063452
updated_at2024-04-03 12:42:03.063452
descriptionSimple three-dimensional vectors in Rust
homepage
repositoryhttps://github.com/Theboiboi8/simple_3d_vector
max_upload_size
id1195018
size22,292
Kamela (Theboiboi8)

documentation

README

Simple 3D Vector

Simple grid-based 3-dimensional vectors in Rust.

Also provides advanced operations for working with simple_2d_vector

Getting Started

To get started with simple_3d_vector, add it to your project using cargo add simple_3d_vector.

You can then use it by using the provided Vector3D struct.

Examples

Creating a Vector3D with Vector3D::new() and comparing it to a Vector3D::null()

use vector3d::{Vector3D, Point3D};

fn main() {
    let vector = Vector3D::new(
        Point3D::zero(), // The origin of the vector
        Poine3D::zero()  // The target of the vector
    );
    
    // Null vectors are vectors with a length of zero
    // They are also called zero-length vectors as they only have an origin
    let null_vector = Vector3D::null(Point3D::new(0.0, 0.0, 0.0)); // A null vector
    
    assert_eq!(vector, null_vector); // The two vectors are the same
}

Performing addition and subtraction with vectors

use vector3d::{Vector3D, Point3D};

fn main() {
	let vector1 = Vector3D::new(
		Point3D::new(
			10.0,
			10.0,
			10.0
		),
		Point3D::new(
			10.0,
			5.0,
			5.0
		)
	);
	let vector2 = Vector3D::new(
		Point3D::new(
			10.0,
			10.0,
			10.0
		),
		Point3D::new(
			5.0,
			10.0,
			5.0
		)
	);

	let result_vector_addition = Vector3D::new(
		Point3D::new(
			10.0,
			10.0,
			10.0
		),
		Point3D::new(
			15.0,
			15.0,
			10.0
		)
	);
	let result_vector_subtraction = Vector3D::new(
		Point3D::new(
			10.0,
			10.0,
			10.0
		),
		Point3D::new(
			5.0,
			-5.0,
			0.0
		)
	);
    
    assert_eq!(vector1 + vector2, result_vector_addition);
    assert_eq!(vector1 - vector2, result_vector_subtraction);
}

Shifting a vector

use vector3d::{Vector3D, Point3D};

fn main() { 
    let vector = Vector3D::new(
	    Point3D::new(
		    10.0,
		    10.0,
		    10.0
	    ),
	    Point3D::new(
		    10.0,
		    5.0,
		    5.0
	    )
    );
    
    // `Vector3D.shift` automatically converts applicable types into f64
    let shift = (-2, 1.25, 2_u16); // This allows for a mismatch of types
    
    // Shifting a vector moves only its `origin`,
    // as it's `target` is relative to its `origin`
    let result_vector = Vector3D::new(
	    Point3D::new(
		    8.0,
		    11.25,
		    12.0
	    ),
	    Point3D::new(
		    10.0,
		    5.0,
		    5.0
	    )
    );
    
    assert_eq!(vector.shift(shift), result_vector);
}
Commit count: 11

cargo fmt