#[repr(C)]
pub struct Vector2<T> { pub x: T, pub y: T, }
Expand description

Utility type for manipulating 2-dimensional vectors.

Vector2 is a simple type that defines a mathematical vector with two coordinates (x and y).

It can be used to represent anything that has two dimensions: a size, a point, a velocity, etc.

The type parameter T is the type of the coordinates.

You generally don’t have to care about the generic form (Vector2<T>), the most common specializations have special type aliases:

The Vector2 type has a small and simple interface, its x and y members can be accessed directly (there are no accessors like set_x(), get_x()) and it contains no mathematical function like dot product, cross product, length, etc.

Usage example

let mut v1 = Vector2f::new(16.5, 24.0);
v1.x = 18.2;
let y = v1.y;

let v2 = v1 * 5.0;
let v3 = v1 + v2;
assert_ne!(v2, v3);

Note: for 3-dimensional vectors, see Vector3.

Fields

x: T

X coordinate of the vector.

y: T

Y coordinate of the vector.

Implementations

Creates a new vector from its coordinates.

Usage example
let v: Vector2<isize> = Vector2::new(6969, 6969);

Lossless conversion into Vector2<U>.

Usage example
let vu: Vector2<u16> = Vector2::new(6969, 6969);
let vi: Vector2<i32> = vu.into_other();
assert_eq!(vu.x, vi.x.try_into().unwrap());
assert_eq!(vu.y, vu.y.try_into().unwrap());

Fallible conversion into Vector2<U>

Usage example
// Passing case
let vi: Vector2<i32> = Vector2::new(21, 21);
let vu: Vector2<u32> = vi.try_into_other().unwrap(); // or any other Result resolution
assert_eq!(u32::try_from(vi.x).unwrap(), vu.x);
assert_eq!(u32::try_from(vi.y).unwrap(), vu.y);

// Failing case
let vi: Vector2<i32> = Vector2::new(-21, -21);
let vu = vi.try_into_other::<u32>();
assert!(vu.is_err());

Lossy conversion into Vector2<U>

Usage example
let vf: Vector2<f32> = Vector2::new(696969.6969, 6969.6969);
let vi: Vector2<i32> = vf.as_other();
assert_eq!(vf.x as i32, vi.x);
assert_eq!(vf.y as i32, vi.y);

Dot product of two 2D vectors.

Usage example
let a = Vector2i::new(16, 64);
let b = Vector2i::new(2, 4);
assert_eq!(a.dot(b), 288);

Square of vector’s length.

Usage example
let a = Vector2i::new(10, 9);
assert_eq!(a.length_sq(), 181);

Z component of the cross product of two 2D vectors.

Usage example
let a = Vector2i::new(69, 420);
let b = Vector2i::new(21, 101);
assert_eq!(a.cross(b), -1851);

Component-wise multiplication of self and rhs.

Usage example
let a = Vector2i::new(1, 1);
let b = Vector2i::new(2, 2);
assert_eq!(a.cwise_mul(b), Vector2i::new(2, 2));

Component-wise division of self and rhs. Panics on divide by zero

Usage example
let a = Vector2i::new(69, 69);
let b = Vector2i::new(3, 3);
assert_eq!(a.cwise_div(b), Vector2i::new(23, 23));

Component-wise checked division of self and rhs. Returns None on divide by zero

Usage example
// Passing case
let a = Vector2i::new(69, 69);
let b = Vector2i::new(3, 3);
assert_eq!(a.cwise_checked_div(b), Some(Vector2i::new(23, 23)));

// Failing case
let b = Vector2i::new(0, 3);
assert_eq!(a.cwise_checked_div(b), None);

Returns a perpendicular vector rotated +90 degrees

Usage example
let a = Vector2i::new(21, -21);
assert_eq!(a.perpendicular(), Vector2i::new(21, 21));

checked_div for scalar division

Usage Example
// Passing case
let a = Vector2i::new(420, 69);
assert_eq!(a.checked_div(1000), Some(Vector2i::new(0, 0)));

// Failing case
assert_eq!(a.checked_div(0), None);

Trait Implementations

Performs component wise addition

Usage Example
let a = Vector2i::new(9, 10);
let b = Vector2i::new(10, 9);
assert_ne!(a + b, Vector2i::new(21, 21));
The resulting type after applying the + operator.

Performs component wise addition assignment

Usage Example
let mut a = Vector2i::new(9, 10);
let b = Vector2i::new(10, 9);
a += b;
assert_eq!(a, Vector2i::new(19, 19));
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Performs scalar division

Usage Example
let a = Vector2i::new(9, 10);
assert_eq!(a / 3, Vector2i::new(3, 3));
The resulting type after applying the / operator.

Performs scalar division assignment

Usage Example
let mut a = Vector2i::new(9, 10);
a /= 3;
assert_eq!(a, Vector2i::new(3, 3));

Constructs a Vector2 from (x, y).

Usage example
let a: Vector2<u16> = Vector2::from((69u16, 420u16));
assert_eq!(a.x, 69u16);
assert_eq!(a.y, 420u16);

Performs scalar multiplication

Usage Example
let a = Vector2i::new(9, 10);
assert_eq!(a * 420, Vector2i::new(3780, 4200));
The resulting type after applying the * operator.

Performs scalar multiplication assignment

Usage Example
let mut a = Vector2i::new(9, 10);
a *= 420;
assert_eq!(a, Vector2i::new(3780, 4200));

Negates the vector

Usage Example
use std::ops::Neg;
let a = Vector2i::new(21, 21);
assert_eq!(a.neg(), Vector2i::new(-21, -21));
The resulting type after applying the - operator.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Performs component wise subtraction

Usage Example
let a = Vector2i::new(9, 10);
let b = Vector2i::new(10, 9);
assert_eq!(a - b, Vector2i::new(-1, 1));
The resulting type after applying the - operator.

Performs component wise subtraction assignment

Usage Example
let mut a = Vector2i::new(9, 10);
let b = Vector2i::new(10, 9);
a -= b;
assert_eq!(a, Vector2i::new(-1, 1));

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.