#[repr(C)]pub struct vec2<T>(pub T, pub T);
Expand description
2 dimensional vector.
Tuple Fields§
§0: T
§1: T
Implementations§
§impl<T> vec2<T>where
T: Float,
impl<T> vec2<T>where
T: Float,
pub fn normalize(self) -> vec2<T>
pub fn normalize(self) -> vec2<T>
Normalize a vector.
§Examples
let v: vec2<f64> = vec2(1.0, 2.0);
assert!((v.normalize().len() - 1.0).abs() < 1e-5);
pub fn normalize_or_zero(self) -> vec2<T>
pub fn normalize_or_zero(self) -> vec2<T>
Normalizes a vector unless its length its approximately 0. Can be used to avoid division by 0.
§Examples
let v = vec2(1.0, 2.0);
assert_eq!(v.normalize_or_zero(), v.normalize());
let v = vec2(1e-10, 1e-10);
assert_eq!(v.normalize_or_zero(), vec2::ZERO);
pub fn len(self) -> T
pub fn len(self) -> T
pub fn len_sqr(self) -> T
pub fn len_sqr(self) -> T
Calculate squared length of this vector
pub fn rotate(self, angle: Angle<T>) -> vec2<T>
pub fn rotate(self, angle: Angle<T>) -> vec2<T>
Rotate a vector by a given angle.
§Examples
let v = vec2(1.0, 2.0);
assert!((v.rotate(Angle::from_radians(std::f32::consts::FRAC_PI_2)) - vec2(-2.0, 1.0)).len() < 1e-5);
pub fn clamp_len(self, len_range: impl FixedRangeBounds<T>) -> vec2<T>
pub fn clamp_len(self, len_range: impl FixedRangeBounds<T>) -> vec2<T>
Clamp vector’s length. Note that the range must be inclusive.
§Examples
let v = vec2(1.0, 2.0);
assert_eq!(v.clamp_len(..=1.0), v.normalize());
pub fn clamp_coordinates(
self,
x_range: impl FixedRangeBounds<T>,
y_range: impl FixedRangeBounds<T>
) -> vec2<T>
pub fn clamp_coordinates( self, x_range: impl FixedRangeBounds<T>, y_range: impl FixedRangeBounds<T> ) -> vec2<T>
Clamp vector in range. Note the range must be inclusive.
§Examples
let v = vec2(1.0, 2.0);
assert_eq!(v.clamp_coordinates(.., 0.0..=1.0), vec2(1.0, 1.0));
pub fn clamp_aabb(self, aabb: Aabb2<T>) -> vec2<T>
pub fn clamp_aabb(self, aabb: Aabb2<T>) -> vec2<T>
Clamp vector by aabb
corners.
§Examples
let v = vec2(0.5, 2.0);
let min = vec2(0.0, 0.0);
let max = vec2(1.0, 1.0);
let aabb = Aabb2::from_corners(min, max);
assert_eq!(v.clamp_aabb(aabb), vec2(0.5, 1.0));
pub fn arg(self) -> Angle<T>
pub fn arg(self) -> Angle<T>
Get an angle between the positive direction of the x-axis.
§Examples
let v = vec2(0.0, 1.0);
assert_eq!(v.arg().as_radians(), std::f32::consts::FRAC_PI_2);
pub fn aspect(self) -> T
pub fn aspect(self) -> T
Calculate aspect ratio (x / y)
Methods from Deref<Target = [T; 2]>§
1.57.0 · sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a slice containing the entire array. Equivalent to &s[..]
.
1.57.0 · sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice containing the entire array. Equivalent to
&mut s[..]
.
sourcepub fn each_ref(&self) -> [&T; N]
🔬This is a nightly-only experimental API. (array_methods
)
pub fn each_ref(&self) -> [&T; N]
array_methods
)Borrows each element and returns an array of references with the same
size as self
.
§Example
#![feature(array_methods)]
let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);
This method is particularly useful if combined with other methods, like
map
. This way, you can avoid moving the original
array if its elements are not Copy
.
#![feature(array_methods)]
let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);
// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);
sourcepub fn each_mut(&mut self) -> [&mut T; N]
🔬This is a nightly-only experimental API. (array_methods
)
pub fn each_mut(&mut self) -> [&mut T; N]
array_methods
)Borrows each element mutably and returns an array of mutable references
with the same size as self
.
§Example
#![feature(array_methods)]
let mut floats = [3.1, 2.7, -1.0];
let float_refs: [&mut f64; 3] = floats.each_mut();
*float_refs[0] = 0.0;
assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]);
assert_eq!(floats, [0.0, 2.7, -1.0]);
sourcepub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
🔬This is a nightly-only experimental API. (split_array
)
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
split_array
)Divides one array reference into two at an index.
The first will contain all indices from [0, M)
(excluding
the index M
itself) and the second will contain all
indices from [M, N)
(excluding the index N
itself).
§Panics
Panics if M > N
.
§Examples
#![feature(split_array)]
let v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.split_array_ref::<0>();
assert_eq!(left, &[]);
assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
{
let (left, right) = v.split_array_ref::<2>();
assert_eq!(left, &[1, 2]);
assert_eq!(right, &[3, 4, 5, 6]);
}
{
let (left, right) = v.split_array_ref::<6>();
assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
assert_eq!(right, &[]);
}
sourcepub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
🔬This is a nightly-only experimental API. (split_array
)
pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
split_array
)Divides one mutable array reference into two at an index.
The first will contain all indices from [0, M)
(excluding
the index M
itself) and the second will contain all
indices from [M, N)
(excluding the index N
itself).
§Panics
Panics if M > N
.
§Examples
#![feature(split_array)]
let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.split_array_mut::<2>();
assert_eq!(left, &mut [1, 0][..]);
assert_eq!(right, &mut [3, 0, 5, 6]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);
sourcepub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
🔬This is a nightly-only experimental API. (split_array
)
pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
split_array
)Divides one array reference into two at an index from the end.
The first will contain all indices from [0, N - M)
(excluding
the index N - M
itself) and the second will contain all
indices from [N - M, N)
(excluding the index N
itself).
§Panics
Panics if M > N
.
§Examples
#![feature(split_array)]
let v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.rsplit_array_ref::<0>();
assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
assert_eq!(right, &[]);
}
{
let (left, right) = v.rsplit_array_ref::<2>();
assert_eq!(left, &[1, 2, 3, 4]);
assert_eq!(right, &[5, 6]);
}
{
let (left, right) = v.rsplit_array_ref::<6>();
assert_eq!(left, &[]);
assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
sourcepub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
🔬This is a nightly-only experimental API. (split_array
)
pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
split_array
)Divides one mutable array reference into two at an index from the end.
The first will contain all indices from [0, N - M)
(excluding
the index N - M
itself) and the second will contain all
indices from [N - M, N)
(excluding the index N
itself).
§Panics
Panics if M > N
.
§Examples
#![feature(split_array)]
let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.rsplit_array_mut::<4>();
assert_eq!(left, &mut [1, 0]);
assert_eq!(right, &mut [3, 0, 5, 6][..]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);
sourcepub fn as_ascii(&self) -> Option<&[AsciiChar; N]>
🔬This is a nightly-only experimental API. (ascii_char
)
pub fn as_ascii(&self) -> Option<&[AsciiChar; N]>
ascii_char
)Converts this array of bytes into a array of ASCII characters,
or returns None
if any of the characters is non-ASCII.
§Examples
#![feature(ascii_char)]
#![feature(const_option)]
const HEX_DIGITS: [std::ascii::Char; 16] =
*b"0123456789abcdef".as_ascii().unwrap();
assert_eq!(HEX_DIGITS[1].as_str(), "1");
assert_eq!(HEX_DIGITS[10].as_str(), "a");
sourcepub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]
🔬This is a nightly-only experimental API. (ascii_char
)
pub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]
ascii_char
)Converts this array of bytes into a array of ASCII characters, without checking whether they’re valid.
§Safety
Every byte in the array must be in 0..=127
, or else this is UB.
Trait Implementations§
§impl<T> AddAssign for vec2<T>where
T: AddAssign,
impl<T> AddAssign for vec2<T>where
T: AddAssign,
§fn add_assign(&mut self, rhs: vec2<T>)
fn add_assign(&mut self, rhs: vec2<T>)
+=
operation. Read more§impl<'de, T> Deserialize<'de> for vec2<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for vec2<T>where
T: Deserialize<'de>,
§fn deserialize<__D>(
__deserializer: __D
) -> Result<vec2<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D
) -> Result<vec2<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl<T> DivAssign<T> for vec2<T>
impl<T> DivAssign<T> for vec2<T>
§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/=
operation. Read more§impl<T> DivAssign for vec2<T>where
T: DivAssign,
impl<T> DivAssign for vec2<T>where
T: DivAssign,
§fn div_assign(&mut self, rhs: vec2<T>)
fn div_assign(&mut self, rhs: vec2<T>)
/=
operation. Read more§impl<T> MulAssign<T> for vec2<T>
impl<T> MulAssign<T> for vec2<T>
§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*=
operation. Read more§impl<T> MulAssign for vec2<T>where
T: MulAssign,
impl<T> MulAssign for vec2<T>where
T: MulAssign,
§fn mul_assign(&mut self, rhs: vec2<T>)
fn mul_assign(&mut self, rhs: vec2<T>)
*=
operation. Read more§impl<T> Serialize for vec2<T>where
T: Serialize,
impl<T> Serialize for vec2<T>where
T: Serialize,
§fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
§impl<T> SubAssign for vec2<T>where
T: SubAssign,
impl<T> SubAssign for vec2<T>where
T: SubAssign,
§fn sub_assign(&mut self, rhs: vec2<T>)
fn sub_assign(&mut self, rhs: vec2<T>)
-=
operation. Read moresource§impl VertexAttribute for vec2<f32>
impl VertexAttribute for vec2<f32>
impl<T> Copy for vec2<T>where
T: Copy,
impl<T> Eq for vec2<T>where
T: Eq,
impl<T> StructuralEq for vec2<T>
impl<T> StructuralPartialEq for vec2<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for vec2<T>where
T: RefUnwindSafe,
impl<T> Send for vec2<T>where
T: Send,
impl<T> Sync for vec2<T>where
T: Sync,
impl<T> Unpin for vec2<T>where
T: Unpin,
impl<T> UnwindSafe for vec2<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
§impl<T> CompatExt for T
impl<T> CompatExt for T
source§impl<T> Configurable for T
impl<T> Configurable for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.