Struct Vector2

Source
pub struct Vector2 { /* private fields */ }
Expand description

A 2D vector that holds an x-coordinate and y-coordinate

Implementations§

Source§

impl Vector2

Constructors

Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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

Source

pub fn x(&self) -> f32

Gets the x coordinate of the vector

Returns: Returns the x coordinate of the vector

Source

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
Source

pub fn y(&self) -> f32

Gets the y coordinate of the vector

Returns: Returns the y coordinate of the vector

Source

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
Source

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);
Source

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());
Source

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);
Source

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());
Source

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());
Source

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

Source

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));
Source

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);
Source

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));
Source

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);
Source

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));
Source

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));
Source

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));
Source

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());
Source

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);
Source

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());
Source

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());
Source

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));
Source

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));
Source

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));
Source

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);
Source§

impl Vector2

Conversions

Source

pub fn to_vector3(self) -> Vector3

Trait Implementations§

Source§

impl Add<Vector2> for Vector3

Source§

type Output = Vector3

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Vector2) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<Vector3> for Vector2

Source§

type Output = Vector3

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add for Vector2

Source§

type Output = Vector2

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Vector2) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<Vector2> for Vector3

Source§

fn add_assign(&mut self, rhs: Vector2)

Performs the += operation. Read more
Source§

impl AddAssign<Vector3> for Vector2

Source§

fn add_assign(&mut self, rhs: Vector3)

Performs the += operation. Read more
Source§

impl AddAssign for Vector2

Source§

fn add_assign(&mut self, rhs: Vector2)

Performs the += operation. Read more
Source§

impl Clone for Vector2

Source§

fn clone(&self) -> Vector2

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 Vector2

Source§

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

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

impl Div<Vector2> for f32

Source§

type Output = Vector2

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Vector2) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<Vector2> for i32

Source§

type Output = Vector2

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Vector2) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<f32> for Vector2

Source§

type Output = Vector2

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f32) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<i32> for Vector2

Source§

type Output = Vector2

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i32) -> Self::Output

Performs the / operation. Read more
Source§

impl DivAssign<f32> for Vector2

Source§

fn div_assign(&mut self, rhs: f32)

Performs the /= operation. Read more
Source§

impl DivAssign<i32> for Vector2

Source§

fn div_assign(&mut self, rhs: i32)

Performs the /= operation. Read more
Source§

impl From<Vector2> for Vector3

Source§

fn from(value: Vector2) -> Self

Converts to this type from the input type.
Source§

impl From<Vector3> for Vector2

Source§

fn from(value: Vector3) -> Self

Converts to this type from the input type.
Source§

impl Mul<Vector2> for Quaternion

Source§

type Output = Vector2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector2) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Vector2> for f32

Source§

type Output = Vector2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector2) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Vector2> for i32

Source§

type Output = Vector2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector2) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<f32> for Vector2

Source§

type Output = Vector2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f32) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<i32> for Vector2

Source§

type Output = Vector2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i32) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for Vector2

Source§

type Output = f32

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector2) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<f32> for Vector2

Source§

fn mul_assign(&mut self, rhs: f32)

Performs the *= operation. Read more
Source§

impl MulAssign<i32> for Vector2

Source§

fn mul_assign(&mut self, rhs: i32)

Performs the *= operation. Read more
Source§

impl Neg for Vector2

Source§

type Output = Vector2

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl PartialEq for Vector2

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub<Vector2> for Vector3

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Vector2) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<Vector3> for Vector2

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub for Vector2

Source§

type Output = Vector2

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Vector2) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign<Vector2> for Vector3

Source§

fn sub_assign(&mut self, rhs: Vector2)

Performs the -= operation. Read more
Source§

impl SubAssign<Vector3> for Vector2

Source§

fn sub_assign(&mut self, rhs: Vector3)

Performs the -= operation. Read more
Source§

impl SubAssign for Vector2

Source§

fn sub_assign(&mut self, rhs: Vector2)

Performs the -= operation. Read more
Source§

impl Copy for Vector2

Source§

impl Eq for Vector2

Source§

impl Send for Vector2

Source§

impl Sync for Vector2

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.