author | unc0rr |
Mon, 05 Nov 2018 21:36:28 +0100 | |
changeset 14139 | 37c99587825d |
parent 14137 | 3119d665d3c6 |
child 14140 | 3078123e84ea |
permissions | -rw-r--r-- |
14139 | 1 |
#[macro_use] |
14081 | 2 |
extern crate fpnum; |
3 |
||
14139 | 4 |
use fpnum::{distance, FPNum, FPPoint}; |
14134
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
5 |
use std::{ |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
6 |
cmp::max, |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
7 |
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Range, RangeInclusive, Sub, SubAssign} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
8 |
}; |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
9 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
10 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
11 |
pub struct Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
12 |
pub x: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
13 |
pub y: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
14 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
15 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
16 |
impl Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
17 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
18 |
pub fn new(x: i32, y: i32) -> Self { |
13941 | 19 |
Self { x, y } |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
20 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
21 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
22 |
#[inline] |
14131 | 23 |
pub fn diag(v: i32) -> Self { |
24 |
Self::new(v, v) |
|
25 |
} |
|
26 |
||
27 |
#[inline] |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
28 |
pub fn zero() -> Self { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
29 |
Self::new(0, 0) |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
30 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
31 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
32 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
33 |
pub fn signum(self) -> Self { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
34 |
Self::new(self.x.signum(), self.y.signum()) |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
35 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
36 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
37 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
38 |
pub fn abs(self) -> Self { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
39 |
Self::new(self.x.abs(), self.y.abs()) |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
40 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
41 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
42 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
43 |
pub fn dot(self, other: Point) -> i32 { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
44 |
self.x * other.x + self.y * other.y |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
45 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
46 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
47 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
48 |
pub fn max_norm(self) -> i32 { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
49 |
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:
13937
diff
changeset
|
50 |
} |
14031 | 51 |
|
52 |
#[inline] |
|
14081 | 53 |
pub fn integral_norm(self) -> u32 { |
54 |
distance(self.x, self.y).abs_round() |
|
55 |
} |
|
56 |
||
57 |
#[inline] |
|
14031 | 58 |
pub fn transform(self, matrix: &[i32; 4]) -> Self { |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
59 |
Point::new( |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
60 |
matrix[0] * self.x + matrix[1] * self.y, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
61 |
matrix[2] * self.x + matrix[3] * self.y, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
62 |
) |
14031 | 63 |
} |
14106 | 64 |
|
65 |
#[inline] |
|
66 |
pub fn rotate90(self) -> Self { |
|
14107 | 67 |
Point::new(self.y, -self.x) |
14106 | 68 |
} |
14108 | 69 |
|
70 |
#[inline] |
|
71 |
pub fn cross(self, other: Point) -> i32 { |
|
72 |
self.dot(other.rotate90()) |
|
73 |
} |
|
14125 | 74 |
|
75 |
#[inline] |
|
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
76 |
pub fn clamp(self, rect: &Rect) -> Point { |
14131 | 77 |
Point::new( |
78 |
rect.x_range().clamp(self.x), |
|
79 |
rect.y_range().clamp(self.y) |
|
80 |
) |
|
81 |
} |
|
82 |
||
83 |
#[inline] |
|
84 |
pub fn line_to(self, end: Point) -> Line { |
|
85 |
Line::new(self, end) |
|
86 |
} |
|
14125 | 87 |
|
14131 | 88 |
#[inline] |
89 |
pub fn ray_to(self, end: Point) -> Ray { |
|
90 |
self.line_to(end).to_ray() |
|
91 |
} |
|
92 |
||
93 |
#[inline] |
|
94 |
pub fn tangent(self) -> i32 { |
|
95 |
self.y / self.x |
|
96 |
} |
|
97 |
||
98 |
#[inline] |
|
99 |
pub fn cotangent(self) -> i32 { |
|
100 |
self.x / self.y |
|
14125 | 101 |
} |
14139 | 102 |
|
103 |
#[inline] |
|
104 |
pub fn to_fppoint(&self) -> FPPoint { |
|
105 |
FPPoint::new(self.x.into(), self.y.into()) |
|
106 |
} |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
107 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
108 |
|
14032 | 109 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
110 |
pub struct Size { |
|
111 |
pub width: usize, |
|
112 |
pub height: usize, |
|
113 |
} |
|
114 |
||
115 |
impl Size { |
|
116 |
#[inline] |
|
117 |
pub fn new(width: usize, height: usize) -> Self { |
|
118 |
Size { width, height } |
|
119 |
} |
|
120 |
||
121 |
#[inline] |
|
122 |
pub fn square(size: usize) -> Self { |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
123 |
Size { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
124 |
width: size, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
125 |
height: size, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
126 |
} |
14032 | 127 |
} |
128 |
||
129 |
#[inline] |
|
130 |
pub fn area(&self) -> usize { |
|
131 |
self.width * self.height |
|
132 |
} |
|
133 |
||
134 |
#[inline] |
|
135 |
pub fn linear_index(&self, x: usize, y: usize) -> usize { |
|
136 |
y * self.width + x |
|
137 |
} |
|
138 |
||
139 |
#[inline] |
|
140 |
pub fn is_power_of_two(&self) -> bool { |
|
141 |
self.width.is_power_of_two() && self.height.is_power_of_two() |
|
142 |
} |
|
143 |
||
144 |
#[inline] |
|
14052 | 145 |
pub fn next_power_of_two(&self) -> Self { |
146 |
Self { |
|
147 |
width: self.width.next_power_of_two(), |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
148 |
height: self.height.next_power_of_two(), |
14052 | 149 |
} |
150 |
} |
|
151 |
||
152 |
#[inline] |
|
14032 | 153 |
pub fn to_mask(&self) -> SizeMask { |
154 |
SizeMask::new(*self) |
|
155 |
} |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
156 |
|
14125 | 157 |
#[inline] |
158 |
pub fn to_square(&self) -> Size { |
|
159 |
Size::square(max(self.width, self.height)) |
|
160 |
} |
|
161 |
||
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
162 |
pub fn to_grid_index(&self) -> GridIndex { |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
163 |
GridIndex::new(*self) |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
164 |
} |
14032 | 165 |
} |
166 |
||
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
167 |
pub struct SizeMask { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
168 |
size: Size, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
169 |
} |
14032 | 170 |
|
171 |
impl SizeMask { |
|
172 |
#[inline] |
|
173 |
pub fn new(size: Size) -> Self { |
|
174 |
assert!(size.is_power_of_two()); |
|
175 |
let size = Size { |
|
176 |
width: !(size.width - 1), |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
177 |
height: !(size.height - 1), |
14032 | 178 |
}; |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
179 |
Self { size } |
14032 | 180 |
} |
181 |
||
182 |
#[inline] |
|
183 |
pub fn contains_x<T: Into<usize>>(&self, x: T) -> bool { |
|
184 |
(self.size.width & x.into()) == 0 |
|
185 |
} |
|
186 |
||
187 |
#[inline] |
|
188 |
pub fn contains_y<T: Into<usize>>(&self, y: T) -> bool { |
|
189 |
(self.size.height & y.into()) == 0 |
|
190 |
} |
|
191 |
||
192 |
#[inline] |
|
193 |
pub fn contains(&self, point: Point) -> bool { |
|
194 |
self.contains_x(point.x as usize) && self.contains_y(point.y as usize) |
|
195 |
} |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
196 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
197 |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
198 |
pub struct GridIndex { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
199 |
shift: Point, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
200 |
} |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
201 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
202 |
impl GridIndex { |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
203 |
pub fn new(size: Size) -> Self { |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
204 |
assert!(size.is_power_of_two()); |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
205 |
let shift = Point::new( |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
206 |
size.width.trailing_zeros() as i32, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
207 |
size.height.trailing_zeros() as i32, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
208 |
); |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
209 |
Self { shift } |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
210 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
211 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
212 |
pub fn map(&self, position: Point) -> Point { |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
213 |
Point::new(position.x >> self.shift.x, position.y >> self.shift.y) |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
214 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
215 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
216 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
217 |
macro_rules! bin_op_impl { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
218 |
($op: ty, $name: tt) => { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
219 |
impl $op for Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
220 |
type Output = Self; |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
221 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
222 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
223 |
fn $name(self, rhs: Self) -> Self::Output { |
13941 | 224 |
Self::new(self.x.$name(rhs.x), self.y.$name(rhs.y)) |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
225 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
226 |
} |
13941 | 227 |
}; |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
228 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
229 |
|
14088
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
230 |
macro_rules! scalar_bin_op_impl { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
231 |
($($op: tt)::+, $name: tt) => { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
232 |
impl $($op)::+<i32> for Point { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
233 |
type Output = Self; |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
234 |
|
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
235 |
#[inline] |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
236 |
fn $name(self, rhs: i32) -> Self::Output { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
237 |
Self::new(self.x.$name(rhs), self.y.$name(rhs)) |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
238 |
} |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
239 |
} |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
240 |
}; |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
241 |
} |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
242 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
243 |
macro_rules! bin_assign_op_impl { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
244 |
($op: ty, $name: tt) => { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
245 |
impl $op for Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
246 |
#[inline] |
13941 | 247 |
fn $name(&mut self, rhs: Self) { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
248 |
self.x.$name(rhs.x); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
249 |
self.y.$name(rhs.y); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
250 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
251 |
} |
13941 | 252 |
}; |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
253 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
254 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
255 |
bin_op_impl!(Add, add); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
256 |
bin_op_impl!(Sub, sub); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
257 |
bin_op_impl!(Mul, mul); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
258 |
bin_op_impl!(Div, div); |
14088
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
259 |
scalar_bin_op_impl!(Mul, mul); |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
260 |
scalar_bin_op_impl!(Div, div); |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
261 |
bin_assign_op_impl!(AddAssign, add_assign); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
262 |
bin_assign_op_impl!(SubAssign, sub_assign); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
263 |
bin_assign_op_impl!(MulAssign, mul_assign); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
264 |
bin_assign_op_impl!(DivAssign, div_assign); |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
265 |
|
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
266 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
267 |
pub struct Rect { |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
268 |
top_left: Point, |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
269 |
bottom_right: Point, |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
270 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
271 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
272 |
impl Rect { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
273 |
#[inline] |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
274 |
pub fn new(top_left: Point, bottom_right: Point) -> Self { |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
275 |
assert!(top_left.x <= bottom_right.x + 1); |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
276 |
assert!(top_left.y <= bottom_right.y + 1); |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
277 |
Self { top_left, bottom_right } |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
278 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
279 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
280 |
pub fn from_box(left: i32, right: i32, top: i32, bottom: i32) -> Self { |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
281 |
Self::new(Point::new(left, top), Point::new(right, bottom)) |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
282 |
} |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
283 |
|
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
284 |
pub fn from_size(top_left: Point, size: Size) -> Self { |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
285 |
Self::new( |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
286 |
top_left, |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
287 |
top_left + Point::new(size.width as i32 - 1, size.height as i32 - 1) |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
288 |
) |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
289 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
290 |
|
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
291 |
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:
14135
diff
changeset
|
292 |
Self::from_size(Point::new(x, y), Size::new(width, height)) |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
293 |
} |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
294 |
|
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
295 |
pub fn at_origin(size: Size) -> Self { |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
296 |
Self::from_size(Point::zero(), size) |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
297 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
298 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
299 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
300 |
pub fn width(&self) -> usize { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
301 |
(self.right() - self.left() + 1) as usize |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
302 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
303 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
304 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
305 |
pub fn height(&self) -> usize { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
306 |
(self.right() - self.left() + 1) as usize |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
307 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
308 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
309 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
310 |
pub fn size(&self) -> Size { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
311 |
Size::new(self.width(), self.height()) |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
312 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
313 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
314 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
315 |
pub fn area(&self) -> usize { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
316 |
self.size().area() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
317 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
318 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
319 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
320 |
pub fn left(&self) -> i32 { |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
321 |
self.top_left().x |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
322 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
323 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
324 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
325 |
pub fn top(&self) -> i32 { |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
326 |
self.top_left().y |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
327 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
328 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
329 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
330 |
pub fn right(&self) -> i32 { |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
331 |
self.bottom_right().x |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
332 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
333 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
334 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
335 |
pub fn bottom(&self) -> i32 { |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
336 |
self.bottom_right().y |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
337 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
338 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
339 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
340 |
pub fn top_left(&self) -> Point { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
341 |
self.top_left |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
342 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
343 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
344 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
345 |
pub fn bottom_right(&self) -> Point { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
346 |
self.bottom_right |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
347 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
348 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
349 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
350 |
pub fn center(&self) -> Point { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
351 |
(self.top_left() + self.bottom_right()) / 2 |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
352 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
353 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
354 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
355 |
pub fn with_margin(&self, margin: i32) -> Self { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
356 |
let offset = Point::diag(margin); |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
357 |
Self::new( |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
358 |
self.top_left() + offset, |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
359 |
self.bottom_right() - offset |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
360 |
) |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
361 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
362 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
363 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
364 |
pub fn x_range(&self) -> RangeInclusive<i32> { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
365 |
self.left()..=self.right() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
366 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
367 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
368 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
369 |
pub fn y_range(&self) -> RangeInclusive<i32> { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
370 |
self.top()..=self.bottom() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
371 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
372 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
373 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
374 |
pub fn contains(&self, point: Point) -> bool { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
375 |
self.x_range().contains(point.x) && self.y_range().contains(point.y) |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
376 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
377 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
378 |
#[inline] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
379 |
pub fn contains_inside(&self, point: Point) -> bool { |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
380 |
point.x > self.left() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
381 |
&& point.x < self.right() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
382 |
&& point.y > self.top() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
383 |
&& point.y < self.bottom() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
384 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
385 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
386 |
#[inline] |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
387 |
pub fn intersects(&self, other: &Rect) -> bool { |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
388 |
self.left() <= self.right() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
389 |
&& self.right() >= other.left() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
390 |
&& self.top() <= other.bottom() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
391 |
&& self.bottom() >= other.top() |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
392 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
393 |
|
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
394 |
#[inline] |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
395 |
pub fn split_at(&self, point: Point) -> [Rect; 4] { |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
396 |
assert!(self.contains_inside(point)); |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
397 |
[ |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
398 |
Self::from_box(self.left(), point.x, self.top(), point.y), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
399 |
Self::from_box(point.x, self.right(), self.top(), point.y), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
400 |
Self::from_box(point.x, self.right(), point.y, self.bottom()), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
401 |
Self::from_box(self.left(), point.x, point.y, self.bottom()), |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
402 |
] |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
403 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
404 |
|
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
405 |
#[inline] |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
406 |
pub fn quotient(self, x: usize, y: usize) -> Point { |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
407 |
self.top_left() + |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
408 |
Point::new( |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
409 |
(x % self.width()) as i32, |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
410 |
(y % self.height()) as i32 |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
411 |
) |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
412 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
413 |
} |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
414 |
|
14131 | 415 |
trait RangeContains<T> { |
416 |
fn contains(&self, value: T) -> bool; |
|
417 |
} |
|
418 |
||
419 |
impl <T: Ord> RangeContains<T> for Range<T> { |
|
420 |
fn contains(&self, value: T) -> bool { |
|
421 |
value >= self.start && value < self.end |
|
422 |
} |
|
423 |
} |
|
424 |
||
425 |
impl <T: Ord> RangeContains<T> for RangeInclusive<T> { |
|
426 |
fn contains(&self, value: T) -> bool { |
|
427 |
value >= *self.start() && value <= *self.end() |
|
428 |
} |
|
429 |
} |
|
430 |
||
431 |
trait RangeClamp<T> { |
|
432 |
fn clamp(&self, value: T) -> T; |
|
433 |
} |
|
434 |
||
435 |
impl <T: Ord + Copy> RangeClamp<T> for RangeInclusive<T> { |
|
436 |
fn clamp(&self, value: T) -> T { |
|
437 |
if value < *self.start() { |
|
438 |
*self.start() |
|
439 |
} else if value > *self.end() { |
|
440 |
*self.end() |
|
441 |
} else { |
|
442 |
value |
|
443 |
} |
|
444 |
} |
|
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
445 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
446 |
|
14094 | 447 |
pub struct Polygon { |
14125 | 448 |
vertices: Vec<Point>, |
14094 | 449 |
} |
450 |
||
451 |
impl Polygon { |
|
452 |
pub fn new(vertices: &[Point]) -> Self { |
|
453 |
let mut v = Vec::with_capacity(vertices.len() + 1); |
|
454 |
v.extend_from_slice(vertices); |
|
455 |
if !v.is_empty() { |
|
456 |
let start = v[0]; |
|
457 |
v.push(start); |
|
458 |
} |
|
459 |
Self { vertices: v } |
|
460 |
} |
|
461 |
||
462 |
pub fn edges_count(&self) -> usize { |
|
463 |
self.vertices.len() - 1 |
|
464 |
} |
|
465 |
||
466 |
pub fn get_edge(&self, index: usize) -> Line { |
|
467 |
Line::new(self.vertices[index], self.vertices[index + 1]) |
|
468 |
} |
|
469 |
||
14124 | 470 |
pub fn split_edge(&mut self, edge_index: usize, vertex: Point) { |
471 |
self.vertices.insert(edge_index + 1, vertex); |
|
472 |
} |
|
473 |
||
474 |
pub fn iter<'a>(&'a self) -> impl Iterator<Item = &Point> + 'a { |
|
475 |
(&self.vertices[..self.edges_count()]).iter() |
|
476 |
} |
|
477 |
||
14134
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
478 |
pub fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &mut Point> + 'a { |
14124 | 479 |
let edges_count = self.edges_count(); |
14134
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
480 |
let start = self.vertices.as_mut_ptr(); |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
481 |
let end = unsafe { start.add(self.vertices.len()) }; |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
482 |
PolygonPointsIteratorMut { source: self, start, end } |
14094 | 483 |
} |
484 |
||
14134
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
485 |
fn force_close(&mut self) { |
14133 | 486 |
if !self.vertices.is_empty() { |
14134
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
487 |
self.vertices[0] = self.vertices[self.vertices.len() - 1]; |
14133 | 488 |
} |
489 |
} |
|
490 |
||
14094 | 491 |
pub fn iter_edges<'a>(&'a self) -> impl Iterator<Item = Line> + 'a { |
492 |
(&self.vertices[0..self.edges_count()]) |
|
493 |
.iter() |
|
494 |
.zip(&self.vertices[1..]) |
|
495 |
.map(|(s, e)| Line::new(*s, *e)) |
|
496 |
} |
|
497 |
} |
|
498 |
||
14134
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
499 |
struct PolygonPointsIteratorMut<'a> { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
500 |
source: &'a mut Polygon, |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
501 |
start: *mut Point, |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
502 |
end: *mut Point |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
503 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
504 |
|
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
505 |
impl <'a> Iterator for PolygonPointsIteratorMut<'a> { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
506 |
type Item = &'a mut Point; |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
507 |
|
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
508 |
fn next(&mut self) -> Option<<Self as Iterator>::Item> { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
509 |
if self.start == self.end { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
510 |
None |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
511 |
} else { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
512 |
unsafe { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
513 |
let result = &mut *self.start; |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
514 |
self.start = self.start.add(1); |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
515 |
Some(result) |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
516 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
517 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
518 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
519 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
520 |
|
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
521 |
impl <'a> Drop for PolygonPointsIteratorMut<'a> { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
522 |
fn drop(&mut self) { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
523 |
self.source.force_close(); |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
524 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
525 |
} |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14133
diff
changeset
|
526 |
|
14094 | 527 |
impl From<Vec<Point>> for Polygon { |
528 |
fn from(mut v: Vec<Point>) -> Self { |
|
529 |
if !v.is_empty() && v[0] != v[v.len() - 1] { |
|
530 |
let start = v[0]; |
|
531 |
v.push(start) |
|
532 |
} |
|
533 |
Self { vertices: v } |
|
534 |
} |
|
535 |
} |
|
536 |
||
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
537 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
14131 | 538 |
pub struct Ray { |
539 |
pub start: Point, |
|
540 |
pub direction: Point |
|
541 |
} |
|
542 |
||
543 |
impl Ray { |
|
544 |
#[inline] |
|
545 |
pub fn new(start: Point, direction: Point) -> Ray { |
|
546 |
Self { start, direction } |
|
547 |
} |
|
548 |
||
549 |
#[inline] |
|
550 |
pub fn tangent(&self) -> i32 { |
|
551 |
self.direction.tangent() |
|
552 |
} |
|
553 |
||
554 |
#[inline] |
|
555 |
pub fn cotangent(&self) -> i32 { |
|
556 |
self.direction.cotangent() |
|
557 |
} |
|
558 |
||
559 |
#[inline] |
|
560 |
pub fn orientation(&self, point: Point) -> i32 { |
|
561 |
(point - self.start).cross(self.direction).signum() |
|
562 |
} |
|
563 |
} |
|
564 |
||
565 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
566 |
pub struct Line { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
567 |
pub start: Point, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
568 |
pub end: Point, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
569 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
570 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
571 |
impl Line { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
572 |
#[inline] |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
573 |
pub fn new(start: Point, end: Point) -> Self { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
574 |
Self { start, end } |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
575 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
576 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
577 |
#[inline] |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
578 |
pub fn zero() -> Self { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
579 |
Self::new(Point::zero(), Point::zero()) |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
580 |
} |
14077 | 581 |
|
582 |
#[inline] |
|
583 |
pub fn center(&self) -> Point { |
|
14088
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
584 |
(self.start + self.end) / 2 |
14077 | 585 |
} |
14106 | 586 |
|
587 |
#[inline] |
|
14131 | 588 |
pub fn scaled_direction(&self) -> Point { |
589 |
self.end - self.start |
|
590 |
} |
|
591 |
||
592 |
#[inline] |
|
14106 | 593 |
pub fn scaled_normal(&self) -> Point { |
14131 | 594 |
self.scaled_direction().rotate90() |
595 |
} |
|
596 |
||
597 |
#[inline] |
|
598 |
pub fn to_ray(&self) -> Ray { |
|
599 |
Ray::new(self.start, self.scaled_direction()) |
|
14106 | 600 |
} |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
601 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
602 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
603 |
impl IntoIterator for Line { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
604 |
type Item = Point; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
605 |
type IntoIter = LinePoints; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
606 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
607 |
fn into_iter(self) -> Self::IntoIter { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
608 |
LinePoints::new(self) |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
609 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
610 |
} |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
611 |
|
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
612 |
pub struct LinePoints { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
613 |
accumulator: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
614 |
direction: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
615 |
sign: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
616 |
current: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
617 |
total_steps: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
618 |
step: i32, |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
619 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
620 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
621 |
impl LinePoints { |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
622 |
pub fn new(line: Line) -> Self { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
623 |
let dir = line.end - line.start; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
624 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
625 |
Self { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
626 |
accumulator: Point::zero(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
627 |
direction: dir.abs(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
628 |
sign: dir.signum(), |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
629 |
current: line.start, |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
630 |
total_steps: dir.max_norm(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
631 |
step: 0, |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
632 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
633 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
634 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
635 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
636 |
impl Iterator for LinePoints { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
637 |
type Item = Point; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
638 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
639 |
fn next(&mut self) -> Option<Self::Item> { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
640 |
if self.step <= self.total_steps { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
641 |
self.accumulator += self.direction; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
642 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
643 |
if self.accumulator.x > self.total_steps { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
644 |
self.accumulator.x -= self.total_steps; |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
645 |
self.current.x += self.sign.x; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
646 |
} |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
647 |
if self.accumulator.y > self.total_steps { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
648 |
self.accumulator.y -= self.total_steps; |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
649 |
self.current.y += self.sign.y; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
650 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
651 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
652 |
self.step += 1; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
653 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
654 |
Some(self.current) |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
655 |
} else { |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
656 |
None |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
657 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
658 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
659 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
660 |
|
13941 | 661 |
pub struct ArcPoints { |
662 |
point: Point, |
|
663 |
step: i32, |
|
664 |
} |
|
665 |
||
666 |
impl ArcPoints { |
|
667 |
pub fn new(radius: i32) -> Self { |
|
668 |
Self { |
|
669 |
point: Point::new(0, radius), |
|
670 |
step: 3 - 2 * radius, |
|
671 |
} |
|
672 |
} |
|
673 |
} |
|
674 |
||
675 |
impl Iterator for ArcPoints { |
|
676 |
type Item = Point; |
|
677 |
||
678 |
fn next(&mut self) -> Option<Self::Item> { |
|
679 |
if self.point.x < self.point.y { |
|
680 |
let result = self.point; |
|
681 |
||
682 |
if self.step < 0 { |
|
683 |
self.step += self.point.x * 4 + 6; |
|
684 |
} else { |
|
685 |
self.step += (self.point.x - self.point.y) * 4 + 10; |
|
686 |
self.point.y -= 1; |
|
687 |
} |
|
688 |
||
689 |
self.point.x += 1; |
|
690 |
||
691 |
Some(result) |
|
692 |
} else if self.point.x == self.point.y { |
|
13947 | 693 |
self.point.x += 1; |
13950 | 694 |
|
13941 | 695 |
Some(self.point) |
696 |
} else { |
|
697 |
None |
|
698 |
} |
|
699 |
} |
|
700 |
} |
|
701 |
||
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
702 |
pub struct EquidistantPoints { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
703 |
vector: Point, |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
704 |
iteration: u8, |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
705 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
706 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
707 |
impl EquidistantPoints { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
708 |
pub fn new(vector: Point) -> Self { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
709 |
Self { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
710 |
vector, |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
711 |
iteration: if vector.x == vector.y { 4 } else { 8 }, |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
712 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
713 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
714 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
715 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
716 |
impl Iterator for EquidistantPoints { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
717 |
type Item = Point; |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
718 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
719 |
fn next(&mut self) -> Option<Self::Item> { |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
720 |
if self.iteration > 0 { |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
721 |
self.vector.x = -self.vector.x; |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
722 |
if self.iteration & 1 == 0 { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
723 |
self.vector.y = -self.vector.y; |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
724 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
725 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
726 |
if self.iteration == 4 { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
727 |
std::mem::swap(&mut self.vector.x, &mut self.vector.y); |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
728 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
729 |
|
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
730 |
self.iteration -= 1; |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
731 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
732 |
Some(self.vector) |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
733 |
} else { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
734 |
None |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
735 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
736 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
737 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
738 |
|
14139 | 739 |
pub struct BezierCurveSegments { |
740 |
segment: Line, |
|
741 |
c1: FPPoint, |
|
742 |
c2: FPPoint, |
|
743 |
offset: FPNum, |
|
744 |
delta: FPNum, |
|
745 |
} |
|
746 |
||
747 |
impl BezierCurveSegments { |
|
748 |
pub fn new(segment: Line, p1: Point, p2: Point, delta: FPNum) -> Self { |
|
749 |
Self { |
|
750 |
segment, |
|
751 |
c1: (segment.start - p1).to_fppoint(), |
|
752 |
c2: (segment.end - p2).to_fppoint(), |
|
753 |
offset: fp!(0), |
|
754 |
delta, |
|
755 |
} |
|
756 |
} |
|
757 |
} |
|
758 |
||
759 |
impl Iterator for BezierCurveSegments { |
|
760 |
type Item = Point; |
|
761 |
||
762 |
fn next(&mut self) -> Option<Self::Item> { |
|
763 |
if self.offset < fp!(1) { |
|
764 |
let offset_sq = self.offset * self.offset; |
|
765 |
let offset_cub = offset_sq * self.offset; |
|
766 |
||
767 |
let r1 = fp!(1) - self.offset * 3 + offset_sq * 3 - offset_cub; |
|
768 |
let r2 = self.offset * 3 - offset_sq * 6 + offset_cub * 3; |
|
769 |
let r3 = offset_sq * 3 - offset_cub * 3; |
|
770 |
||
771 |
let x = r1 * self.segment.start.x |
|
772 |
+ r2 * self.c1.x() |
|
773 |
+ r3 * self.c2.x() |
|
774 |
+ offset_cub * self.segment.end.x; |
|
775 |
let y = r1 * self.segment.start.y |
|
776 |
+ r2 * self.c1.y() |
|
777 |
+ r3 * self.c2.y() |
|
778 |
+ offset_cub * self.segment.end.y; |
|
779 |
||
780 |
self.offset += self.delta; |
|
781 |
||
782 |
Some(Point::new(x.round(), y.round())) |
|
783 |
} else { |
|
784 |
None |
|
785 |
} |
|
786 |
} |
|
787 |
} |
|
788 |
||
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
789 |
#[cfg(test)] |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
790 |
mod tests { |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
791 |
use super::*; |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
792 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
793 |
fn get_points(coords: &[(i32, i32)]) -> Vec<Point> { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
794 |
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:
13937
diff
changeset
|
795 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
796 |
|
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
797 |
#[test] |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
798 |
fn line_basic() { |
14077 | 799 |
let line: Vec<Point> = Line::new(Point::new(0, 0), Point::new(3, 3)) |
800 |
.into_iter() |
|
801 |
.collect(); |
|
802 |
let v = get_points(&[(0, 0), (1, 1), (2, 2), (3, 3)]); |
|
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
803 |
|
14077 | 804 |
assert_eq!(line, v); |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
805 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
806 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
807 |
#[test] |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
808 |
fn line_skewed() { |
14077 | 809 |
let line: Vec<Point> = Line::new(Point::new(0, 0), Point::new(5, -7)) |
810 |
.into_iter() |
|
811 |
.collect(); |
|
13941 | 812 |
let v = get_points(&[ |
813 |
(0, 0), |
|
814 |
(1, -1), |
|
815 |
(2, -2), |
|
816 |
(2, -3), |
|
817 |
(3, -4), |
|
818 |
(4, -5), |
|
819 |
(4, -6), |
|
820 |
(5, -7), |
|
821 |
]); |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
822 |
|
14077 | 823 |
assert_eq!(line, v); |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
824 |
} |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
825 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
826 |
#[test] |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
827 |
fn equidistant_full() { |
14077 | 828 |
let n: Vec<Point> = EquidistantPoints::new(Point::new(1, 3)).collect(); |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
829 |
let v = get_points(&[ |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
830 |
(-1, -3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
831 |
(1, -3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
832 |
(-1, 3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
833 |
(1, 3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
834 |
(-3, -1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
835 |
(3, -1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
836 |
(-3, 1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
837 |
(3, 1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
838 |
]); |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
839 |
|
14077 | 840 |
assert_eq!(n, v); |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
841 |
} |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
842 |
|
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
843 |
#[test] |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
844 |
fn equidistant_half() { |
14077 | 845 |
let n: Vec<Point> = EquidistantPoints::new(Point::new(2, 2)).collect(); |
846 |
let v = get_points(&[(-2, -2), (2, -2), (-2, 2), (2, 2)]); |
|
847 |
||
848 |
assert_eq!(n, v); |
|
849 |
} |
|
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
850 |
|
14077 | 851 |
#[test] |
852 |
fn line() { |
|
853 |
let l = Line::new(Point::new(1, 1), Point::new(5, 6)); |
|
854 |
||
855 |
assert_eq!(l.center(), Point::new(3, 3)); |
|
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
856 |
} |
14078 | 857 |
|
858 |
#[test] |
|
859 |
fn rect() { |
|
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
860 |
let r = Rect::from_box(10, 100, 0, 70); |
14078 | 861 |
|
14081 | 862 |
assert!(r.contains_inside(Point::new(99, 69))); |
863 |
assert!(!r.contains_inside(Point::new(100, 70))); |
|
864 |
||
14078 | 865 |
assert_eq!(r.top_left(), Point::new(10, 0)); |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
866 |
assert_eq!(r.with_margin(12), Rect::from_box(22, 88, 12, 58)); |
14078 | 867 |
} |
14125 | 868 |
|
869 |
#[test] |
|
870 |
fn fit() { |
|
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
871 |
let r = Rect::from_box(10, 100, 0, 70); |
14125 | 872 |
|
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
873 |
assert_eq!(Point::new(0, -10).clamp(&r), Point::new(10, 0)); |
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14134
diff
changeset
|
874 |
assert_eq!(Point::new(1000, 1000).clamp(&r), Point::new(100, 70)); |
14125 | 875 |
} |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
876 |
} |