pub struct Vector2 { /* private fields */ }
Expand description
A 2D vector that holds an x-coordinate and y-coordinate
Implementations§
Source§impl Vector2
Constructors
impl Vector2
Constructors
Sourcepub fn new(x: f32, y: f32) -> Self
pub fn new(x: f32, y: f32) -> Self
Creates a new 2D vector
- x: The x coordinate of the vector
- y: The y coordinate of the vector
Returns: Returns a new 2D vector
§Examples
let vector = Vector2::new(1.2, 3.45);
assert_eq!(1.2, vector.x());
assert_eq!(3.45, vector.y());
Sourcepub fn from_vector3(vector: Vector3) -> Self
pub fn from_vector3(vector: Vector3) -> Self
Creates a new 2D vector from a 3D vector
- vector: The 3D vector to convert from
Returns: Returns a converted 2D vector
§Examples
let vector3 = Vector3::new(1.2, 3.45, 6.789);
let vector2 = Vector2::from_vector3(vector3);
assert_eq!(1.2, vector2.x());
assert_eq!(3.45, vector2.y());
Sourcepub fn zero() -> Self
pub fn zero() -> Self
Creates an empty 2D vector: (0, 0)
Returns: Returns an empty 2D vector
§Examples
let vector = Vector2::zero();
assert_eq!(0.0, vector.x());
assert_eq!(0.0, vector.y());
Sourcepub fn left() -> Self
pub fn left() -> Self
Creates a 2D unit vector that’s pointing to the left: (-1, 0)
Returns: Returns a 2D unit vector that’s pointing to the left
§Examples
let vector = Vector2::left();
assert_eq!(-1.0, vector.x());
assert_eq!(0.0, vector.y());
Sourcepub fn right() -> Self
pub fn right() -> Self
Creates a 2D unit vector that’s pointing to the right: (1, 0)
Returns: Returns a 2D unit vector that’s pointing to the right
§Examples
let vector = Vector2::right();
assert_eq!(1.0, vector.x());
assert_eq!(0.0, vector.y());
Sourcepub fn up() -> Self
pub fn up() -> Self
Creates a 2D unit vector that’s pointing up: (0, 1)
Returns: Returns a 2D unit vector that’s pointing up
§Examples
let vector = Vector2::up();
assert_eq!(0.0, vector.x());
assert_eq!(1.0, vector.y());
Sourcepub fn down() -> Self
pub fn down() -> Self
Creates a 2D unit vector that’s pointing down: (0, -1)
Returns: Returns a 2D unit vector that’s pointing down
§Examples
let vector = Vector2::down();
assert_eq!(0.0, vector.x());
assert_eq!(-1.0, vector.y());
Sourcepub fn one() -> Self
pub fn one() -> Self
Creates a 2D vector that contains 1 in all it’s components: (1, 1)
Returns: Returns a 2D vector that contains 1 in all it’s components
§Examples
let vector = Vector2::one();
assert_eq!(1.0, vector.x());
assert_eq!(1.0, vector.y());
Sourcepub fn from_heading(angle: f32) -> Self
pub fn from_heading(angle: f32) -> Self
Creates a 2D vector from a single angle (heading)
- angle: The angle in radians to create the 2D vector from
Returns: Returns a 2D vector from the single angle
§Examples
let vector = Vector2::from_heading(Math::PI_OVER_4);
assert_range!(0.7071068, vector.x());
assert_range!(0.7071068, vector.y());
let vector = Vector2::from_heading(4.0);
assert_range!(-0.653643620864, vector.x());
assert_range!(-0.756802495308, vector.y());
Sourcepub fn from_heading_deg(angle: f32) -> Self
pub fn from_heading_deg(angle: f32) -> Self
Creates a 2D vector from a single angle (heading)
- angle: The angle in degrees to create the 2D vector from
Returns: Returns a 2D vector from the single angle
§Examples
let vector = Vector2::from_heading_deg(45.0);
assert_range!(0.7071068, vector.x());
assert_range!(0.7071068, vector.y());
let vector = Vector2::from_heading_deg(229.183118052);
assert_range!(-0.653643620864, vector.x());
assert_range!(-0.756802495308, vector.y());
Source§impl Vector2
Properties
impl Vector2
Properties
Sourcepub fn x(&self) -> f32
pub fn x(&self) -> f32
Gets the x coordinate of the vector
Returns: Returns the x coordinate of the vector
Sourcepub fn set_x(&mut self, value: f32)
pub fn set_x(&mut self, value: f32)
Sets the x coordinate of the vector
- value: The value to set the x coordinate of the vector
Sourcepub fn y(&self) -> f32
pub fn y(&self) -> f32
Gets the y coordinate of the vector
Returns: Returns the y coordinate of the vector
Sourcepub fn set_y(&mut self, value: f32)
pub fn set_y(&mut self, value: f32)
Sets the y coordinate of the vector
- value: The value to set the y coordinate of the vector
Sourcepub fn heading(&self) -> f32
pub fn heading(&self) -> f32
Get the heading from the vector in radians
Returns: Returns the heading from the vector in radians
§Examples
let heading = Vector2::one().heading();
assert_range!(Math::PI_OVER_4, heading);
Sourcepub fn set_heading(&mut self, angle: f32)
pub fn set_heading(&mut self, angle: f32)
Sets the heading for the vector in radians
- angle: The angle to set the heading of the vector for in radians
§Examples
let mut vector = Vector2::zero();
vector.set_heading(Math::PI_OVER_4);
assert_range!(0.70710678118, vector.x());
assert_range!(0.70710678118, vector.y());
Sourcepub fn heading_deg(&self) -> f32
pub fn heading_deg(&self) -> f32
Get the heading from the vector in degrees
Returns: Returns the heading from the vector in degrees
§Examples
let heading = Vector2::one().heading_deg();
assert_range!(45.0, heading, 0.001);
Sourcepub fn set_heading_deg(&mut self, angle: f32)
pub fn set_heading_deg(&mut self, angle: f32)
Sets the heading for the vector in degrees
- angle: The angle to set the heading of the vector for in degrees
§Examples
let mut vector = Vector2::zero();
vector.set_heading_deg(45.0);
assert_range!(0.70710678118, vector.x());
assert_range!(0.70710678118, vector.y());
Sourcepub fn magnitude(&self) -> f32
pub fn magnitude(&self) -> f32
Gets the magnitude of the vector. This returns the length of the vector
Returns: Returns the magnitude of the vector
§Examples
let a = Vector2::new(-1.0, 2.0);
assert_eq!(2.236068, a.magnitude());
Sourcepub fn square_magnitude(&self) -> f32
pub fn square_magnitude(&self) -> f32
Gets the magnitude squared, avoiding the use of a square root
Returns: Returns the magnitude of the vector squared
§Examples
let a = Vector2::new(-1.0, 2.0);
assert_eq!(5.0, a.square_magnitude());
Source§impl Vector2
Public Methods
impl Vector2
Public Methods
Sourcepub fn angle_between(self, rhs: Vector2) -> f32
pub fn angle_between(self, rhs: Vector2) -> f32
Gets the angle between the two vectors in radians
- rhs: The other vector to get the angle from
Returns: Returns the angle between the two vectors in radians
§Examples
let a = Vector2::new(0.25, -0.5);
let b = Vector2::new(2.0, 0.5);
assert_range!(1.35212751547, a.angle_between(b));
Sourcepub fn angle_between_deg(self, rhs: Vector2) -> f32
pub fn angle_between_deg(self, rhs: Vector2) -> f32
Gets the angle between the two vectors in degrees
- rhs: The other vector to get the angle from
Returns: Returns the angle between the two vectors in degrees
§Examples
let a = Vector2::new(0.25, -0.5);
let b = Vector2::new(2.0, 0.5);
assert_range!(77.4712, a.angle_between_deg(b), 0.01);
Sourcepub fn distance(self, rhs: Vector2) -> f32
pub fn distance(self, rhs: Vector2) -> f32
Gets the distance between the two vectors
- rhs: The other vector to get the distance between
Returns: Returns the distance between the two vectors
§Examples
let a = Vector2::new(0.25, -0.5);
let b = Vector2::new(2.0, 0.5);
assert_eq!(2.0155644, a.distance(b));
Sourcepub fn dot(self, rhs: Vector2) -> f32
pub fn dot(self, rhs: Vector2) -> f32
Gets the dot product of between the two vectors. It can be used to determine the angle between two vectors.
- rhs: The other vector to dot product with
Returns: Returns the dot product
§Remarks
Using two unit vectors, the maximum range of numbers go from -1 to 1. It scales with
the magnitude of both vectors (multiplying them together a.magnitude() * b.magnitude()
)
§Examples
let a = Vector2::one();
let b = Vector2::new(0.25, 1.1);
let dot = a.dot(b);
assert_eq!(1.35, dot);
Note that if the angle is 90 degrees (PI / 2) then it’s going to return 0
let a = Vector2::right();
let b = 2.0 * Vector2::up();
let dot = a.dot(b);
assert_eq!(0.0, dot);
Where as, if the angle is 0 degrees or 180 degrees (PI) then it’s going to return 1 and -1 respectively; given that the two vectors are unit vectors
let a = Vector2::right();
let b = Vector2::left();
let dot_one = a.dot(a);
let dot_negative_one = a.dot(b);
assert_eq!(1.0, dot_one);
assert_eq!(-1.0, dot_negative_one);
Sourcepub fn lerp(self, rhs: Vector2, t: f32) -> Self
pub fn lerp(self, rhs: Vector2, t: f32) -> Self
Linearly interpolates between the this and the other vector
- rhs: The other vector to end from
- t: The ratio value to interpolate between both vectors. Clamped between 0.0 and 1.0
Returns: Returns the interpolated vector
§Examples
let a = Vector2::new(0.0, -10.0);
let b = Vector2::new(1.0, -4.0);
let expected = Vector2::new(0.7, -5.8);
assert_eq!(expected, a.lerp_unclamped(b, 0.7));
Sourcepub fn lerp_unclamped(self, rhs: Vector2, t: f32) -> Self
pub fn lerp_unclamped(self, rhs: Vector2, t: f32) -> Self
Linearly interpolates between the this and the other vector (not clamped)
- rhs: The other vector to end from
- t: The ratio value to interpolate between both vectors
Returns: Returns the interpolated vector
§Examples
let a = Vector2::new(0.0, -10.0);
let b = Vector2::new(1.0, -4.0);
let expected = Vector2::new(0.7, -5.8);
assert_eq!(expected, a.lerp_unclamped(b, 0.7));
Sourcepub fn move_towards(self, target: Vector2, delta: f32) -> Self
pub fn move_towards(self, target: Vector2, delta: f32) -> Self
Moves this vector towards the target vector, it will never move past the target
- target: The target vector to move towards
- delta: The delta distance to try and move with, defines the maximum distance moved
Returns: Returns the vector that is closer towards the target
§Examples
let a = Vector2::new(0.25, -0.5);
let b = Vector2::new(2.0, 0.5);
let expected = Vector2::new(0.42364863, -0.4007722);
assert_eq!(expected, a.move_towards(b, 0.2));
assert_eq!(b, a.move_towards(b, 20.0));
Sourcepub fn normalize(self) -> Self
pub fn normalize(self) -> Self
Normalizes the vector
Returns: Returns the unit vector version of this vector
§Examples
let vector = Vector2::one().normalize();
assert_range!(0.70710678118, vector.x());
assert_range!(0.70710678118, vector.y());
let vector = Vector2::new(-0.1, 1.0).normalize();
assert_range!(-0.09950372, vector.x());
assert_range!(0.99503714, vector.y());
Sourcepub fn perpendicular(self) -> Self
pub fn perpendicular(self) -> Self
Creates a perpendicular 2D vector
Returns: Returns a perpendicular 2D vector
§Examples
let vector = Vector2::new(1.0, 2.0);
let perpendicular = vector.perpendicular();
assert_eq!(0.0, vector * perpendicular);
Sourcepub fn project(self, rhs: Vector2) -> Self
pub fn project(self, rhs: Vector2) -> Self
Projects this vector onto the given vector
- rhs: The vector to project onto
Returns: Returns the projected vector
§Examples
let a = Vector2::new(1.0, 2.0);
let b = Vector2::new(3.0, 4.0);
let expected = Vector2::new(1.32, 1.76);
assert_range!(expected.x(), a.project(b).x());
assert_range!(expected.y(), a.project(b).y());
Sourcepub fn reject(self, rhs: Vector2) -> Self
pub fn reject(self, rhs: Vector2) -> Self
Rejects this vector from the given vector
- rhs: The vector to reject from
Returns: Returns the rejected vector
§Examples
let a = Vector2::new(1.0, 2.0);
let b = Vector2::new(3.0, 4.0);
let expected = Vector2::new(-0.32, 0.24);
assert_range!(expected.x(), a.reject(b).x());
assert_range!(expected.y(), a.reject(b).y());
Sourcepub fn reflect(self, normal: Vector2) -> Self
pub fn reflect(self, normal: Vector2) -> Self
Reflects this vector using a normal vector
- normal: The normal vector to reflect off of
Returns: Returns the reflected vector
§Examples
let direction = Vector2::new(1.0, 0.0);
let normal = Vector2::new(1.0, 1.0);
let expected = Vector2::new(-1.0, -2.0);
assert_eq!(expected, direction.reflect(normal));
let direction = Vector2::new(0.25, -0.5);
let normal = Vector2::new(1.0, 0.5);
let expected = Vector2::new(0.25, -0.5);
assert_eq!(expected, direction.reflect(normal));
Sourcepub fn scale(self, rhs: Vector2) -> Self
pub fn scale(self, rhs: Vector2) -> Self
Scales the vector using another vector, multiplying everything component-wise
- rhs: The other vector to scale with
Returns: Returns the scaled vector
§Examples
let a = Vector2::new(0.25, -0.5);
let b = Vector2::new(2.0, 0.5);
let expected = Vector2::new(0.5, -0.25);
assert_eq!(expected, a.scale(b));
Sourcepub fn signed_angle_between(self, rhs: Vector2) -> f32
pub fn signed_angle_between(self, rhs: Vector2) -> f32
Gets the signed angle between the two vectors using an axis in radians
- rhs: The other vector to get the angle from
Returns: Returns the signed angle between the two vectors using an axis in radians
§Examples
let a = Vector2::new(0.25, -0.5);
let b = Vector2::new(-2.0, 0.5);
assert_range!(-2.27942269238, a.signed_angle_between(b));
Sourcepub fn signed_angle_between_deg(self, rhs: Vector2) -> f32
pub fn signed_angle_between_deg(self, rhs: Vector2) -> f32
Gets the signed angle between the two vectors using an axis in degrees
- rhs: The other vector to get the angle from
Returns: Returns the signed angle between the two vectors using an axis in degrees
§Examples
let a = Vector2::new(0.25, -0.5);
let b = Vector2::new(-2.0, 0.5);
assert_range!(-130.6013, a.signed_angle_between_deg(b), 0.01);
Trait Implementations§
Source§impl AddAssign<Vector2> for Vector3
impl AddAssign<Vector2> for Vector3
Source§fn add_assign(&mut self, rhs: Vector2)
fn add_assign(&mut self, rhs: Vector2)
+=
operation. Read moreSource§impl AddAssign<Vector3> for Vector2
impl AddAssign<Vector3> for Vector2
Source§fn add_assign(&mut self, rhs: Vector3)
fn add_assign(&mut self, rhs: Vector3)
+=
operation. Read moreSource§impl AddAssign for Vector2
impl AddAssign for Vector2
Source§fn add_assign(&mut self, rhs: Vector2)
fn add_assign(&mut self, rhs: Vector2)
+=
operation. Read moreSource§impl DivAssign<f32> for Vector2
impl DivAssign<f32> for Vector2
Source§fn div_assign(&mut self, rhs: f32)
fn div_assign(&mut self, rhs: f32)
/=
operation. Read moreSource§impl DivAssign<i32> for Vector2
impl DivAssign<i32> for Vector2
Source§fn div_assign(&mut self, rhs: i32)
fn div_assign(&mut self, rhs: i32)
/=
operation. Read moreSource§impl Mul<Vector2> for Quaternion
impl Mul<Vector2> for Quaternion
Source§impl MulAssign<f32> for Vector2
impl MulAssign<f32> for Vector2
Source§fn mul_assign(&mut self, rhs: f32)
fn mul_assign(&mut self, rhs: f32)
*=
operation. Read moreSource§impl MulAssign<i32> for Vector2
impl MulAssign<i32> for Vector2
Source§fn mul_assign(&mut self, rhs: i32)
fn mul_assign(&mut self, rhs: i32)
*=
operation. Read moreSource§impl SubAssign<Vector2> for Vector3
impl SubAssign<Vector2> for Vector3
Source§fn sub_assign(&mut self, rhs: Vector2)
fn sub_assign(&mut self, rhs: Vector2)
-=
operation. Read moreSource§impl SubAssign<Vector3> for Vector2
impl SubAssign<Vector3> for Vector2
Source§fn sub_assign(&mut self, rhs: Vector3)
fn sub_assign(&mut self, rhs: Vector3)
-=
operation. Read moreSource§impl SubAssign for Vector2
impl SubAssign for Vector2
Source§fn sub_assign(&mut self, rhs: Vector2)
fn sub_assign(&mut self, rhs: Vector2)
-=
operation. Read more