glium_types/vectors/
duvec2.rs

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