pub struct Vector3<T: Vector3Coordinate> { /* private fields */ }Expand description
Represents a vector in 3D space.
Implementations§
Source§impl<T: Vector3Coordinate> Vector3<T>
impl<T: Vector3Coordinate> Vector3<T>
Source§impl<T: Vector3Coordinate + Float> Vector3<T>
impl<T: Vector3Coordinate + Float> Vector3<T>
Sourcepub fn fuzzy_equal(&self, target: &Self, epsilon: T) -> bool
pub fn fuzzy_equal(&self, target: &Self, epsilon: T) -> bool
Checks if this vector is approximately equal to another vector within a given epsilon.
§Panics
Panics if epsilon is negative.
§Examples
use vec3_rs::Vector3;
let v1 = Vector3::new(0.1, 0.2, 0.3);
let v2 = Vector3::new(0.101, 0.199, 0.299);
let epsilon = 0.01;
let is_approx_equal = v1.fuzzy_equal(&v2, epsilon);
assert!(is_approx_equal)Sourcepub fn lerp(&self, target: &Self, alpha: T) -> Self
pub fn lerp(&self, target: &Self, alpha: T) -> Self
Linearly interpolates between this vector and another vector by a given ratio.
§Examples
type Vector3 = vec3_rs::Vector3<f64>;
let origin = Vector3::zero();
let x_axis = Vector3::x_axis();
assert_eq!(origin.lerp(&x_axis, 0.5), Vector3::new(0.5, 0, 0))
Sourcepub fn magnitude(&self) -> T
pub fn magnitude(&self) -> T
Computes the magnitude (length) of the vector.
§Examples
type Vector3 = vec3_rs::Vector3<f64>;
let mut vector3 = Vector3::new(12354,7324,-7765).normalized();
assert_eq!(vector3.magnitude(), 1.0);Sourcepub fn angle(&self, target: &Self) -> T
pub fn angle(&self, target: &Self) -> T
Computes the angle in radians (0..2pi) between this vector and another vector.
Sourcepub fn angle_deg(&self, target: &Self) -> T
pub fn angle_deg(&self, target: &Self) -> T
Computes the angle in degrees (0.0..360.0) between this vector and another vector.
Sourcepub fn normalized(&self) -> Self
pub fn normalized(&self) -> Self
Copies the vector and scales it such that its magnitude becomes 1.
Sourcepub fn distance(&self, target: &Self) -> T
pub fn distance(&self, target: &Self) -> T
Computes the distance between this vector and another vector.
§Examples
type Vector3 = vec3_rs::Vector3<f64>;
let left = -Vector3::x_axis();
let right = Vector3::x_axis();
assert_eq!(left.distance(&right), 2.0);Sourcepub fn reflect(&self, normal: &Self) -> Self
pub fn reflect(&self, normal: &Self) -> Self
Reflects this vector off a surface defined by a normal.
Sourcepub fn clamp(&self, min: T, max: T) -> Self
pub fn clamp(&self, min: T, max: T) -> Self
Returns a new vector with each component clamped to a given range.
Sourcepub fn rotated(&self, axis: &Self, angle: T) -> Self
pub fn rotated(&self, axis: &Self, angle: T) -> Self
Rotates the vector around an axis by a given angle in radians.
Sourcepub fn from_spherical(radius: T, polar: T, azimuth: T) -> Self
pub fn from_spherical(radius: T, polar: T, azimuth: T) -> Self
Creates a new Vector3 from spherical coordinates.
§Arguments
radius- The distance from the origin.polar- The polar angle (the angle from the z-axis, in radians).azimuth- The azimuth angle (the angle from the x-axis in the xy-plane, in radians).
Source§impl<T: Vector3Coordinate> Vector3<T>
impl<T: Vector3Coordinate> Vector3<T>
Sourcepub fn new<U: Into<T>, V: Into<T>, W: Into<T>>(x: U, y: V, z: W) -> Self
pub fn new<U: Into<T>, V: Into<T>, W: Into<T>>(x: U, y: V, z: W) -> Self
Creates a new Vector3 with the specified coordinates.
§Examples
type Vector3 = vec3_rs::Vector3<f64>;
let vector3 = Vector3::new(1.0, 2.0, 3.0);
let also_ok = Vector3::new(1, 2, 3);
assert_eq!(vector3, also_ok);
// can also be created from arrays and tuples
// but no automatic number conversion
let from_array = Vector3::from([1.0, 2.0, 3.0]);
let from_tuple = Vector3::from((1.0, 2.0, 3.0));
assert_eq!(vector3, from_array);
assert_eq!(vector3, from_tuple);
// conversion is fallible with unknown sized data types
let slice: &[f64] = [1.0, 2.0, 3.0].as_ref();
let from_slice = Vector3::try_from(slice).unwrap();
let from_vec = Vector3::try_from(vec![1.0, 2.0, 3.0]).unwrap();
assert_eq!(vector3, from_slice);
assert_eq!(vector3, from_vec);
Sourcepub fn dot(&self, target: &Self) -> T
pub fn dot(&self, target: &Self) -> T
Computes the dot product between this vector and another vector. https://en.wikipedia.org/wiki/Dot_product
Sourcepub fn cross(&self, target: &Self) -> Self
pub fn cross(&self, target: &Self) -> Self
Computes the cross product between this vector and another vector. https://en.wikipedia.org/wiki/Cross_product
Sourcepub fn max(&self, target: &Self) -> Self
pub fn max(&self, target: &Self) -> Self
Computes the component-wise maximum of this vector and another vector.
Trait Implementations§
Source§impl<T: Vector3Coordinate> Add for Vector3<T>
impl<T: Vector3Coordinate> Add for Vector3<T>
Source§impl<T: Vector3Coordinate> AddAssign for Vector3<T>
impl<T: Vector3Coordinate> AddAssign for Vector3<T>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<'de, T> Deserialize<'de> for Vector3<T>where
T: Deserialize<'de> + Vector3Coordinate,
impl<'de, T> Deserialize<'de> for Vector3<T>where
T: Deserialize<'de> + Vector3Coordinate,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T: Vector3Coordinate> Display for Vector3<T>
impl<T: Vector3Coordinate> Display for Vector3<T>
Source§impl<T: Vector3Coordinate> Div<T> for Vector3<T>
impl<T: Vector3Coordinate> Div<T> for Vector3<T>
Source§impl<T: Vector3Coordinate> DivAssign<T> for Vector3<T>
impl<T: Vector3Coordinate> DivAssign<T> for Vector3<T>
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/= operation. Read moreSource§impl<T: Vector3Coordinate> Mul<T> for Vector3<T>
impl<T: Vector3Coordinate> Mul<T> for Vector3<T>
Source§impl<T: Vector3Coordinate> MulAssign<T> for Vector3<T>
impl<T: Vector3Coordinate> MulAssign<T> for Vector3<T>
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*= operation. Read moreSource§impl<T: Vector3Coordinate> Sub for Vector3<T>
impl<T: Vector3Coordinate> Sub for Vector3<T>
Source§impl<T: Vector3Coordinate> SubAssign for Vector3<T>
impl<T: Vector3Coordinate> SubAssign for Vector3<T>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read moreSource§impl<T: Vector3Coordinate> TryFrom<Vec<T>> for Vector3<T>
Available on crate feature std only.
impl<T: Vector3Coordinate> TryFrom<Vec<T>> for Vector3<T>
std only.Source§impl<T: Vector3Coordinate> TryFrom<VecDeque<T>> for Vector3<T>
Available on crate feature std only.
impl<T: Vector3Coordinate> TryFrom<VecDeque<T>> for Vector3<T>
std only.