Struct gamemath::Vec3

source ·
pub struct Vec3<T> {
    pub x: T,
    pub y: T,
    pub z: T,
}
Expand description

A three-component Euclidean vector useful for linear algebra computation in game development and 3D rendering.

Fields§

§x: T

The X/first component of the vector.

§y: T

The Y/second component of the vector.

§z: T

The Z/third component of the vector.

Implementations§

source§

impl<T> Vec3<T>where T: Copy + Debug + PartialEq + Default + Sub<Output = T> + Mul<Output = T> + Add<Output = T> + Neg<Output = T> + PartialOrd,

source

pub fn new(x: T, y: T, z: T) -> Vec3<T>

Constructs a new Vec3<T> from three initial values.

Examples
use gamemath::Vec3;

let v = Vec3::new(1.0, 5.0, 23.0);

assert_eq!(v.x, 1.0);
assert_eq!(v.y, 5.0);
assert_eq!(v.z, 23.0);
source

pub fn dot(&self, right: Vec3<T>) -> T

Calculates the dot/scalar product of two Vec3<T>s.

The calling object is considered the left value and the argument object is considered the right value.

Examples
use gamemath::Vec3;

let v1 = Vec3::new(1.0, 2.0, 3.0);
let v2 = Vec3::new(4.0, 5.0, 6.0);

assert_eq!(v1.dot(v2), 32.0);
assert_eq!(v2.dot(v1), 32.0);
source

pub fn cross(&self, right: Vec3<T>) -> Vec3<T>

Calculates the cross/vector product of two Vec3<T>s.

The calling object is considered the left value and the argument object is considered the right value.

Examples
use gamemath::Vec3;

let v1 = Vec3::new(1.0, 2.0, 3.0);
let v2 = Vec3::new(4.0, 5.0, 6.0);

assert_eq!(v1.cross(v2), Vec3::new(-3.0, 6.0, -3.0));
assert_eq!(v2.cross(v1), Vec3::new(3.0, -6.0, 3.0));
source

pub fn fill(&mut self, value: T)

Fills all components of the calling Vec3<T> with the provided value.

Examples
use gamemath::Vec3;

let mut v = Vec3::new(0.0, 0.0, 0.0);

v.fill(6.0);

assert_eq!(v, Vec3::new(6.0, 6.0, 6.0));
source

pub fn length_squared(&self) -> T

Calculates the squared length/magnitude/norm of a Vec3<T>. This saves an expensive square root calculation compared to calculating the actual length, and comparing two squared lengths can therefore often be cheaper than, and yield the same result as, computing two real lengths.

Also useful for data types that does not implement a square root function, i.e. non-floating-point data types.

Examples
use gamemath::Vec3;

let v = Vec3::new(1.0, 2.0, 3.0);

assert_eq!(v.length_squared(), 14.0);
source

pub fn manhattan_distance(&self, right: Vec3<T>) -> T

Calculates and returns the manhattan distance between the two points pointed to by two Vec3<T> objects.

Examples
use gamemath::Vec3;

let v1 = Vec3::new(1.0, 2.0, 3.0);
let v2 = Vec3::new(2.0, 4.0, 6.0);

assert_eq!(v1.manhattan_distance(v2), 6.0);
source§

impl Vec3<f32>

source

pub fn length(&self) -> f32

Calculates the real length/magnitude/norm of a Vec3<f32>. This results in an expensive square root calculation, and you might want to consider using a squared length instead when possible.

Examples
use gamemath::Vec3;

let v = Vec3::new(1.0_f32, 4.0_f32, 8.0_f32);

assert_eq!(v.length(), 9.0_f32);
source

pub fn normalized(&self) -> Vec3<f32>

Calculates and returns the unit vector representation of a Vec3<f32>. This results in an an expensive square root calculation.

Examples
use gamemath::Vec3;

let v = Vec3::new(9.0_f32, 12.0_f32, 20.0_f32);

assert_eq!(v.normalized(), Vec3::new(0.36_f32, 0.48_f32, 0.8_f32));
source

pub fn normalize(&mut self)

Normalizes a Vec3<f32> into its unit vector representation. This results in an an expensive square root calculation.

Examples
use gamemath::Vec3;

let mut v = Vec3::new(9.0_f32, 12.0_f32, 20.0_f32);

v.normalize();

assert_eq!(v, Vec3::new(0.36_f32, 0.48_f32, 0.8_f32));
source§

impl Vec3<f64>

source

pub fn length(&self) -> f64

Calculates the real length/magnitude/norm of a Vec3<f64>. This results in an expensive square root calculation, and you might want to consider using a squared length instead when possible.

Examples
use gamemath::Vec3;

let v = Vec3::new(1.0_f64, 4.0_f64, 8.0_f64);

assert_eq!(v.length(), 9.0_f64);
source

pub fn normalized(&self) -> Vec3<f64>

Calculates and returns the unit vector representation of a Vec3<f64>. This results in an an expensive square root calculation.

Examples
use gamemath::Vec3;

let v = Vec3::new(9.0_f64, 12.0_f64, 20.0_f64);

assert_eq!(v.normalized(), Vec3::new(0.36_f64, 0.48_f64, 0.8_f64));
source

pub fn normalize(&mut self)

Normalizes a Vec3<f64> into its unit vector representation. This results in an an expensive square root calculation.

Examples
use gamemath::Vec3;

let mut v = Vec3::new(9.0_f64, 12.0_f64, 20.0_f64);

v.normalize();

assert_eq!(v, Vec3::new(0.36_f64, 0.48_f64, 0.8_f64));

Trait Implementations§

source§

impl<T: Add<Output = T>> Add for Vec3<T>

§

type Output = Vec3<T>

The resulting type after applying the + operator.
source§

fn add(self, right: Vec3<T>) -> Vec3<T>

Performs the + operation. Read more
source§

impl<T: AddAssign> AddAssign for Vec3<T>

source§

fn add_assign(&mut self, right: Vec3<T>)

Performs the += operation. Read more
source§

impl<T: Clone> Clone for Vec3<T>

source§

fn clone(&self) -> Vec3<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Vec3<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default> Default for Vec3<T>

source§

fn default() -> Vec3<T>

Returns the “default value” for a type. Read more
source§

impl<T: Copy> From<[T; 3]> for Vec3<T>

source§

fn from(slice: [T; 3]) -> Vec3<T>

Converts to this type from the input type.
source§

impl<T> From<(T, T, T)> for Vec3<T>

source§

fn from(tuple: (T, T, T)) -> Vec3<T>

Converts to this type from the input type.
source§

impl<T: Copy> From<T> for Vec3<T>

source§

fn from(value: T) -> Vec3<T>

Converts to this type from the input type.
source§

impl<T: Default> From<Vec2<T>> for Vec3<T>

source§

fn from(vec: Vec2<T>) -> Vec3<T>

Converts to this type from the input type.
source§

impl<T> From<Vec3<T>> for Vec2<T>

source§

fn from(vector: Vec3<T>) -> Vec2<T>

Converts to this type from the input type.
source§

impl<T: Default> From<Vec3<T>> for Vec4<T>

source§

fn from(vec: Vec3<T>) -> Vec4<T>

Converts to this type from the input type.
source§

impl<T> From<Vec4<T>> for Vec3<T>

source§

fn from(vec: Vec4<T>) -> Vec3<T>

Converts to this type from the input type.
source§

impl<T> Index<usize> for Vec3<T>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, index: usize) -> &T

Performs the indexing (container[index]) operation. Read more
source§

impl<T> IndexMut<usize> for Vec3<T>

source§

fn index_mut(&mut self, index: usize) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<T: Mul<Output = T> + Copy> Mul<T> for Vec3<T>

§

type Output = Vec3<T>

The resulting type after applying the * operator.
source§

fn mul(self, right: T) -> Vec3<T>

Performs the * operation. Read more
source§

impl Mul<Vec3<f32>> for Mat3

§

type Output = Vec3<f32>

The resulting type after applying the * operator.
source§

fn mul(self, vec: Vec3<f32>) -> Vec3<f32>

Performs the * operation. Read more
source§

impl<T: MulAssign + Copy> MulAssign<T> for Vec3<T>

source§

fn mul_assign(&mut self, right: T)

Performs the *= operation. Read more
source§

impl<T: Neg<Output = T>> Neg for Vec3<T>

§

type Output = Vec3<T>

The resulting type after applying the - operator.
source§

fn neg(self) -> Vec3<T>

Performs the unary - operation. Read more
source§

impl<T: PartialEq> PartialEq for Vec3<T>

source§

fn eq(&self, other: &Vec3<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Sub<Output = T>> Sub for Vec3<T>

§

type Output = Vec3<T>

The resulting type after applying the - operator.
source§

fn sub(self, right: Vec3<T>) -> Vec3<T>

Performs the - operation. Read more
source§

impl<T: SubAssign> SubAssign for Vec3<T>

source§

fn sub_assign(&mut self, right: Vec3<T>)

Performs the -= operation. Read more
source§

impl<T: Copy> Copy for Vec3<T>

source§

impl<T> StructuralPartialEq for Vec3<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Vec3<T>where T: RefUnwindSafe,

§

impl<T> Send for Vec3<T>where T: Send,

§

impl<T> Sync for Vec3<T>where T: Sync,

§

impl<T> Unpin for Vec3<T>where T: Unpin,

§

impl<T> UnwindSafe for Vec3<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.