Struct cgl_rs::math::Vector3

source ·
#[repr(C)]
pub struct Vector3 { pub x: f32, pub y: f32, pub z: f32, }
Expand description

A 3-dimensional vector with x, y and z components.

Fields§

§x: f32§y: f32§z: f32

Implementations§

source§

impl Vector3

source

pub fn new(x: f32, y: f32, z: f32) -> Vector3

Creates a new Vector3 with the given x, y, and z components.

Arguments
  • x - The x component of the new vector.
  • y - The y component of the new vector.
  • z - The z component of the new vector.
Returns

A new Vector3 with the given x, y, and z components.

Examples
use cgl_rs::math::Vector3;

let vec = Vector3::new(1.0, 2.0, 3.0);
source

pub fn from_vec2(vec: Vector2, z: f32) -> Vector3

Creates a new Vector3 from a Vector2 and a z component.

Arguments
  • vec - The Vector2 to use for the x and y components of the new vector.
  • z - The z component of the new vector.
Returns

A new Vector3 with the x and y components taken from vec and the z component set to z.

Examples
use cgl_rs::math::{Vector2, Vector3};

let vec2 = Vector2::new(1.0, 2.0);
let vec3 = Vector3::from_vec2(vec2, 3.0);
assert_eq!(vec3.x, 1.0);
assert_eq!(vec3.y, 2.0);
assert_eq!(vec3.z, 3.0);
source

pub fn zero() -> Vector3

Returns a new Vector3 with all components set to zero.

Returns

A new Vector3 with all components set to zero.

Examples
use cgl_rs::math::Vector3;

let vec = Vector3::zero();
assert_eq!(vec.x, 0.0);
assert_eq!(vec.y, 0.0);
assert_eq!(vec.z, 0.0);
source

pub fn one() -> Vector3

Returns a new Vector3 with all components set to one.

Returns

A new Vector3 with all components set to one.

Examples
use cgl_rs::math::Vector3;

let vec = Vector3::one();
assert_eq!(vec.x, 1.0);
assert_eq!(vec.y, 1.0);
assert_eq!(vec.z, 1.0);
source

pub fn dot(&self, other: Vector3) -> f32

Computes the dot product of two Vector3s.

Arguments
  • other - The other Vector3 to compute the dot product with.
Returns

The dot product of the two Vector3s.

Examples
use cgl_rs::math::Vector3;

let vec1 = Vector3::new(1.0, 2.0, 3.0);
let vec2 = Vector3::new(4.0, 5.0, 6.0);
let dot_product = vec1.dot(vec2);
assert_eq!(dot_product, 32.0);
source

pub fn cross(&self, other: Vector3) -> Vector3

Computes the cross product of two Vector3s.

Arguments
  • other - The other Vector3 to compute the cross product with.
Returns

The cross product of the two Vector3s.

Examples
use cgl_rs::math::Vector3;

let vec1 = Vector3::new(1.0, 2.0, 3.0);
let vec2 = Vector3::new(4.0, 5.0, 6.0);
let cross_product = vec1.cross(vec2);
assert_eq!(cross_product.x, -3.0);
assert_eq!(cross_product.y, 6.0);
assert_eq!(cross_product.z, -3.0);
source

pub fn length(&self) -> f32

source

pub fn normalize(&mut self)

Normalizes the Vector3 in place.

Examples
use cgl_rs::math::Vector3;

let mut vec = Vector3::new(1.0, 2.0, 2.0);
vec.normalize();
assert_eq!(vec.length(), 1.0);
source

pub fn normalized(&self) -> Vector3

Returns a new Vector3 that is the normalized version of this Vector3.

Returns

A new Vector3 that is the normalized version of this Vector3.

Examples
use cgl_rs::math::Vector3;

let vec = Vector3::new(1.0, 2.0, 2.0);
let normalized_vec = vec.normalized();
assert_eq!(normalized_vec.length(), 1.0);
source

pub fn xy(&self) -> Vector2

Returns a new Vector2 containing the x and y components of this Vector3.

Returns

A new Vector2 containing the x and y components of this Vector3.

Examples
use cgl_rs::math::{Vector2, Vector3};

let vec = Vector3::new(1.0, 2.0, 3.0);
let vec2 = vec.xy();
assert_eq!(vec2.x, 1.0);
assert_eq!(vec2.y, 2.0);
source

pub fn xz(&self) -> Vector2

Returns a new Vector2 containing the x and z components of this Vector3.

Returns

A new Vector2 containing the x and z components of this Vector3.

Examples
use cgl_rs::math::{Vector2, Vector3};

let vec = Vector3::new(1.0, 2.0, 3.0);
let vec2 = vec.xz();
assert_eq!(vec2.x, 1.0);
assert_eq!(vec2.y, 3.0);
source

pub fn yz(&self) -> Vector2

Returns a new Vector2 containing the y and z components of this Vector3.

Returns

A new Vector2 containing the y and z components of this Vector3.

Examples
use cgl_rs::math::{Vector2, Vector3};

let vec = Vector3::new(1.0, 2.0, 3.0);
let vec2 = vec.yz();
assert_eq!(vec2.x, 2.0);
assert_eq!(vec2.y, 3.0);
source

pub fn angle_between(&self, other: Vector3) -> f32

Returns the angle between this Vector3 and another Vector3.

Arguments
  • other - The other Vector3 to calculate the angle with.
Returns

The angle between this Vector3 and other in radians.

Examples
use cgl_rs::math::Vector3;

let vec1 = Vector3::new(1.0, 0.0, 0.0);
let vec2 = Vector3::new(0.0, 1.0, 0.0);
let angle = vec1.angle_between(vec2);
assert_eq!(angle, std::f32::consts::FRAC_PI_2);
source

pub fn reflect(&self, normal: Vector3) -> Vector3

Calculates the reflection of this Vector3 off a surface with the given normal.

Arguments
  • normal - The normal of the surface to reflect off of.
Returns

The reflection of this Vector3 off the surface with the given normal.

Examples
use cgl_rs::math::Vector3;

let vec = Vector3::new(1.0, 1.0, 0.0);
let normal = Vector3::new(0.0, 1.0, 0.0);
let reflected = vec.reflect(normal);
assert_eq!(reflected, Vector3::new(1.0, -1.0, 0.0));
source

pub fn rotate_about_axis(&self, axis: Vector3, angle: f32) -> Vector3

Rotates this Vector3 about the given axis by the given angle in radians.

Arguments
  • axis - The axis to rotate about.
  • angle - The angle to rotate by in radians.
Returns

The rotated Vector3.

Examples
use cgl_rs::math::Vector3;

let vec = Vector3::new(1.0, 0.0, 0.0);
let axis = Vector3::new(0.0, 0.0, 1.0);
let angle = std::f32::consts::FRAC_PI_2;
let rotated = vec.rotate_about_axis(axis, angle);
assert_eq!(rotated, Vector3::new(0.0, 1.0, 0.0));

Trait Implementations§

source§

impl Add<Vector3> for Vector3

§

type Output = Vector3

The resulting type after applying the + operator.
source§

fn add(self, rhs: Vector3) -> Vector3

Performs the + operation. Read more
source§

impl Clone for Vector3

source§

fn clone(&self) -> Vector3

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 Debug for Vector3

source§

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

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

impl Div<Vector3> for Vector3

§

type Output = Vector3

The resulting type after applying the / operator.
source§

fn div(self, rhs: Vector3) -> Vector3

Performs the / operation. Read more
source§

impl Div<f32> for Vector3

§

type Output = Vector3

The resulting type after applying the / operator.
source§

fn div(self, rhs: f32) -> Vector3

Performs the / operation. Read more
source§

impl Index<usize> for Vector3

Allows indexing into a Vector2.

source§

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

Returns a reference to the element at the given index.

Arguments
  • index - The index of the element to return.
Panics

Panics if the index is out of bounds for a Vector3.

Examples
use cgl_rs::math::Vector3;

let vec = Vector3::new(1.0, 2.0, 3.0);
assert_eq!(vec[0], 1.0);
assert_eq!(vec[1], 2.0);
assert_eq!(vec[2], 3.0);
§

type Output = f32

The returned type after indexing.
source§

impl IndexMut<usize> for Vector3

Allows indexing into a Vector3 mutably.

source§

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

Allows mutable indexing into a Vector3.

Arguments
  • index - The index of the element to return.
Panics

Panics if the index is out of bounds for a Vector3.

Examples
use cgl_rs::math::Vector3;

let mut vec = Vector3::new(1.0, 2.0, 3.0);
vec[0] = 4.0;
vec[1] = 5.0;
vec[2] = 6.0;
source§

impl Mul<Vector3> for Vector3

§

type Output = Vector3

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Vector3) -> Vector3

Performs the * operation. Read more
source§

impl Mul<f32> for Vector3

§

type Output = Vector3

The resulting type after applying the * operator.
source§

fn mul(self, rhs: f32) -> Vector3

Performs the * operation. Read more
source§

impl Neg for Vector3

§

type Output = Vector3

The resulting type after applying the - operator.
source§

fn neg(self) -> Vector3

Performs the unary - operation. Read more
source§

impl PartialEq<Vector3> for Vector3

source§

fn eq(&self, other: &Vector3) -> 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 Sub<Vector3> for Vector3

§

type Output = Vector3

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Vector3) -> Vector3

Performs the - operation. Read more
source§

impl Copy for Vector3

source§

impl StructuralPartialEq for Vector3

Auto Trait Implementations§

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<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.