glium_types/vectors/
divec2.rs1use 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)]
6pub struct DIVec2 {
8 pub x: i64,
9 pub y: i64,
10}
11impl DIVec2{
12 pub const ZERO: Self = divec2(0, 0);
14 pub const ONE: Self = divec2(1, 1);
16 pub const X: Self = divec2(1, 0);
18 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 pub const fn splat(value: i64) -> Self { Self::new(value, value) }
28
29 pub fn length_squared(self) -> i64 {
31 self.x*self.x + self.y*self.y
32 }
33 pub fn distance_squared(self, other: DIVec2) -> i64 {
35 (self - other).length_squared()
36 }
37 pub fn dot(self, other: DIVec2) -> i64 {
39 self.x * other.x + self.y * other.y
40 }
41 pub fn scale(self, scalar: i64) -> DIVec2 {
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<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}
89pub 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}