glium_types/vectors/
ivec2.rs

1use derive_cmp_ops::CmpOps;
2use glium::uniforms::AsUniformValue;
3
4use super::{ivec3::*, vec2::Vec2, bvec2::*};
5#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, CmpOps)]
6/// an interger vector made from a x and y coordinate.
7pub struct IVec2 {
8    pub x: i32,
9    pub y: i32,
10}
11impl IVec2 {
12    /// a zero vector
13    pub const ZERO: Self = ivec2(0, 0);
14    /// a vector full of ones
15    pub const ONE: Self = ivec2(1, 1);
16    /// the x axis
17    pub const X: Self = ivec2(1, 0);
18    /// the y axis
19    pub const Y: Self = ivec2(0, 1);
20
21    pub const fn new(x: i32, y: i32) -> Self { Self { x, y } }
22    pub const fn extend(self, z: i32) -> IVec3 { ivec3(self.x, self.y, z) }
23    pub const fn truncate(self) -> i32 { self.x }
24
25    /// create a vector where x and y equals `value`
26    pub fn splat(value: i32) -> Self {
27        Self::new(value, value)
28    }
29    /// the length of the vector without being square rooted
30    pub fn length_squared(self) -> i32 {
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: IVec2) -> i32 {
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: IVec2) -> i32 {
39        self.x * other.x + self.y * other.y
40    }
41    /// multiplies each value by the scalar
42    pub fn scale(self, scalar: i32) -> IVec2 {
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<IVec2> for i32 {
57    fn mul(self, rhs: IVec2) -> Self::Output { rhs * self }
58    type Output = IVec2;
59}
60impl std::ops::Mul<i32> for IVec2 {
61    fn mul(self, rhs: i32) -> Self::Output { self.scale(rhs) }
62    type Output = Self;
63}
64impl std::ops::MulAssign<i32> for IVec2 { fn mul_assign(&mut self, rhs: i32) { *self = *self * rhs } }
65impl std::ops::Div<IVec2> for i32 {
66    fn div(self, rhs: IVec2) -> Self::Output { IVec2::splat(self) / rhs }
67    type Output = IVec2;
68}
69impl std::ops::Div<i32> for IVec2 {
70    fn div(self, rhs: i32) -> Self::Output { self / IVec2::splat(rhs) }
71    type Output = Self;
72}
73impl std::ops::DivAssign<i32> for IVec2 { fn div_assign(&mut self, rhs: i32) { *self = *self / rhs } }
74impl std::ops::Rem<IVec2> for i32 {
75    fn rem(self, rhs: IVec2) -> Self::Output { IVec2::splat(self) % rhs }
76    type Output = IVec2;
77}
78impl std::ops::Rem<i32> for IVec2 {
79    fn rem(self, rhs: i32) -> Self::Output { self % IVec2::splat(rhs) }
80    type Output = Self;
81}
82impl std::ops::RemAssign<i32> for IVec2 { fn rem_assign(&mut self, rhs: i32) { *self = *self % rhs } }
83
84impl AsUniformValue for IVec2{
85    fn as_uniform_value(&self) -> glium::uniforms::UniformValue<'_> {
86        glium::uniforms::UniformValue::IntVec2([self.x, self.y])
87    }
88}
89/// create an interger vector with an x and y coordinate.
90pub const fn ivec2(x: i32, y: i32) -> IVec2 {
91    IVec2 { x, y }
92}
93impl From<Vec2> for IVec2 {
94    fn from (value: Vec2) -> Self {
95        ivec2(value.x as i32, value.y as i32)
96    }
97}
98impl From<[i32; 2]> for IVec2 {
99    fn from(value: [i32; 2]) -> Self {
100        ivec2(value[0], value[1])
101    }
102}
103impl From<(i32, i32)> for IVec2 {
104    fn from(value: (i32, i32)) -> Self {
105        ivec2(value.0, value.1)
106    }
107}
108impl From<IVec2> for [i32; 2] {
109    fn from(value: IVec2) -> Self {
110        [value.x, value.y]
111    }
112}
113impl From<IVec2> for (i32, i32) {
114    fn from(value: IVec2) -> Self {
115        (value.x, value.y)
116    }
117}