pub struct Vec2 {
pub x: f32,
pub y: f32,
}
Expand description
2D f32 Vector
Fields§
§x: f32
§y: f32
Implementations§
Source§impl Vec2
impl Vec2
pub fn new(x: f32, y: f32) -> Vec2
Sourcepub fn set(&mut self, x: f32, y: f32)
pub fn set(&mut self, x: f32, y: f32)
Sets the values of this vector.
§Examples
use gamevecs::Vec2;
let mut vec = Vec2::new(3.0, 4.0);
//set the values
vec.set(4.0, 5.0);
assert_eq!(Vec2::new(4.0, 5.0), vec);
Sourcepub fn magnitude(&self) -> f32
pub fn magnitude(&self) -> f32
Returns the length (distance to (0|0)) of this vector.
§Examples
use gamevecs::Vec2;
let vec = Vec2::new(3.0, 4.0);
//get the magnitude
let magnitude = vec.magnitude();
assert_eq!(5.0, magnitude);
Sourcepub fn magnitude_squared(&self) -> f32
pub fn magnitude_squared(&self) -> f32
Returns the squared length of this vector.
§Examples
use gamevecs::Vec2;
let vec = Vec2::new(3.0, 4.0);
//get the magnitude
let magnitude = vec.magnitude_squared();
assert_eq!(25.0, magnitude);
Sourcepub fn equals(&self, other: Vec2, epsilon: f32) -> bool
pub fn equals(&self, other: Vec2, epsilon: f32) -> bool
Returns true if this vector approximately equals another one.
§Examples
use gamevecs::Vec2;
let vec = Vec2::new(3.000001, 4.000001);
let other_vec = Vec2::new(3.0, 4.0);
let equal = vec.equals(other_vec, 1e-5);
assert_eq!(true, equal);
Sourcepub fn normalized(&self) -> Vec2
pub fn normalized(&self) -> Vec2
Returns this vector with a magnitude of 1. Used when only the direction of the Vector is important.
§Examples
use gamevecs::Vec2;
let vec1 = Vec2::new(5.0, 0.0);
//normalize
let normalized = vec1.normalized();
assert_eq!(Vec2::new(1.0, 0.0), normalized);
Sourcepub fn distance_to(&self, other: Vec2) -> f32
pub fn distance_to(&self, other: Vec2) -> f32
Returns the distance from this vector to another.
§Examples
use gamevecs::Vec2;
let vec1 = Vec2::new(7.0, 8.0);
let vec2 = Vec2::new(4.0, 4.0);
//get the distance
let distance = vec1.distance_to(vec2);
assert_eq!(5.0, distance);
Sourcepub fn distance_to_squared(&self, other: Vec2) -> f32
pub fn distance_to_squared(&self, other: Vec2) -> f32
Returns the squared distance from this vector to another.
§Examples
use gamevecs::Vec2;
let vec1 = Vec2::new(7.0, 8.0);
let vec2 = Vec2::new(4.0, 4.0);
//get the squared distance
let distance = vec1.distance_to_squared(vec2);
assert_eq!(25.0, distance);
Sourcepub fn dot(&self, other: Vec2) -> f32
pub fn dot(&self, other: Vec2) -> f32
Returns the dot product of this vector and another one.
§Examples
use gamevecs::Vec2;
let vec1 = Vec2::new(3.0, 4.0);
let vec2 = Vec2::new(-2.0, 5.0);
//get the dot product
let dot_product = vec1.dot(vec2);
assert_eq!(14.0, dot_product);
Sourcepub fn angle_between(&self, other: Vec2) -> f32
pub fn angle_between(&self, other: Vec2) -> f32
Returns the angle between this vector and another one.
§Examples
use gamevecs::Vec2;
let vec1 = Vec2::new(1.0, 1.0);
let vec2 = Vec2::new(-1.0, -1.0);
//get the dot product
let angle = vec1.angle_between(vec2);
assert_eq!(180.0, angle.to_degrees());
Sourcepub fn lerp(&self, other: Vec2, t: f32) -> Vec2
pub fn lerp(&self, other: Vec2, t: f32) -> Vec2
Returns the linear interpolation by t between this and another vector.
§Examples
use gamevecs::Vec2;
let vec1 = Vec2::new(1.0, 2.0);
let other = Vec2::new(3.0, 4.0);
//lerp
let new_vec = vec1.lerp(other, 0.5);
assert_eq!(Vec2::new(2.0, 3.0), new_vec);
Sourcepub fn add_length_by_angle(&self, angle: f32, length: f32) -> Vec2
pub fn add_length_by_angle(&self, angle: f32, length: f32) -> Vec2
Adds the length in direction of the angle in radians to the vector and returns the result as a new Vec2.
§Examples
use gamevecs::Vec2;
let vec1 = Vec2::new(3.0, 4.0);
//add length
let new_vec = vec1.add_length_by_angle(53.13f32.to_radians(), 5.0);
//using the approximate equals function due to floating point inaccuracity
assert!(new_vec.equals(Vec2::new(6.0, 8.0), 1e-4));
Sourcepub fn project(&self, onto: Vec2) -> Vec2
pub fn project(&self, onto: Vec2) -> Vec2
Projects a vector onto another and returns the result as a new Vec2.
§Examples
use gamevecs::Vec2;
let vec1 = Vec2::new(3.0, 4.0);
let vec2 = Vec2::new(1.0, 0.0);
//project
let projection = vec1.project(vec2);
assert_eq!(Vec2::new(3.0, 0.0), projection);
Sourcepub fn rotate(&self, angle: f32) -> Vec2
pub fn rotate(&self, angle: f32) -> Vec2
Rotates a vector counterclockwise and returns the result as a new Vec2.
§Examples
use gamevecs::Vec2;
let vec1 = Vec2::new(1.0, 0.0);
//rotate
let rotated_vec = vec1.rotate(90.0f32.to_radians());
//using the approximate equals function due to floating point inaccuracity
assert!(rotated_vec.equals(Vec2::new(0.0, 1.0), 1e-4));
Trait Implementations§
Source§impl AddAssign for Vec2
impl AddAssign for Vec2
Source§fn add_assign(&mut self, rhs: Vec2)
fn add_assign(&mut self, rhs: Vec2)
+=
operation. Read moreSource§impl DivAssign for Vec2
impl DivAssign for Vec2
Source§fn div_assign(&mut self, rhs: Vec2)
fn div_assign(&mut self, rhs: Vec2)
/=
operation. Read moreSource§impl MulAssign for Vec2
impl MulAssign for Vec2
Source§fn mul_assign(&mut self, rhs: Vec2)
fn mul_assign(&mut self, rhs: Vec2)
*=
operation. Read moreSource§impl SubAssign for Vec2
impl SubAssign for Vec2
Source§fn sub_assign(&mut self, rhs: Vec2)
fn sub_assign(&mut self, rhs: Vec2)
-=
operation. Read more