glium_types/vectors/
uvec2.rs1use 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)]
9pub struct UVec2 {
11 pub x: u32,
12 pub y: u32,
13}
14impl UVec2 {
15 pub const ZERO: Self = uvec2(0, 0);
17 pub const ONE: Self = uvec2(1, 1);
19 pub const X: Self = uvec2(1, 0);
21 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 pub const fn splat(value: u32) -> Self { Self::new(value, value) }
29
30 pub fn length_squared(self) -> u32 {
32 self.x*self.x + self.y*self.y
33 }
34 pub fn distance_squared(self, other: UVec2) -> u32 {
36 (self - other).length_squared()
37 }
38 pub fn dot(self, other: UVec2) -> u32 {
40 self.x * other.x + self.y * other.y
41 }
42 pub fn scale(self, scalar: u32) -> UVec2 {
44 Self::new(self.x * scalar, self.y * scalar)
45 }
46 pub fn eq(self, rhs: Self) -> BVec2 { bvec2(self.x == rhs.x, self.y == rhs.y) }
48 pub fn less(self, rhs: Self) -> BVec2 { bvec2(self.x < rhs.x, self.y < rhs.y) }
50 pub fn more(self, rhs: Self) -> BVec2 { bvec2(self.x > rhs.x, self.y > rhs.y) }
52 pub fn less_or_eq(self, rhs: Self) -> BVec2 { bvec2(self.x <= rhs.x, self.y <= rhs.y) }
54 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}
90pub 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}