glium_types/vectors/
divec2.rs

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