#[repr(C)]pub struct Vector2 {
pub x: f32,
pub y: f32,
}
Expand description
A 2-dimensional vector with x
and y
components.
Fields§
§x: f32
§y: f32
Implementations§
source§impl Vector2
impl Vector2
sourcepub fn dot(&self, other: &Vector2) -> f32
pub fn dot(&self, other: &Vector2) -> f32
Calculates the dot product of this vector and another vector.
Arguments
other
- The other vector to calculate the dot product with.
Returns
The dot product of this vector and the other vector.
Examples
use cgl_rs::math::Vector2;
let vec1 = Vector2::new(1.0, 2.0);
let vec2 = Vector2::new(3.0, 4.0);
let dot_product = vec1.dot(&vec2);
sourcepub fn cross(&self, other: &Vector2) -> f32
pub fn cross(&self, other: &Vector2) -> f32
Calculates the cross product of this vector and another vector.
Arguments
other
- The other vector to calculate the cross product with.
Returns
The cross product of this vector and the other vector.
Examples
use cgl_rs::math::Vector2;
let vec1 = Vector2::new(1.0, 2.0);
let vec2 = Vector2::new(3.0, 4.0);
let cross_product = vec1.cross(&vec2);
sourcepub fn rotate_about_origin(&self, angle: f32) -> Vector2
pub fn rotate_about_origin(&self, angle: f32) -> Vector2
Rotates this vector about the origin by the given angle (in radians).
Arguments
angle
- The angle (in radians) to rotate this vector by.
Returns
A new Vector2
representing the result of rotating this vector about the origin by the given angle.
Examples
use cgl_rs::math::Vector2;
let vec = Vector2::new(1.0, 0.0);
let rotated_vec = vec.rotate_about_origin(std::f32::consts::PI / 2.0);
sourcepub fn from_angle(angle: f32) -> Vector2
pub fn from_angle(angle: f32) -> Vector2
sourcepub fn angle_between(&self, other: &Vector2) -> f32
pub fn angle_between(&self, other: &Vector2) -> f32
Calculates the angle (in radians) between this vector and another vector.
Arguments
other
- The other vector to calculate the angle between.
Returns
The angle (in radians) between this vector and the other vector.
Examples
use cgl_rs::math::Vector2;
let vec1 = Vector2::new(1.0, 0.0);
let vec2 = Vector2::new(0.0, 1.0);
let angle = vec1.angle_between(&vec2);
sourcepub fn normalize(&mut self)
pub fn normalize(&mut self)
sourcepub fn normalized(&self) -> Vector2
pub fn normalized(&self) -> Vector2
sourcepub fn perpendicular(&self) -> Vector2
pub fn perpendicular(&self) -> Vector2
sourcepub fn reflect(&self, normal: &Vector2) -> Vector2
pub fn reflect(&self, normal: &Vector2) -> Vector2
Returns the reflection of this vector off a surface with the given normal.
Arguments
normal
- The normal of the surface being reflected off of.
Returns
The reflection of this vector off the surface with the given normal.
Examples
use cgl_rs::math::Vector2;
let vec = Vector2::new(1.0, 2.0);
let normal = Vector2::new(0.0, 1.0);
let reflected_vec = vec.reflect(&normal);
Trait Implementations§
source§impl Index<usize> for Vector2
impl Index<usize> for Vector2
Allows indexing into a Vector2
by usize
index.
source§fn index(&self, index: usize) -> &f32
fn index(&self, index: usize) -> &f32
Returns a reference to the f32
value at the given index.
Arguments
index
- The index of the value to retrieve. Must be0
or1
.
Returns
A reference to the f32
value at the given index.
Panics
Panics if the index is not 0
or 1
.
Examples
use cgl_rs::math::Vector2;
let vec = Vector2::new(1.0, 2.0);
assert_eq!(vec[0], 1.0);
assert_eq!(vec[1], 2.0);
source§impl IndexMut<usize> for Vector2
impl IndexMut<usize> for Vector2
Allows mutable indexing into a Vector2
by usize
index.
source§fn index_mut(&mut self, index: usize) -> &mut f32
fn index_mut(&mut self, index: usize) -> &mut f32
Returns a mutable reference to the f32
value at the given index.
Arguments
index
- The index of the value to retrieve. Must be0
or1
.
Returns
A mutable reference to the f32
value at the given index.
Panics
Panics if the index is not 0
or 1
.
Examples
use cgl_rs::math::Vector2;
let mut vec = Vector2::new(1.0, 2.0);
vec[0] = 3.0;
assert_eq!(vec.x, 3.0);