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