pub struct Vector2D<T> {
pub x: T,
pub y: T,
}
Expand description
A 2D vector, containing an x
and a y
component. While many types can be
used for a Vector2D
’s components, the traits they implement determine
what functions are available.
Provided that the components implement the necessary traits, Vector2D
s
can be added to or subtracted from one-another, and they can be mulitplied
and divided by scalar values.
There are generally two options for converting between Vector2D
types. If
the internal components’ type has an implementation of Into
that targets
the desired type, then into_vec2d()
can be called from the source object,
or from_vec2d(..)
can be called and the source object can be provided.
If no Into
implementation exists, then the only option is to use one of the
flavours of casting with as
. These are in the form as_types()
, and are only
implemented for specific types of components. An example usage would look like
this:
use vector2d::Vector2D;
let f64_vector: Vector2D<f64> = Vector2D::new(10.3, 11.1);
let i32_vector: Vector2D<i32> = f64_vector.as_i32s();
assert_eq!(Vector2D::new(10, 11), i32_vector);
Implementations of as_types()
are only available when an implementation of
into_vec2d()
is unavailable. This is to seperate between the lossless casting
of primitives with into()
and from(..)
, and the lossy casting between
primitives of varying detail.
Casts from signed types to unsigned types have a small additional check that
ensures a lower bound of 0 on the signed value, to reduce the chances of
experiencing undefined behaviour. This means that a Vector2D<f64>
with a
value of (-10.3, 11.1)
would become (0, 11)
when cast to a Vector2D<u32>
with as_u32s()
.
The current list of interoperable types that can be cast with the as
family of
functions is as follows:
i32
i64
,isize
u32
u64
usize
f32
f64
Fields§
§x: T
§y: T
Implementations§
Source§impl<T: Copy + Clone> Vector2D<T>
impl<T: Copy + Clone> Vector2D<T>
Sourcepub fn from_vec2d<U: Into<T> + Copy + Clone>(src: Vector2D<U>) -> Vector2D<T>
pub fn from_vec2d<U: Into<T> + Copy + Clone>(src: Vector2D<U>) -> Vector2D<T>
Convert a Vector2
of type U
to one of type T
. Available only when
type T has implemented From<U>
.
§Example
use vector2d::Vector2D;
let i32_vector: Vector2D<i32> = Vector2D::new(25, 8);
let f64_vector: Vector2D<f64> = Vector2D::from_vec2d(i32_vector);
assert_eq!(Vector2D::new(25.0, 8.0), f64_vector);
Sourcepub fn into_vec2d<U: From<T>>(self) -> Vector2D<U>
pub fn into_vec2d<U: From<T>>(self) -> Vector2D<U>
Convert a Vector2
of type T
to one of type U
. Available only when
type T has implemented Into<U>
.
§Example
use vector2d::Vector2D;
let i32_vector: Vector2D<i32> = Vector2D::new(25, 8);
let i32_vector: Vector2D<i32> = Vector2D::new(25, 8);
let f64_vector: Vector2D<f64> = i32_vector.into_vec2d();
assert_eq!(Vector2D::new(25.0, 8.0), f64_vector);
Source§impl<T: Default> Vector2D<T>
impl<T: Default> Vector2D<T>
Sourcepub fn horizontal(self) -> Self
pub fn horizontal(self) -> Self
Returns a vector with only the horizontal component of the current one
§Example
use vector2d::Vector2D;
let v = Vector2D::new(10, 20);
assert_eq!(Vector2D::new(10, 0), v.horizontal());
Source§impl<T> Vector2D<T>
impl<T> Vector2D<T>
Sourcepub fn mul_components(self, other: Self) -> Self
pub fn mul_components(self, other: Self) -> Self
Returns a new vector with components equal to each of the current vector’s components multiplied by the corresponding component of the provided vector
§Example
use vector2d::Vector2D;
let v1 = Vector2D::new(11.0, -2.5);
let v2 = Vector2D::new(0.5, -2.0);
assert_eq!(Vector2D::new(5.5, 5.0), v1.mul_components(v2));
Source§impl<T> Vector2D<T>
impl<T> Vector2D<T>
Sourcepub fn div_components(self, other: Self) -> Self
pub fn div_components(self, other: Self) -> Self
Returns a new vector with components equal to each of the current vector’s components divided by the corresponding component of the provided vector
§Example
use vector2d::Vector2D;
let v1 = Vector2D::new(11.0, -2.5);
let v2 = Vector2D::new(0.5, -2.0);
assert_eq!(Vector2D::new(22.0, 1.25), v1.div_components(v2));
Source§impl<T, U, V> Vector2D<T>
impl<T, U, V> Vector2D<T>
Sourcepub fn length_squared(self) -> V
pub fn length_squared(self) -> V
Get the squared length of a Vector2D
. This is more performant than using
length()
– which is only available for Vector2D<f32>
and Vector2D<f64>
– as it does not perform any square root operation.
Source§impl Vector2D<f32>
impl Vector2D<f32>
Sourcepub fn length(self) -> f32
pub fn length(self) -> f32
Get the length of the vector. If possible, favour length_squared()
over
this function, as it is more performant.
Sourcepub fn normalise(self) -> Self
pub fn normalise(self) -> Self
Get a new vector with the same direction as this vector, but with a length of 1.0. If the the length of the vector is 0, then the original vector is returned.
pub fn as_i32s(&self) -> Vector2D<i32>
pub fn as_i64s(&self) -> Vector2D<i64>
pub fn as_isizes(&self) -> Vector2D<isize>
pub fn as_u32s(&self) -> Vector2D<u32>
pub fn as_u64s(&self) -> Vector2D<u64>
pub fn as_usizes(&self) -> Vector2D<usize>
Source§impl Vector2D<f64>
impl Vector2D<f64>
Sourcepub fn length(self) -> f64
pub fn length(self) -> f64
Get the length of the vector. If possible, favour length_squared()
over
this function, as it is more performant.
Sourcepub fn normalise(self) -> Self
pub fn normalise(self) -> Self
Get a new vector with the same direction as this vector, but with a length of 1.0. If the the length of the vector is 0, then the original vector is returned.
pub fn as_i32s(&self) -> Vector2D<i32>
pub fn as_i64s(&self) -> Vector2D<i64>
pub fn as_isizes(&self) -> Vector2D<isize>
pub fn as_f32s(&self) -> Vector2D<f32>
pub fn as_u32s(&self) -> Vector2D<u32>
pub fn as_u64s(&self) -> Vector2D<u64>
pub fn as_usizes(&self) -> Vector2D<usize>
Source§impl Vector2D<i64>
impl Vector2D<i64>
pub fn as_i32s(&self) -> Vector2D<i32>
pub fn as_isizes(&self) -> Vector2D<isize>
pub fn as_f32s(&self) -> Vector2D<f32>
pub fn as_f64s(&self) -> Vector2D<f64>
pub fn as_u32s(&self) -> Vector2D<u32>
pub fn as_u64s(&self) -> Vector2D<u64>
pub fn as_usizes(&self) -> Vector2D<usize>
Source§impl Vector2D<isize>
impl Vector2D<isize>
pub fn as_i32s(&self) -> Vector2D<i32>
pub fn as_i64s(&self) -> Vector2D<i64>
pub fn as_f32s(&self) -> Vector2D<f32>
pub fn as_f64s(&self) -> Vector2D<f64>
pub fn as_u32s(&self) -> Vector2D<u32>
pub fn as_u64s(&self) -> Vector2D<u64>
pub fn as_usizes(&self) -> Vector2D<usize>
Source§impl Vector2D<u64>
impl Vector2D<u64>
pub fn as_i32s(&self) -> Vector2D<i32>
pub fn as_i64s(&self) -> Vector2D<i64>
pub fn as_isizes(&self) -> Vector2D<isize>
pub fn as_f32s(&self) -> Vector2D<f32>
pub fn as_f64s(&self) -> Vector2D<f64>
pub fn as_u32s(&self) -> Vector2D<u32>
pub fn as_usizes(&self) -> Vector2D<usize>
Source§impl Vector2D<usize>
impl Vector2D<usize>
pub fn as_i32s(&self) -> Vector2D<i32>
pub fn as_i64s(&self) -> Vector2D<i64>
pub fn as_isizes(&self) -> Vector2D<isize>
pub fn as_f32s(&self) -> Vector2D<f32>
pub fn as_f64s(&self) -> Vector2D<f64>
pub fn as_u32s(&self) -> Vector2D<u32>
pub fn as_u64s(&self) -> Vector2D<u64>
Trait Implementations§
Source§impl<T> AddAssign for Vector2D<T>
impl<T> AddAssign for Vector2D<T>
Source§fn add_assign(&mut self, rhs: Vector2D<T>)
fn add_assign(&mut self, rhs: Vector2D<T>)
+=
operation. Read moreSource§impl<T> DivAssign<T> for Vector2D<T>
impl<T> DivAssign<T> for Vector2D<T>
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/=
operation. Read moreSource§impl<T> MulAssign<T> for Vector2D<T>
impl<T> MulAssign<T> for Vector2D<T>
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*=
operation. Read moreSource§impl<T> SubAssign for Vector2D<T>
impl<T> SubAssign for Vector2D<T>
Source§fn sub_assign(&mut self, rhs: Vector2D<T>)
fn sub_assign(&mut self, rhs: Vector2D<T>)
-=
operation. Read moreimpl<T: Copy> Copy for Vector2D<T>
impl<T: Eq> Eq for Vector2D<T>
impl<T> StructuralPartialEq for Vector2D<T>
Auto Trait Implementations§
impl<T> Freeze for Vector2D<T>where
T: Freeze,
impl<T> RefUnwindSafe for Vector2D<T>where
T: RefUnwindSafe,
impl<T> Send for Vector2D<T>where
T: Send,
impl<T> Sync for Vector2D<T>where
T: Sync,
impl<T> Unpin for Vector2D<T>where
T: Unpin,
impl<T> UnwindSafe for Vector2D<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)