glium_types/vectors/
ivec2.rs1use 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)]
6pub struct IVec2 {
8 pub x: i32,
9 pub y: i32,
10}
11impl IVec2 {
12 pub const ZERO: Self = ivec2(0, 0);
14 pub const ONE: Self = ivec2(1, 1);
16 pub const X: Self = ivec2(1, 0);
18 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 pub fn splat(value: i32) -> Self {
27 Self::new(value, value)
28 }
29 pub fn length_squared(self) -> i32 {
31 self.x*self.x + self.y*self.y
32 }
33 pub fn distance_squared(self, other: IVec2) -> i32 {
35 (self - other).length_squared()
36 }
37 pub fn dot(self, other: IVec2) -> i32 {
39 self.x * other.x + self.y * other.y
40 }
41 pub fn scale(self, scalar: i32) -> IVec2 {
43 Self::new(self.x * scalar, self.y * scalar)
44 }
45 pub fn eq(self, rhs: Self) -> BVec2 { bvec2(self.x == rhs.x, self.y == rhs.y) }
47 pub fn less(self, rhs: Self) -> BVec2 { bvec2(self.x < rhs.x, self.y < rhs.y) }
49 pub fn more(self, rhs: Self) -> BVec2 { bvec2(self.x > rhs.x, self.y > rhs.y) }
51 pub fn less_or_eq(self, rhs: Self) -> BVec2 { bvec2(self.x <= rhs.x, self.y <= rhs.y) }
53 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}
89pub 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}