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