author | unC0Rr |
Thu, 19 Dec 2024 14:18:55 +0100 | |
branch | transitional_engine |
changeset 16081 | 6633961698ad |
parent 16068 | a236360669cc |
permissions | -rw-r--r-- |
15235 | 1 |
use fpnum::{fp, integral_sqrt, FPNum, FPPoint}; |
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
2 |
use std::{ |
14161 | 3 |
cmp::{max, min}, |
15046
dc4a12a84c92
remove RangeContains in favor of standard contains
alfadur
parents:
14747
diff
changeset
|
4 |
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, RangeInclusive, Sub, SubAssign}, |
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
5 |
}; |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
6 |
|
16068 | 7 |
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)] |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
8 |
pub struct Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
9 |
pub x: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
10 |
pub y: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
11 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
12 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
13 |
impl Point { |
14648 | 14 |
pub const ZERO: Self = Self::new(0, 0); |
15 |
||
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
16 |
#[inline] |
14648 | 17 |
pub const fn new(x: i32, y: i32) -> Self { |
13962 | 18 |
Self { x, y } |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
19 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
20 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
21 |
#[inline] |
14648 | 22 |
pub const fn diag(v: i32) -> Self { |
14152 | 23 |
Self::new(v, v) |
24 |
} |
|
25 |
||
26 |
#[inline] |
|
15773
a4558e2be08c
add more const qualifiers to maintain a semblance of activity
alfadur
parents:
15405
diff
changeset
|
27 |
pub const fn signum(self) -> Self { |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
28 |
Self::new(self.x.signum(), self.y.signum()) |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
29 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
30 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
31 |
#[inline] |
15773
a4558e2be08c
add more const qualifiers to maintain a semblance of activity
alfadur
parents:
15405
diff
changeset
|
32 |
pub const fn abs(self) -> Self { |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
33 |
Self::new(self.x.abs(), self.y.abs()) |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
34 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
35 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
36 |
#[inline] |
14648 | 37 |
pub const fn dot(self, other: Point) -> i32 { |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
38 |
self.x * other.x + self.y * other.y |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
39 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
40 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
41 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
42 |
pub fn max_norm(self) -> i32 { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
43 |
std::cmp::max(self.x.abs(), self.y.abs()) |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
44 |
} |
14052 | 45 |
|
46 |
#[inline] |
|
14102 | 47 |
pub fn integral_norm(self) -> u32 { |
15405 | 48 |
let sqr = (self.x as u64).wrapping_pow(2) + (self.y as u64).wrapping_pow(2); |
15235 | 49 |
integral_sqrt(sqr) as u32 |
14102 | 50 |
} |
51 |
||
52 |
#[inline] |
|
14648 | 53 |
pub const fn transform(self, matrix: &[i32; 4]) -> Self { |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
54 |
Point::new( |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
55 |
matrix[0] * self.x + matrix[1] * self.y, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
56 |
matrix[2] * self.x + matrix[3] * self.y, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
57 |
) |
14052 | 58 |
} |
14127 | 59 |
|
60 |
#[inline] |
|
14648 | 61 |
pub const fn rotate90(self) -> Self { |
16010 | 62 |
Self::new(self.y, -self.x) |
14127 | 63 |
} |
16061 | 64 |
#[inline] |
65 |
pub const fn rotate180(self) -> Self { |
|
66 |
Self::new(-self.x, -self.y) |
|
67 |
} |
|
68 |
#[inline] |
|
69 |
pub const fn rotate270(self) -> Self { |
|
70 |
Self::new(-self.y, self.x) |
|
71 |
} |
|
14129 | 72 |
|
73 |
#[inline] |
|
14648 | 74 |
pub const fn cross(self, other: Point) -> i32 { |
14129 | 75 |
self.dot(other.rotate90()) |
76 |
} |
|
14146 | 77 |
|
78 |
#[inline] |
|
16010 | 79 |
pub fn clamp(self, rect: &Rect) -> Self { |
80 |
Self::new(rect.x_range().clamp(self.x), rect.y_range().clamp(self.y)) |
|
14152 | 81 |
} |
82 |
||
83 |
#[inline] |
|
14648 | 84 |
pub const fn line_to(self, end: Point) -> Line { |
14152 | 85 |
Line::new(self, end) |
86 |
} |
|
14146 | 87 |
|
14152 | 88 |
#[inline] |
14648 | 89 |
pub const fn ray_with_dir(self, direction: Point) -> Ray { |
14162 | 90 |
Ray::new(self, direction) |
14152 | 91 |
} |
92 |
||
93 |
#[inline] |
|
14648 | 94 |
pub const fn tangent_mul(self, x: i32) -> i32 { |
14163 | 95 |
x * self.y / self.x |
14152 | 96 |
} |
97 |
||
98 |
#[inline] |
|
14648 | 99 |
pub const fn cotangent_mul(self, y: i32) -> i32 { |
14163 | 100 |
y * self.x / self.y |
14146 | 101 |
} |
14160 | 102 |
|
103 |
#[inline] |
|
14165 | 104 |
pub fn to_fppoint(self) -> FPPoint { |
14160 | 105 |
FPPoint::new(self.x.into(), self.y.into()) |
106 |
} |
|
14165 | 107 |
|
108 |
#[inline] |
|
109 |
pub fn from_fppoint(p: &FPPoint) -> Self { |
|
16081
6633961698ad
Make wider range of numbers representable with FPNum on the cost of smaller precision
unC0Rr
parents:
16068
diff
changeset
|
110 |
Self::new(p.x().round() as i32, p.y().round() as i32) |
14165 | 111 |
} |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
112 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
113 |
|
14053 | 114 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
115 |
pub struct Size { |
|
116 |
pub width: usize, |
|
117 |
pub height: usize, |
|
118 |
} |
|
119 |
||
120 |
impl Size { |
|
14738 | 121 |
pub const EMPTY: Self = Self::square(0); |
122 |
||
14053 | 123 |
#[inline] |
14648 | 124 |
pub const fn new(width: usize, height: usize) -> Self { |
125 |
Self { width, height } |
|
14053 | 126 |
} |
127 |
||
128 |
#[inline] |
|
14648 | 129 |
pub const fn square(size: usize) -> Self { |
130 |
Self { |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
131 |
width: size, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
132 |
height: size, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
133 |
} |
14053 | 134 |
} |
135 |
||
136 |
#[inline] |
|
14648 | 137 |
pub const fn area(&self) -> usize { |
14053 | 138 |
self.width * self.height |
139 |
} |
|
140 |
||
141 |
#[inline] |
|
14648 | 142 |
pub const fn linear_index(&self, x: usize, y: usize) -> usize { |
14053 | 143 |
y * self.width + x |
144 |
} |
|
145 |
||
146 |
#[inline] |
|
15773
a4558e2be08c
add more const qualifiers to maintain a semblance of activity
alfadur
parents:
15405
diff
changeset
|
147 |
pub const fn is_power_of_two(&self) -> bool { |
14053 | 148 |
self.width.is_power_of_two() && self.height.is_power_of_two() |
149 |
} |
|
150 |
||
151 |
#[inline] |
|
15850 | 152 |
pub const fn as_power_of_two(&self) -> Option<PotSize> { |
153 |
PotSize::new(self.width, self.height) |
|
154 |
} |
|
155 |
||
156 |
#[inline] |
|
157 |
pub const fn next_power_of_two(&self) -> PotSize { |
|
158 |
PotSize::new_impl( |
|
159 |
self.width.next_power_of_two(), |
|
160 |
self.height.next_power_of_two(), |
|
161 |
) |
|
14073 | 162 |
} |
163 |
||
164 |
#[inline] |
|
14648 | 165 |
pub const fn transpose(&self) -> Self { |
14196 | 166 |
Self::new(self.height, self.width) |
167 |
} |
|
168 |
||
169 |
#[inline] |
|
14648 | 170 |
pub fn to_square(&self) -> Self { |
171 |
Self::square(max(self.width, self.height)) |
|
14146 | 172 |
} |
173 |
||
14738 | 174 |
#[inline] |
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15850
diff
changeset
|
175 |
pub fn is_square(&self) -> bool { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15850
diff
changeset
|
176 |
self.width == self.height |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15850
diff
changeset
|
177 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15850
diff
changeset
|
178 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15850
diff
changeset
|
179 |
#[inline] |
15773
a4558e2be08c
add more const qualifiers to maintain a semblance of activity
alfadur
parents:
15405
diff
changeset
|
180 |
pub const fn contains(&self, other: Self) -> bool { |
14738 | 181 |
self.width >= other.width && self.height >= other.height |
182 |
} |
|
15307 | 183 |
|
184 |
#[inline] |
|
185 |
pub fn join(&self, other: Self) -> Self { |
|
15850 | 186 |
Self::new(max(self.width, other.width), max(self.height, other.height)) |
187 |
} |
|
188 |
} |
|
189 |
||
190 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
|
191 |
pub struct PotSize { |
|
192 |
size: Size, |
|
193 |
} |
|
194 |
||
195 |
impl PotSize { |
|
196 |
#[inline] |
|
197 |
const fn new_impl(width: usize, height: usize) -> Self { |
|
198 |
debug_assert!(width.is_power_of_two() && height.is_power_of_two()); |
|
15307 | 199 |
Self { |
15850 | 200 |
size: Size::new(width, height), |
201 |
} |
|
202 |
} |
|
203 |
||
204 |
#[inline] |
|
205 |
pub const fn new(width: usize, height: usize) -> Option<Self> { |
|
206 |
if width.is_power_of_two() && height.is_power_of_two() { |
|
207 |
Some(Self::new_impl(width, height)) |
|
208 |
} else { |
|
209 |
None |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
pub const fn size(&self) -> Size { |
|
214 |
self.size |
|
215 |
} |
|
216 |
||
217 |
pub const fn width(&self) -> usize { |
|
218 |
self.size.width |
|
219 |
} |
|
220 |
||
221 |
pub const fn height(&self) -> usize { |
|
222 |
self.size.height |
|
223 |
} |
|
224 |
||
225 |
#[inline] |
|
226 |
pub const fn square(size: usize) -> Option<Self> { |
|
227 |
if size.is_power_of_two() { |
|
228 |
Some(Self::new_impl(size, size)) |
|
229 |
} else { |
|
230 |
None |
|
15307 | 231 |
} |
232 |
} |
|
15850 | 233 |
|
234 |
#[inline] |
|
235 |
pub const fn area(&self) -> usize { |
|
236 |
self.size.area() |
|
237 |
} |
|
238 |
||
239 |
#[inline] |
|
240 |
pub const fn linear_index(&self, x: usize, y: usize) -> usize { |
|
241 |
self.size.linear_index(x, y) |
|
242 |
} |
|
243 |
||
244 |
#[inline] |
|
245 |
pub const fn transpose(&self) -> Self { |
|
246 |
Self::new_impl(self.height(), self.width()) |
|
247 |
} |
|
248 |
||
249 |
#[inline] |
|
250 |
pub fn to_square(&self) -> Self { |
|
251 |
let size = max(self.width(), self.height()); |
|
252 |
Self::new_impl(size, size) |
|
253 |
} |
|
254 |
||
255 |
#[inline] |
|
256 |
pub const fn to_mask(&self) -> SizeMask { |
|
257 |
SizeMask::new(*self) |
|
258 |
} |
|
259 |
||
260 |
pub const fn to_grid_index(&self) -> GridIndex { |
|
261 |
GridIndex::new(*self) |
|
262 |
} |
|
263 |
||
264 |
#[inline] |
|
265 |
pub const fn contains(&self, other: Self) -> bool { |
|
266 |
self.size.contains(other.size) |
|
267 |
} |
|
268 |
||
269 |
#[inline] |
|
270 |
pub fn join(&self, other: Self) -> Self { |
|
271 |
Self::new_impl( |
|
272 |
max(self.width(), other.width()), |
|
273 |
max(self.height(), other.height()), |
|
274 |
) |
|
275 |
} |
|
14053 | 276 |
} |
277 |
||
14169 | 278 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
279 |
pub struct SizeMask { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
280 |
size: Size, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
281 |
} |
14053 | 282 |
|
283 |
impl SizeMask { |
|
284 |
#[inline] |
|
15850 | 285 |
pub const fn new(size: PotSize) -> Self { |
286 |
Self { |
|
287 |
size: Size::new(!(size.width() - 1), !(size.height() - 1)), |
|
288 |
} |
|
14053 | 289 |
} |
290 |
||
291 |
#[inline] |
|
292 |
pub fn contains_x<T: Into<usize>>(&self, x: T) -> bool { |
|
293 |
(self.size.width & x.into()) == 0 |
|
294 |
} |
|
295 |
||
296 |
#[inline] |
|
297 |
pub fn contains_y<T: Into<usize>>(&self, y: T) -> bool { |
|
298 |
(self.size.height & y.into()) == 0 |
|
299 |
} |
|
300 |
||
301 |
#[inline] |
|
302 |
pub fn contains(&self, point: Point) -> bool { |
|
303 |
self.contains_x(point.x as usize) && self.contains_y(point.y as usize) |
|
304 |
} |
|
15850 | 305 |
|
306 |
#[inline] |
|
307 |
pub const fn to_size(&self) -> PotSize { |
|
308 |
PotSize::new_impl(!self.size.width + 1, !self.size.height + 1) |
|
309 |
} |
|
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
310 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
311 |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
312 |
pub struct GridIndex { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
313 |
shift: Point, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
314 |
} |
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14075
diff
changeset
|
315 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14075
diff
changeset
|
316 |
impl GridIndex { |
15850 | 317 |
pub const fn new(size: PotSize) -> Self { |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
318 |
let shift = Point::new( |
15850 | 319 |
size.width().trailing_zeros() as i32, |
320 |
size.height().trailing_zeros() as i32, |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
321 |
); |
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14075
diff
changeset
|
322 |
Self { shift } |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14075
diff
changeset
|
323 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14075
diff
changeset
|
324 |
|
15773
a4558e2be08c
add more const qualifiers to maintain a semblance of activity
alfadur
parents:
15405
diff
changeset
|
325 |
pub const fn map(&self, position: Point) -> Point { |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
326 |
Point::new(position.x >> self.shift.x, position.y >> self.shift.y) |
14080
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14075
diff
changeset
|
327 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14075
diff
changeset
|
328 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14075
diff
changeset
|
329 |
|
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
330 |
macro_rules! bin_op_impl { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
331 |
($op: ty, $name: tt) => { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
332 |
impl $op for Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
333 |
type Output = Self; |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
334 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
335 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
336 |
fn $name(self, rhs: Self) -> Self::Output { |
13962 | 337 |
Self::new(self.x.$name(rhs.x), self.y.$name(rhs.y)) |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
338 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
339 |
} |
13962 | 340 |
}; |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
341 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
342 |
|
14109
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
343 |
macro_rules! scalar_bin_op_impl { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
344 |
($($op: tt)::+, $name: tt) => { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
345 |
impl $($op)::+<i32> for Point { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
346 |
type Output = Self; |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
347 |
|
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
348 |
#[inline] |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
349 |
fn $name(self, rhs: i32) -> Self::Output { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
350 |
Self::new(self.x.$name(rhs), self.y.$name(rhs)) |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
351 |
} |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
352 |
} |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
353 |
}; |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
354 |
} |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
355 |
|
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
356 |
macro_rules! bin_assign_op_impl { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
357 |
($op: ty, $name: tt) => { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
358 |
impl $op for Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
359 |
#[inline] |
13962 | 360 |
fn $name(&mut self, rhs: Self) { |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
361 |
self.x.$name(rhs.x); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
362 |
self.y.$name(rhs.y); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
363 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
364 |
} |
13962 | 365 |
}; |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
366 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
367 |
|
14167 | 368 |
macro_rules! fp_scalar_bin_op_impl { |
369 |
($($op: tt)::+, $name: tt) => { |
|
370 |
impl $($op)::+<FPNum> for Point { |
|
371 |
type Output = FPPoint; |
|
372 |
||
373 |
#[inline] |
|
374 |
fn $name(self, rhs: FPNum) -> Self::Output { |
|
375 |
FPPoint::new(rhs.$name(self.x), rhs.$name(self.y)) |
|
376 |
} |
|
377 |
} |
|
378 |
}; |
|
379 |
} |
|
380 |
||
381 |
macro_rules! left_fp_scalar_bin_op_impl { |
|
382 |
($($op: tt)::+, $name: tt) => { |
|
383 |
impl $($op)::+<Point> for FPNum { |
|
384 |
type Output = FPPoint; |
|
385 |
||
386 |
#[inline] |
|
387 |
fn $name(self, rhs: Point) -> Self::Output { |
|
388 |
FPPoint::new(self.$name(rhs.x), self.$name(rhs.y)) |
|
389 |
} |
|
390 |
} |
|
391 |
}; |
|
392 |
} |
|
393 |
||
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
394 |
bin_op_impl!(Add, add); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
395 |
bin_op_impl!(Sub, sub); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
396 |
bin_op_impl!(Mul, mul); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
397 |
bin_op_impl!(Div, div); |
14109
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
398 |
scalar_bin_op_impl!(Mul, mul); |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
399 |
scalar_bin_op_impl!(Div, div); |
14167 | 400 |
fp_scalar_bin_op_impl!(Mul, mul); |
401 |
fp_scalar_bin_op_impl!(Div, div); |
|
402 |
left_fp_scalar_bin_op_impl!(Mul, mul); |
|
403 |
left_fp_scalar_bin_op_impl!(Div, div); |
|
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
404 |
bin_assign_op_impl!(AddAssign, add_assign); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
405 |
bin_assign_op_impl!(SubAssign, sub_assign); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
406 |
bin_assign_op_impl!(MulAssign, mul_assign); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
407 |
bin_assign_op_impl!(DivAssign, div_assign); |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
408 |
|
14072
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13971
diff
changeset
|
409 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13971
diff
changeset
|
410 |
pub struct Rect { |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
411 |
top_left: Point, |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
412 |
bottom_right: Point, |
14072
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13971
diff
changeset
|
413 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13971
diff
changeset
|
414 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13971
diff
changeset
|
415 |
impl Rect { |
14738 | 416 |
pub const EMPTY: Self = Self { |
417 |
top_left: Point::ZERO, |
|
14746 | 418 |
bottom_right: Point::diag(-1), |
14738 | 419 |
}; |
420 |
||
14072
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13971
diff
changeset
|
421 |
#[inline] |
15850 | 422 |
pub const fn new(top_left: Point, bottom_right: Point) -> Self { |
14648 | 423 |
debug_assert!(top_left.x <= bottom_right.x + 1); |
424 |
debug_assert!(top_left.y <= bottom_right.y + 1); |
|
14161 | 425 |
Self { |
426 |
top_left, |
|
427 |
bottom_right, |
|
428 |
} |
|
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
429 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
430 |
|
15850 | 431 |
pub const fn from_box(left: i32, right: i32, top: i32, bottom: i32) -> Self { |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
432 |
Self::new(Point::new(left, top), Point::new(right, bottom)) |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
433 |
} |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
434 |
|
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
435 |
pub fn from_size(top_left: Point, size: Size) -> Self { |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
436 |
Self::new( |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
437 |
top_left, |
14161 | 438 |
top_left + Point::new(size.width as i32 - 1, size.height as i32 - 1), |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
439 |
) |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
440 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
441 |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
442 |
pub fn from_size_coords(x: i32, y: i32, width: usize, height: usize) -> Self { |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
443 |
Self::from_size(Point::new(x, y), Size::new(width, height)) |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
444 |
} |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
445 |
|
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
446 |
pub fn at_origin(size: Size) -> Self { |
14648 | 447 |
Self::from_size(Point::ZERO, size) |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
448 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
449 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
450 |
#[inline] |
14648 | 451 |
pub const fn width(&self) -> usize { |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
452 |
(self.right() - self.left() + 1) as usize |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
453 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
454 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
455 |
#[inline] |
14648 | 456 |
pub const fn height(&self) -> usize { |
14228 | 457 |
(self.bottom() - self.top() + 1) as usize |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
458 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
459 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
460 |
#[inline] |
14648 | 461 |
pub const fn size(&self) -> Size { |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
462 |
Size::new(self.width(), self.height()) |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
463 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
464 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
465 |
#[inline] |
14648 | 466 |
pub const fn area(&self) -> usize { |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
467 |
self.size().area() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
468 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
469 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
470 |
#[inline] |
14648 | 471 |
pub const fn left(&self) -> i32 { |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
472 |
self.top_left().x |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
473 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
474 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
475 |
#[inline] |
14648 | 476 |
pub const fn top(&self) -> i32 { |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
477 |
self.top_left().y |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
478 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
479 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
480 |
#[inline] |
14648 | 481 |
pub const fn right(&self) -> i32 { |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
482 |
self.bottom_right().x |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
483 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
484 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
485 |
#[inline] |
14648 | 486 |
pub const fn bottom(&self) -> i32 { |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
487 |
self.bottom_right().y |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
488 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
489 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
490 |
#[inline] |
14648 | 491 |
pub const fn top_left(&self) -> Point { |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
492 |
self.top_left |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
493 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
494 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
495 |
#[inline] |
14648 | 496 |
pub const fn bottom_right(&self) -> Point { |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
497 |
self.bottom_right |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
498 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
499 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
500 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
501 |
pub fn center(&self) -> Point { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
502 |
(self.top_left() + self.bottom_right()) / 2 |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
503 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
504 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
505 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
506 |
pub fn with_margin(&self, margin: i32) -> Self { |
16056 | 507 |
let offset = Point::new(min(margin, self.width() as i32 / 2), min(margin, self.height() as i32 / 2)); |
14161 | 508 |
Self::new(self.top_left() + offset, self.bottom_right() - offset) |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
509 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
510 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
511 |
#[inline] |
15773
a4558e2be08c
add more const qualifiers to maintain a semblance of activity
alfadur
parents:
15405
diff
changeset
|
512 |
pub const fn x_range(&self) -> RangeInclusive<i32> { |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
513 |
self.left()..=self.right() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
514 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
515 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
516 |
#[inline] |
15773
a4558e2be08c
add more const qualifiers to maintain a semblance of activity
alfadur
parents:
15405
diff
changeset
|
517 |
pub const fn y_range(&self) -> RangeInclusive<i32> { |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
518 |
self.top()..=self.bottom() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
519 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
520 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
521 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
522 |
pub fn contains(&self, point: Point) -> bool { |
15046
dc4a12a84c92
remove RangeContains in favor of standard contains
alfadur
parents:
14747
diff
changeset
|
523 |
self.x_range().contains(&point.x) && self.y_range().contains(&point.y) |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
524 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
525 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
526 |
#[inline] |
15773
a4558e2be08c
add more const qualifiers to maintain a semblance of activity
alfadur
parents:
15405
diff
changeset
|
527 |
pub const fn contains_inside(&self, point: Point) -> bool { |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
528 |
point.x > self.left() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
529 |
&& point.x < self.right() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
530 |
&& point.y > self.top() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
531 |
&& point.y < self.bottom() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
532 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
533 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
534 |
#[inline] |
14738 | 535 |
pub fn contains_rect(&self, other: &Self) -> bool { |
536 |
self.contains(other.top_left()) && self.contains(other.bottom_right()) |
|
537 |
} |
|
538 |
||
539 |
#[inline] |
|
15773
a4558e2be08c
add more const qualifiers to maintain a semblance of activity
alfadur
parents:
15405
diff
changeset
|
540 |
pub const fn intersects(&self, other: &Rect) -> bool { |
14747 | 541 |
self.left() <= other.right() |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
542 |
&& self.right() >= other.left() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
543 |
&& self.top() <= other.bottom() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
544 |
&& self.bottom() >= other.top() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
545 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
546 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
547 |
#[inline] |
15850 | 548 |
pub const fn split_at(&self, point: Point) -> [Rect; 4] { |
15773
a4558e2be08c
add more const qualifiers to maintain a semblance of activity
alfadur
parents:
15405
diff
changeset
|
549 |
debug_assert!(self.contains_inside(point)); |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
550 |
[ |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
551 |
Self::from_box(self.left(), point.x, self.top(), point.y), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
552 |
Self::from_box(point.x, self.right(), self.top(), point.y), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
553 |
Self::from_box(point.x, self.right(), point.y, self.bottom()), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
554 |
Self::from_box(self.left(), point.x, point.y, self.bottom()), |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
555 |
] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
556 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
557 |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
558 |
#[inline] |
15850 | 559 |
pub const fn with_margins(&self, left: i32, right: i32, top: i32, bottom: i32) -> Self { |
14738 | 560 |
Self::from_box( |
14746 | 561 |
self.left() - left, |
14738 | 562 |
self.right() + right, |
14746 | 563 |
self.top() - top, |
14738 | 564 |
self.bottom() + bottom, |
565 |
) |
|
566 |
} |
|
567 |
||
568 |
#[inline] |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
569 |
pub fn quotient(self, x: usize, y: usize) -> Point { |
14161 | 570 |
self.top_left() + Point::new((x % self.width()) as i32, (y % self.height()) as i32) |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
571 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
572 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
573 |
|
14152 | 574 |
trait RangeClamp<T> { |
575 |
fn clamp(&self, value: T) -> T; |
|
576 |
} |
|
577 |
||
14161 | 578 |
impl<T: Ord + Copy> RangeClamp<T> for RangeInclusive<T> { |
14152 | 579 |
fn clamp(&self, value: T) -> T { |
580 |
if value < *self.start() { |
|
581 |
*self.start() |
|
582 |
} else if value > *self.end() { |
|
583 |
*self.end() |
|
584 |
} else { |
|
585 |
value |
|
586 |
} |
|
587 |
} |
|
14072
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13971
diff
changeset
|
588 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13971
diff
changeset
|
589 |
|
14115 | 590 |
pub struct Polygon { |
14146 | 591 |
vertices: Vec<Point>, |
14115 | 592 |
} |
593 |
||
594 |
impl Polygon { |
|
595 |
pub fn new(vertices: &[Point]) -> Self { |
|
596 |
let mut v = Vec::with_capacity(vertices.len() + 1); |
|
597 |
v.extend_from_slice(vertices); |
|
598 |
if !v.is_empty() { |
|
599 |
let start = v[0]; |
|
600 |
v.push(start); |
|
601 |
} |
|
602 |
Self { vertices: v } |
|
603 |
} |
|
604 |
||
605 |
pub fn edges_count(&self) -> usize { |
|
15236 | 606 |
self.vertices.len().saturating_sub(1) |
14115 | 607 |
} |
608 |
||
609 |
pub fn get_edge(&self, index: usize) -> Line { |
|
610 |
Line::new(self.vertices[index], self.vertices[index + 1]) |
|
611 |
} |
|
612 |
||
14145 | 613 |
pub fn split_edge(&mut self, edge_index: usize, vertex: Point) { |
614 |
self.vertices.insert(edge_index + 1, vertex); |
|
615 |
} |
|
616 |
||
16081
6633961698ad
Make wider range of numbers representable with FPNum on the cost of smaller precision
unC0Rr
parents:
16068
diff
changeset
|
617 |
pub fn iter(&self) -> impl Iterator<Item = &Point> { |
14145 | 618 |
(&self.vertices[..self.edges_count()]).iter() |
619 |
} |
|
620 |
||
16081
6633961698ad
Make wider range of numbers representable with FPNum on the cost of smaller precision
unC0Rr
parents:
16068
diff
changeset
|
621 |
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> { |
14176 | 622 |
let edges_count = self.edges_count(); |
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
623 |
let start = self.vertices.as_mut_ptr(); |
14176 | 624 |
let end = unsafe { start.add(edges_count) }; |
14161 | 625 |
PolygonPointsIteratorMut { |
626 |
source: self, |
|
627 |
start, |
|
628 |
end, |
|
629 |
} |
|
14115 | 630 |
} |
631 |
||
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
632 |
fn force_close(&mut self) { |
14154 | 633 |
if !self.vertices.is_empty() { |
14229 | 634 |
let edges_count = self.edges_count(); |
635 |
self.vertices[edges_count] = self.vertices[0]; |
|
14154 | 636 |
} |
637 |
} |
|
638 |
||
14115 | 639 |
pub fn iter_edges<'a>(&'a self) -> impl Iterator<Item = Line> + 'a { |
640 |
(&self.vertices[0..self.edges_count()]) |
|
641 |
.iter() |
|
642 |
.zip(&self.vertices[1..]) |
|
643 |
.map(|(s, e)| Line::new(*s, *e)) |
|
644 |
} |
|
14161 | 645 |
|
646 |
pub fn bezierize(&mut self, segments_number: u32) { |
|
647 |
fn calc_point(p1: Point, p2: Point, p3: Point) -> FPPoint { |
|
648 |
let diff13 = (p1 - p3).to_fppoint(); |
|
649 |
let diff13_norm = diff13.distance(); |
|
650 |
||
651 |
if diff13_norm.is_zero() { |
|
652 |
diff13 |
|
653 |
} else { |
|
654 |
let diff12_norm = (p1 - p2).to_fppoint().distance(); |
|
655 |
let diff23_norm = (p2 - p3).to_fppoint().distance(); |
|
656 |
let min_distance = min(diff13_norm, min(diff12_norm, diff23_norm)); |
|
657 |
||
658 |
diff13 * min_distance / diff13_norm / 3 |
|
659 |
} |
|
660 |
} |
|
661 |
||
662 |
if self.vertices.len() < 4 { |
|
663 |
return; |
|
664 |
} |
|
665 |
||
666 |
let delta = fp!(1 / segments_number); |
|
667 |
let mut bezierized_vertices = Vec::new(); |
|
668 |
let mut pi = 0; |
|
669 |
let mut i = 1; |
|
670 |
let mut ni = 2; |
|
671 |
let mut right_point = calc_point(self.vertices[pi], self.vertices[i], self.vertices[ni]); |
|
672 |
let mut left_point; |
|
673 |
||
674 |
pi += 1; |
|
675 |
while pi != 0 { |
|
676 |
pi = i; |
|
677 |
i = ni; |
|
678 |
ni += 1; |
|
679 |
if ni >= self.vertices.len() { |
|
680 |
ni = 0; |
|
681 |
} |
|
682 |
||
683 |
left_point = right_point; |
|
684 |
right_point = calc_point(self.vertices[pi], self.vertices[i], self.vertices[ni]); |
|
685 |
||
686 |
bezierized_vertices.extend(BezierCurveSegments::new( |
|
687 |
Line::new(self.vertices[pi], self.vertices[i]), |
|
688 |
left_point, |
|
689 |
-right_point, |
|
690 |
delta, |
|
691 |
)); |
|
692 |
} |
|
693 |
||
694 |
self.vertices = bezierized_vertices; |
|
695 |
} |
|
14115 | 696 |
} |
697 |
||
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
698 |
struct PolygonPointsIteratorMut<'a> { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
699 |
source: &'a mut Polygon, |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
700 |
start: *mut Point, |
14161 | 701 |
end: *mut Point, |
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
702 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
703 |
|
14161 | 704 |
impl<'a> Iterator for PolygonPointsIteratorMut<'a> { |
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
705 |
type Item = &'a mut Point; |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
706 |
|
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
707 |
fn next(&mut self) -> Option<<Self as Iterator>::Item> { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
708 |
if self.start == self.end { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
709 |
None |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
710 |
} else { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
711 |
unsafe { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
712 |
let result = &mut *self.start; |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
713 |
self.start = self.start.add(1); |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
714 |
Some(result) |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
715 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
716 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
717 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
718 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
719 |
|
14161 | 720 |
impl<'a> Drop for PolygonPointsIteratorMut<'a> { |
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
721 |
fn drop(&mut self) { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
722 |
self.source.force_close(); |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
723 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
724 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
725 |
|
14115 | 726 |
impl From<Vec<Point>> for Polygon { |
727 |
fn from(mut v: Vec<Point>) -> Self { |
|
728 |
if !v.is_empty() && v[0] != v[v.len() - 1] { |
|
729 |
let start = v[0]; |
|
730 |
v.push(start) |
|
731 |
} |
|
732 |
Self { vertices: v } |
|
733 |
} |
|
734 |
} |
|
735 |
||
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
736 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
14152 | 737 |
pub struct Ray { |
738 |
pub start: Point, |
|
14161 | 739 |
pub direction: Point, |
14152 | 740 |
} |
741 |
||
742 |
impl Ray { |
|
743 |
#[inline] |
|
14648 | 744 |
pub const fn new(start: Point, direction: Point) -> Ray { |
14152 | 745 |
Self { start, direction } |
746 |
} |
|
747 |
||
748 |
#[inline] |
|
14648 | 749 |
pub const fn tangent_mul(&self, x: i32) -> i32 { |
14163 | 750 |
self.direction.tangent_mul(x) |
14152 | 751 |
} |
752 |
||
753 |
#[inline] |
|
14648 | 754 |
pub const fn cotangent_mul(&self, y: i32) -> i32 { |
14163 | 755 |
self.direction.cotangent_mul(y) |
14152 | 756 |
} |
757 |
||
758 |
#[inline] |
|
759 |
pub fn orientation(&self, point: Point) -> i32 { |
|
760 |
(point - self.start).cross(self.direction).signum() |
|
761 |
} |
|
762 |
} |
|
763 |
||
764 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
765 |
pub struct Line { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
766 |
pub start: Point, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
767 |
pub end: Point, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
768 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
769 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
770 |
impl Line { |
14648 | 771 |
pub const ZERO: Self = Self::new(Point::ZERO, Point::ZERO); |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
772 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
773 |
#[inline] |
14648 | 774 |
pub const fn new(start: Point, end: Point) -> Self { |
775 |
Self { start, end } |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
776 |
} |
14098 | 777 |
|
778 |
#[inline] |
|
779 |
pub fn center(&self) -> Point { |
|
14109
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14102
diff
changeset
|
780 |
(self.start + self.end) / 2 |
14098 | 781 |
} |
14127 | 782 |
|
783 |
#[inline] |
|
14152 | 784 |
pub fn scaled_direction(&self) -> Point { |
785 |
self.end - self.start |
|
786 |
} |
|
787 |
||
788 |
#[inline] |
|
14127 | 789 |
pub fn scaled_normal(&self) -> Point { |
14152 | 790 |
self.scaled_direction().rotate90() |
791 |
} |
|
792 |
||
793 |
#[inline] |
|
794 |
pub fn to_ray(&self) -> Ray { |
|
795 |
Ray::new(self.start, self.scaled_direction()) |
|
14127 | 796 |
} |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
797 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
798 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
799 |
impl IntoIterator for Line { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
800 |
type Item = Point; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
801 |
type IntoIter = LinePoints; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
802 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
803 |
fn into_iter(self) -> Self::IntoIter { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
804 |
LinePoints::new(self) |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
805 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
806 |
} |
14072
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13971
diff
changeset
|
807 |
|
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
808 |
pub struct LinePoints { |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
809 |
accumulator: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
810 |
direction: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
811 |
sign: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
812 |
current: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
813 |
total_steps: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
814 |
step: i32, |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
815 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
816 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
817 |
impl LinePoints { |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
818 |
pub fn new(line: Line) -> Self { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
819 |
let dir = line.end - line.start; |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
820 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
821 |
Self { |
14648 | 822 |
accumulator: Point::ZERO, |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
823 |
direction: dir.abs(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
824 |
sign: dir.signum(), |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14080
diff
changeset
|
825 |
current: line.start, |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
826 |
total_steps: dir.max_norm(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
827 |
step: 0, |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
828 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
829 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
830 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
831 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
832 |
impl Iterator for LinePoints { |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
833 |
type Item = Point; |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
834 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
835 |
fn next(&mut self) -> Option<Self::Item> { |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
836 |
if self.step <= self.total_steps { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
837 |
self.accumulator += self.direction; |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
838 |
|
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
839 |
if self.accumulator.x > self.total_steps { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
840 |
self.accumulator.x -= self.total_steps; |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
841 |
self.current.x += self.sign.x; |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
842 |
} |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
843 |
if self.accumulator.y > self.total_steps { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
844 |
self.accumulator.y -= self.total_steps; |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
845 |
self.current.y += self.sign.y; |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
846 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
847 |
|
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
848 |
self.step += 1; |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
849 |
|
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
850 |
Some(self.current) |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
851 |
} else { |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
852 |
None |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
853 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
854 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
855 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
856 |
|
13962 | 857 |
pub struct ArcPoints { |
858 |
point: Point, |
|
859 |
step: i32, |
|
860 |
} |
|
861 |
||
862 |
impl ArcPoints { |
|
14648 | 863 |
pub const fn new(radius: i32) -> Self { |
13962 | 864 |
Self { |
865 |
point: Point::new(0, radius), |
|
866 |
step: 3 - 2 * radius, |
|
867 |
} |
|
868 |
} |
|
869 |
} |
|
870 |
||
871 |
impl Iterator for ArcPoints { |
|
872 |
type Item = Point; |
|
873 |
||
874 |
fn next(&mut self) -> Option<Self::Item> { |
|
875 |
if self.point.x < self.point.y { |
|
876 |
let result = self.point; |
|
877 |
||
878 |
if self.step < 0 { |
|
879 |
self.step += self.point.x * 4 + 6; |
|
880 |
} else { |
|
881 |
self.step += (self.point.x - self.point.y) * 4 + 10; |
|
882 |
self.point.y -= 1; |
|
883 |
} |
|
884 |
||
885 |
self.point.x += 1; |
|
886 |
||
887 |
Some(result) |
|
888 |
} else if self.point.x == self.point.y { |
|
13968 | 889 |
self.point.x += 1; |
13971 | 890 |
|
13962 | 891 |
Some(self.point) |
892 |
} else { |
|
893 |
None |
|
894 |
} |
|
895 |
} |
|
896 |
} |
|
897 |
||
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
898 |
pub struct EquidistantPoints { |
15203 | 899 |
vector: Vec<Point>, |
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
900 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
901 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
902 |
impl EquidistantPoints { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
903 |
pub fn new(vector: Point) -> Self { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
904 |
Self { |
15203 | 905 |
vector: if vector.x == vector.y { |
906 |
vec![ |
|
907 |
Point::new(vector.x, vector.x), |
|
908 |
Point::new(vector.x, -vector.x), |
|
909 |
Point::new(-vector.x, -vector.x), |
|
910 |
Point::new(-vector.x, vector.x), |
|
911 |
] |
|
912 |
} else { |
|
913 |
vec![ |
|
914 |
Point::new(vector.x, vector.y), |
|
915 |
Point::new(vector.x, -vector.y), |
|
916 |
Point::new(-vector.x, -vector.y), |
|
917 |
Point::new(-vector.x, vector.y), |
|
918 |
Point::new(vector.y, vector.x), |
|
919 |
Point::new(vector.y, -vector.x), |
|
920 |
Point::new(-vector.y, -vector.x), |
|
921 |
Point::new(-vector.y, vector.x), |
|
922 |
] |
|
923 |
}, |
|
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
924 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
925 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
926 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
927 |
|
15203 | 928 |
impl IntoIterator for EquidistantPoints { |
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
929 |
type Item = Point; |
15203 | 930 |
type IntoIter = std::vec::IntoIter<Point>; |
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
931 |
|
15203 | 932 |
fn into_iter(self) -> Self::IntoIter { |
933 |
self.vector.into_iter() |
|
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
934 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
935 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
936 |
|
14160 | 937 |
pub struct BezierCurveSegments { |
938 |
segment: Line, |
|
14167 | 939 |
control_point1: FPPoint, |
940 |
control_point2: FPPoint, |
|
14160 | 941 |
offset: FPNum, |
14161 | 942 |
max_offset: FPNum, |
14160 | 943 |
delta: FPNum, |
14161 | 944 |
have_finished: bool, |
14160 | 945 |
} |
946 |
||
947 |
impl BezierCurveSegments { |
|
14161 | 948 |
pub fn new(segment: Line, p1: FPPoint, p2: FPPoint, delta: FPNum) -> Self { |
14160 | 949 |
Self { |
950 |
segment, |
|
14167 | 951 |
control_point1: segment.start.to_fppoint() - p1, |
952 |
control_point2: segment.end.to_fppoint() - p2, |
|
14160 | 953 |
offset: fp!(0), |
14161 | 954 |
max_offset: fp!(4095 / 4096), |
14160 | 955 |
delta, |
14161 | 956 |
have_finished: false, |
14160 | 957 |
} |
958 |
} |
|
959 |
} |
|
960 |
||
961 |
impl Iterator for BezierCurveSegments { |
|
962 |
type Item = Point; |
|
963 |
||
964 |
fn next(&mut self) -> Option<Self::Item> { |
|
14161 | 965 |
if self.offset < self.max_offset { |
14160 | 966 |
let offset_sq = self.offset * self.offset; |
967 |
let offset_cub = offset_sq * self.offset; |
|
968 |
||
969 |
let r1 = fp!(1) - self.offset * 3 + offset_sq * 3 - offset_cub; |
|
970 |
let r2 = self.offset * 3 - offset_sq * 6 + offset_cub * 3; |
|
971 |
let r3 = offset_sq * 3 - offset_cub * 3; |
|
972 |
||
14167 | 973 |
let p = r1 * self.segment.start |
974 |
+ r2 * self.control_point1 |
|
975 |
+ r3 * self.control_point2 |
|
976 |
+ offset_cub * self.segment.end; |
|
14160 | 977 |
|
978 |
self.offset += self.delta; |
|
979 |
||
14167 | 980 |
Some(Point::from_fppoint(&p)) |
14161 | 981 |
} else if !self.have_finished { |
982 |
self.have_finished = true; |
|
983 |
||
984 |
Some(self.segment.end) |
|
14160 | 985 |
} else { |
986 |
None |
|
987 |
} |
|
988 |
} |
|
989 |
} |
|
990 |
||
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
991 |
#[cfg(test)] |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
992 |
mod tests { |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
993 |
use super::*; |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
994 |
|
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
995 |
fn get_points(coords: &[(i32, i32)]) -> Vec<Point> { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
996 |
coords.iter().map(|(x, y)| Point::new(*x, *y)).collect() |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
997 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
998 |
|
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
999 |
#[test] |
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1000 |
fn line_basic() { |
14098 | 1001 |
let line: Vec<Point> = Line::new(Point::new(0, 0), Point::new(3, 3)) |
1002 |
.into_iter() |
|
1003 |
.collect(); |
|
1004 |
let v = get_points(&[(0, 0), (1, 1), (2, 2), (3, 3)]); |
|
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
1005 |
|
14098 | 1006 |
assert_eq!(line, v); |
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
1007 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
1008 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
1009 |
#[test] |
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1010 |
fn line_skewed() { |
14098 | 1011 |
let line: Vec<Point> = Line::new(Point::new(0, 0), Point::new(5, -7)) |
1012 |
.into_iter() |
|
1013 |
.collect(); |
|
13962 | 1014 |
let v = get_points(&[ |
1015 |
(0, 0), |
|
1016 |
(1, -1), |
|
1017 |
(2, -2), |
|
1018 |
(2, -3), |
|
1019 |
(3, -4), |
|
1020 |
(4, -5), |
|
1021 |
(4, -6), |
|
1022 |
(5, -7), |
|
1023 |
]); |
|
13959
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13958
diff
changeset
|
1024 |
|
14098 | 1025 |
assert_eq!(line, v); |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
1026 |
} |
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1027 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1028 |
#[test] |
13964
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13963
diff
changeset
|
1029 |
fn equidistant_full() { |
15203 | 1030 |
let n: Vec<Point> = EquidistantPoints::new(Point::new(1, 3)) |
1031 |
.into_iter() |
|
1032 |
.collect(); |
|
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1033 |
let v = get_points(&[ |
15203 | 1034 |
(1, 3), |
1035 |
(1, -3), |
|
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1036 |
(-1, -3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1037 |
(-1, 3), |
15203 | 1038 |
(3, 1), |
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1039 |
(3, -1), |
15203 | 1040 |
(-3, -1), |
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1041 |
(-3, 1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1042 |
]); |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1043 |
|
14098 | 1044 |
assert_eq!(n, v); |
13963
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13962
diff
changeset
|
1045 |
} |
13964
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13963
diff
changeset
|
1046 |
|
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13963
diff
changeset
|
1047 |
#[test] |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13963
diff
changeset
|
1048 |
fn equidistant_half() { |
15203 | 1049 |
let n: Vec<Point> = EquidistantPoints::new(Point::new(2, 2)) |
1050 |
.into_iter() |
|
1051 |
.collect(); |
|
1052 |
let v = get_points(&[(2, 2), (2, -2), (-2, -2), (-2, 2)]); |
|
14098 | 1053 |
|
1054 |
assert_eq!(n, v); |
|
1055 |
} |
|
13964
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13963
diff
changeset
|
1056 |
|
14098 | 1057 |
#[test] |
1058 |
fn line() { |
|
1059 |
let l = Line::new(Point::new(1, 1), Point::new(5, 6)); |
|
1060 |
||
1061 |
assert_eq!(l.center(), Point::new(3, 3)); |
|
13964
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13963
diff
changeset
|
1062 |
} |
14099 | 1063 |
|
1064 |
#[test] |
|
1065 |
fn rect() { |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
1066 |
let r = Rect::from_box(10, 100, 0, 70); |
14099 | 1067 |
|
14102 | 1068 |
assert!(r.contains_inside(Point::new(99, 69))); |
1069 |
assert!(!r.contains_inside(Point::new(100, 70))); |
|
1070 |
||
14099 | 1071 |
assert_eq!(r.top_left(), Point::new(10, 0)); |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
1072 |
assert_eq!(r.with_margin(12), Rect::from_box(22, 88, 12, 58)); |
14099 | 1073 |
} |
14146 | 1074 |
|
1075 |
#[test] |
|
1076 |
fn fit() { |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
1077 |
let r = Rect::from_box(10, 100, 0, 70); |
14146 | 1078 |
|
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
1079 |
assert_eq!(Point::new(0, -10).clamp(&r), Point::new(10, 0)); |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
1080 |
assert_eq!(Point::new(1000, 1000).clamp(&r), Point::new(100, 70)); |
14146 | 1081 |
} |
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
1082 |
} |