glium_types/vectors/
vec2.rs1use derive_cmp_ops::CmpOps;
2use glium::uniforms::AsUniformValue;
3use crate::matrices::Mat2;
4
5use super::{vec3::{vec3, Vec3}, bvec2::*};
6#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, CmpOps)]
7pub struct Vec2 {
9 pub x: f32,
10 pub y: f32,
11}
12impl Vec2{
13 pub const ZERO: Self = vec2(0.0, 0.0);
15 pub const ONE: Self = vec2(1.0, 1.0);
17 pub const X: Self = vec2(1.0, 0.0);
19 pub const Y: Self = vec2(0.0, 1.0);
21
22 pub const fn new(x: f32, y: f32) -> Self{ Self { x, y } }
23 pub const fn extend(self, z: f32) -> Vec3{ vec3(self.x, self.y, z) }
24 pub const fn truncate(self) -> f32 { self.x }
25 pub const fn splat(value: f32) -> Self { Self::new(value, value) }
27
28 pub fn length_squared(self) -> f32{
30 self.x*self.x + self.y*self.y
31 }
32 pub fn length(self) -> f32{
34 self.length_squared().sqrt()
35 }
36 pub fn distance_squared(self, other: Vec2) -> f32{
38 (self - other).length_squared()
39 }
40 pub fn distance(self, other: Vec2) -> f32{
42 (self - other).length()
43 }
44 pub fn dot(self, other: Vec2) -> f32{
46 self.x * other.x + self.y * other.y
47 }
48 pub fn scale(self, scalar: f32) -> Vec2{
50 Self::new(self.x * scalar, self.y * scalar)
51 }
52 pub fn normalise(self) -> Self{
54 let length = self.length();
55 if length == 0.0 { return vec2(0.0, 0.0); }
56 self.scale(1.0 / length)
57 }
58 pub fn transform(self, matrix: Mat2) -> Vec2{
60 let a: Vec2 = matrix.row(0).into();
61 let b: Vec2 = matrix.row(1).into();
62 vec2(a.dot(self), b.dot(self))
63 }
64 pub fn eq(self, rhs: Self) -> BVec2 { bvec2(self.x == rhs.x, self.y == rhs.y) }
66 pub fn less(self, rhs: Self) -> BVec2 { bvec2(self.x < rhs.x, self.y < rhs.y) }
68 pub fn more(self, rhs: Self) -> BVec2 { bvec2(self.x > rhs.x, self.y > rhs.y) }
70 pub fn less_or_eq(self, rhs: Self) -> BVec2 { bvec2(self.x <= rhs.x, self.y <= rhs.y) }
72 pub fn more_or_eq(self, rhs: Self) -> BVec2 { bvec2(self.x >= rhs.x, self.y >= rhs.y) }
74}
75impl std::ops::Mul<Vec2> for f32 {
76 fn mul(self, rhs: Vec2) -> Self::Output { rhs * self }
77 type Output = Vec2;
78}
79impl std::ops::Mul<f32> for Vec2 {
80 fn mul(self, rhs: f32) -> Self::Output { self.scale(rhs) }
81 type Output = Self;
82}
83impl std::ops::MulAssign<f32> for Vec2 { fn mul_assign(&mut self, rhs: f32) { *self = *self * rhs } }
84impl std::ops::Div<Vec2> for f32 {
85 fn div(self, rhs: Vec2) -> Self::Output { Vec2::splat(self) / rhs }
86 type Output = Vec2;
87}
88impl std::ops::Div<f32> for Vec2 {
89 fn div(self, rhs: f32) -> Self::Output { self.scale(1.0 / rhs) }
90 type Output = Self;
91}
92impl std::ops::DivAssign<f32> for Vec2 { fn div_assign(&mut self, rhs: f32) { *self = *self / rhs } }
93impl std::ops::Rem<Vec2> for f32 {
94 fn rem(self, rhs: Vec2) -> Self::Output { Vec2::splat(self) % rhs }
95 type Output = Vec2;
96}
97impl std::ops::Rem<f32> for Vec2 {
98 fn rem(self, rhs: f32) -> Self::Output { self % Vec2::splat(rhs) }
99 type Output = Self;
100}
101impl std::ops::RemAssign<f32> for Vec2 { fn rem_assign(&mut self, rhs: f32) { *self = *self % rhs } }
102
103impl AsUniformValue for Vec2{
104 fn as_uniform_value(&self) -> glium::uniforms::UniformValue<'_> {
105 glium::uniforms::UniformValue::Vec2([self.x, self.y])
106 }
107}
108pub const fn vec2(x: f32, y: f32) -> Vec2 {
110 Vec2 { x, y }
111}
112impl From<[f32; 2]> for Vec2 {
113 fn from(value: [f32; 2]) -> Self {
114 vec2(value[0], value[1])
115 }
116}
117impl From<(f32, f32)> for Vec2 {
118 fn from(value: (f32, f32)) -> Self {
119 vec2(value.0, value.1)
120 }
121}
122impl From<Vec2> for [f32; 2] {
123 fn from(value: Vec2) -> Self {
124 [value.x, value.y]
125 }
126}
127impl From<Vec2> for (f32, f32) {
128 fn from(value: Vec2) -> Self {
129 (value.x, value.y)
130 }
131}