#[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
impl Vector3
sourcepub fn new(x: f32, y: f32, z: f32) -> Vector3
pub fn new(x: f32, y: f32, z: f32) -> Vector3
Creates a new Vector3
with the given x
, y
, and z
components.
Arguments
x
- Thex
component of the new vector.y
- They
component of the new vector.z
- Thez
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);
sourcepub fn from_vec2(vec: Vector2, z: f32) -> Vector3
pub fn from_vec2(vec: Vector2, z: f32) -> Vector3
Creates a new Vector3
from a Vector2
and a z
component.
Arguments
vec
- TheVector2
to use for thex
andy
components of the new vector.z
- Thez
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);
sourcepub fn dot(&self, other: Vector3) -> f32
pub fn dot(&self, other: Vector3) -> f32
Computes the dot product of two Vector3
s.
Arguments
other
- The otherVector3
to compute the dot product with.
Returns
The dot product of the two Vector3
s.
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);
sourcepub fn cross(&self, other: Vector3) -> Vector3
pub fn cross(&self, other: Vector3) -> Vector3
Computes the cross product of two Vector3
s.
Arguments
other
- The otherVector3
to compute the cross product with.
Returns
The cross product of the two Vector3
s.
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);
pub fn length(&self) -> f32
sourcepub fn normalize(&mut self)
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);
sourcepub fn normalized(&self) -> Vector3
pub fn normalized(&self) -> Vector3
sourcepub fn xy(&self) -> Vector2
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);
sourcepub fn xz(&self) -> Vector2
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);
sourcepub fn yz(&self) -> Vector2
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);
sourcepub fn angle_between(&self, other: Vector3) -> f32
pub fn angle_between(&self, other: Vector3) -> f32
Returns the angle between this Vector3
and another Vector3
.
Arguments
other
- The otherVector3
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);
sourcepub fn reflect(&self, normal: Vector3) -> Vector3
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));
sourcepub fn rotate_about_axis(&self, axis: Vector3, angle: f32) -> Vector3
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 Index<usize> for Vector3
impl Index<usize> for Vector3
Allows indexing into a Vector2
.
source§fn index(&self, index: usize) -> &f32
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);
source§impl IndexMut<usize> for Vector3
impl IndexMut<usize> for Vector3
Allows indexing into a Vector3
mutably.