1use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
6pub struct Vec2 {
7 pub x: f32,
8 pub y: f32,
9}
10
11impl Vec2 {
12 pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
13
14 pub const fn new(x: f32, y: f32) -> Self {
15 Self { x, y }
16 }
17
18 pub fn plus(self, o: Self) -> Self {
19 Self::new(self.x + o.x, self.y + o.y)
20 }
21
22 pub fn minus(self, o: Self) -> Self {
23 Self::new(self.x - o.x, self.y - o.y)
24 }
25
26 pub fn scale(self, k: f32) -> Self {
27 Self::new(self.x * k, self.y * k)
28 }
29
30 pub fn lerp(self, o: Self, t: f32) -> Self {
31 Self::new(self.x + (o.x - self.x) * t, self.y + (o.y - self.y) * t)
32 }
33
34 pub fn length(self) -> f32 {
35 self.x.hypot(self.y)
36 }
37
38 pub fn distance(self, o: Self) -> f32 {
39 self.minus(o).length()
40 }
41
42 pub fn snap(self, grid: f32) -> Self {
44 if grid <= 0.0 {
45 return self;
46 }
47 Self::new(
48 (self.x / grid).round() * grid,
49 (self.y / grid).round() * grid,
50 )
51 }
52}
53
54impl std::ops::Add for Vec2 {
55 type Output = Self;
56 fn add(self, o: Self) -> Self {
57 self.plus(o)
58 }
59}
60
61impl std::ops::Sub for Vec2 {
62 type Output = Self;
63 fn sub(self, o: Self) -> Self {
64 self.minus(o)
65 }
66}
67
68#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
69pub struct Size2 {
70 pub w: f32,
71 pub h: f32,
72}
73
74impl Size2 {
75 pub const fn new(w: f32, h: f32) -> Self {
76 Self { w, h }
77 }
78}
79
80#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
82pub struct Rect {
83 pub min: Vec2,
84 pub max: Vec2,
85}
86
87impl Rect {
88 pub fn from_origin_size(origin: Vec2, size: Size2) -> Self {
89 Self {
90 min: origin,
91 max: Vec2::new(origin.x + size.w, origin.y + size.h),
92 }
93 }
94
95 pub fn from_corners(a: Vec2, b: Vec2) -> Self {
97 Self {
98 min: Vec2::new(a.x.min(b.x), a.y.min(b.y)),
99 max: Vec2::new(a.x.max(b.x), a.y.max(b.y)),
100 }
101 }
102
103 pub fn from_points(points: &[Vec2]) -> Option<Self> {
104 let first = *points.first()?;
105 Some(points.iter().skip(1).fold(
106 Self {
107 min: first,
108 max: first,
109 },
110 |r, p| r.union(Self { min: *p, max: *p }),
111 ))
112 }
113
114 pub fn width(&self) -> f32 {
115 self.max.x - self.min.x
116 }
117
118 pub fn height(&self) -> f32 {
119 self.max.y - self.min.y
120 }
121
122 pub fn center(&self) -> Vec2 {
123 self.min.lerp(self.max, 0.5)
124 }
125
126 pub fn contains(&self, p: Vec2) -> bool {
127 p.x >= self.min.x && p.x <= self.max.x && p.y >= self.min.y && p.y <= self.max.y
128 }
129
130 pub fn contains_rect(&self, o: &Rect) -> bool {
131 self.contains(o.min) && self.contains(o.max)
132 }
133
134 pub fn intersects(&self, o: &Rect) -> bool {
135 self.min.x <= o.max.x
136 && self.max.x >= o.min.x
137 && self.min.y <= o.max.y
138 && self.max.y >= o.min.y
139 }
140
141 pub fn union(&self, o: Rect) -> Rect {
142 Rect {
143 min: Vec2::new(self.min.x.min(o.min.x), self.min.y.min(o.min.y)),
144 max: Vec2::new(self.max.x.max(o.max.x), self.max.y.max(o.max.y)),
145 }
146 }
147
148 pub fn inflate(&self, d: f32) -> Rect {
149 Rect {
150 min: self.min.minus(Vec2::new(d, d)),
151 max: self.max.plus(Vec2::new(d, d)),
152 }
153 }
154}
155
156pub fn distance_to_segment(p: Vec2, a: Vec2, b: Vec2) -> f32 {
158 let ab = b.minus(a);
159 let len2 = ab.x * ab.x + ab.y * ab.y;
160 if len2 <= f32::EPSILON {
161 return p.distance(a);
162 }
163 let t = (((p.x - a.x) * ab.x + (p.y - a.y) * ab.y) / len2).clamp(0.0, 1.0);
164 p.distance(a.lerp(b, t))
165}
166
167#[cfg(test)]
168mod tests {
169 use super::*;
170
171 #[test]
172 fn rect_from_corners_normalises() {
173 let r = Rect::from_corners(Vec2::new(10.0, 10.0), Vec2::new(0.0, 5.0));
174 assert_eq!(r.min, Vec2::new(0.0, 5.0));
175 assert_eq!(r.max, Vec2::new(10.0, 10.0));
176 }
177
178 #[test]
179 fn snap_rounds_to_grid() {
180 assert_eq!(Vec2::new(14.0, 26.0).snap(20.0), Vec2::new(20.0, 20.0));
181 assert_eq!(Vec2::new(3.3, 4.4).snap(0.0), Vec2::new(3.3, 4.4));
182 }
183
184 #[test]
185 fn segment_distance() {
186 let d = distance_to_segment(Vec2::new(5.0, 3.0), Vec2::ZERO, Vec2::new(10.0, 0.0));
187 assert!((d - 3.0).abs() < 1e-5);
188 let d = distance_to_segment(Vec2::new(-4.0, 3.0), Vec2::ZERO, Vec2::new(10.0, 0.0));
189 assert!((d - 5.0).abs() < 1e-5);
190 }
191}