glium_types/vectors/
uvec2.rs

1use derive_cmp_ops::*;
2use glium::uniforms::AsUniformValue;
3
4use super::{uvec3::*, vec2::Vec2, bvec2::*};
5#[derive(
6    Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, CmpAdd, CmpAddAssign, CmpDiv,
7    CmpDivAssign, CmpMul, CmpMulAssign, CmpRem, CmpRemAssign, CmpSub, CmpSubAssign
8)]
9/// an unsigned interger vector made from a x and y coordinate.
10pub struct UVec2 {
11    pub x: u32,
12    pub y: u32,
13}
14impl UVec2 {
15    ///a zero vector
16    pub const ZERO: Self = uvec2(0, 0);
17    ///a vector full of ones
18    pub const ONE: Self = uvec2(1, 1);
19    ///the x axis
20    pub const X: Self = uvec2(1, 0);
21    ///the y axis
22    pub const Y: Self = uvec2(0, 1);
23
24    pub const fn new(x: u32, y: u32) -> Self { Self { x, y } }
25    pub const fn extend(self, z: u32) -> UVec3 { uvec3(self.x, self.y, z) }
26    pub const fn truncate(self) -> u32 { self.x }
27    /// create a vector where x and y equals `value`.
28    pub const fn splat(value: u32) -> Self { Self::new(value, value) }
29
30    /// the length of the vector without being square rooted.
31    pub fn length_squared(self) -> u32 {
32        self.x*self.x + self.y*self.y
33    }
34    /// distance between two vectors without being square rooted.
35    pub fn distance_squared(self, other: UVec2) -> u32 {
36        (self - other).length_squared()
37    }
38    /// get the dot product of 2 vectors. equal to the cosign of the angle between vectors.
39    pub fn dot(self, other: UVec2) -> u32 {
40        self.x * other.x + self.y * other.y
41    }
42    /// multiplies each value by the scalar.
43    pub fn scale(self, scalar: u32) -> UVec2 {
44        Self::new(self.x * scalar, self.y * scalar)
45    }
46    /// returns whether the 2 components are equal
47    pub fn eq(self, rhs: Self) -> BVec2 { bvec2(self.x == rhs.x, self.y == rhs.y) }
48    /// returns whether the 1st components are less than the 2nd
49    pub fn less(self, rhs: Self) -> BVec2 { bvec2(self.x < rhs.x, self.y < rhs.y) }
50    /// returns whether the 1st components are more than the 2nd
51    pub fn more(self, rhs: Self) -> BVec2 { bvec2(self.x > rhs.x, self.y > rhs.y) }
52    /// returns whether the 1st components are less than or equal to the 2nd
53    pub fn less_or_eq(self, rhs: Self) -> BVec2 { bvec2(self.x <= rhs.x, self.y <= rhs.y) }
54    /// returns whether the 1st components are more than or equal to the 2nd
55    pub fn more_or_eq(self, rhs: Self) -> BVec2 { bvec2(self.x >= rhs.x, self.y >= rhs.y) } 
56}
57impl std::ops::Mul<UVec2> for u32 {
58    fn mul(self, rhs: UVec2) -> Self::Output { rhs * self }
59    type Output = UVec2;
60}
61impl std::ops::Mul<u32> for UVec2 {
62    fn mul(self, rhs: u32) -> Self::Output { self.scale(rhs) }
63    type Output = Self;
64}
65impl std::ops::MulAssign<u32> for UVec2 { fn mul_assign(&mut self, rhs: u32) { *self = *self * rhs } }
66impl std::ops::Div<UVec2> for u32 {
67    fn div(self, rhs: UVec2) -> Self::Output { UVec2::splat(self) / rhs }
68    type Output = UVec2;
69}
70impl std::ops::Div<u32> for UVec2 {
71    fn div(self, rhs: u32) -> Self::Output { self / UVec2::splat(rhs) }
72    type Output = Self;
73}
74impl std::ops::DivAssign<u32> for UVec2 { fn div_assign(&mut self, rhs: u32) { *self = *self / rhs } }
75impl std::ops::Rem<UVec2> for u32 {
76    fn rem(self, rhs: UVec2) -> Self::Output { UVec2::splat(self) % rhs }
77    type Output = UVec2;
78}
79impl std::ops::Rem<u32> for UVec2 {
80    fn rem(self, rhs: u32) -> Self::Output { self % UVec2::splat(rhs) }
81    type Output = Self;
82}
83impl std::ops::RemAssign<u32> for UVec2 { fn rem_assign(&mut self, rhs: u32) { *self = *self % rhs } }
84
85impl AsUniformValue for UVec2{
86    fn as_uniform_value(&self) -> glium::uniforms::UniformValue<'_> {
87        glium::uniforms::UniformValue::UnsignedIntVec2([self.x, self.y])
88    }
89}
90///create an unsigned interger vector with an x and y coordinate.
91pub const fn uvec2(x: u32, y: u32) -> UVec2{
92    UVec2 { x, y }
93}
94impl From<Vec2> for UVec2 {
95    fn from (value: Vec2) -> Self {
96        uvec2(value.x as u32, value.y as u32)
97    }
98}
99impl From<[u32; 2]> for UVec2 {
100    fn from(value: [u32; 2]) -> Self {
101        uvec2(value[0], value[1])
102    }
103}
104impl From<(u32, u32)> for UVec2 {
105    fn from(value: (u32, u32)) -> Self {
106        uvec2(value.0, value.1)
107    }
108}
109impl From<UVec2> for [u32; 2] {
110    fn from(value: UVec2) -> Self {
111        [value.x, value.y]
112    }
113}
114impl From<UVec2> for (u32, u32) {
115    fn from(value: UVec2) -> Self {
116        (value.x, value.y)
117    }
118}