author | unC0Rr |
Sun, 01 Dec 2024 21:08:03 +0100 | |
branch | transitional_engine |
changeset 16073 | 5c941f5deeec |
parent 16064 | 0caa3dfb3ba2 |
permissions | -rw-r--r-- |
14090 | 1 |
use itertools::Itertools; |
14102 | 2 |
use std::cmp::min; |
14090 | 3 |
|
15850 | 4 |
use integral_geometry::{Line, Point, Polygon, Ray, Rect, Size}; |
14090 | 5 |
use land2d::Land2D; |
6 |
||
15951 | 7 |
use super::outline_template::OutlineTemplate; |
14090 | 8 |
|
9 |
pub struct OutlinePoints { |
|
14145 | 10 |
pub islands: Vec<Polygon>, |
16073
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
11 |
pub walls: Vec<Polygon>, |
14090 | 12 |
pub fill_points: Vec<Point>, |
13 |
pub size: Size, |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
14 |
pub play_box: Rect, |
16061 | 15 |
pub intersections_box: Rect, |
14090 | 16 |
} |
17 |
||
18 |
impl OutlinePoints { |
|
19 |
pub fn from_outline_template<I: Iterator<Item = u32>>( |
|
20 |
outline_template: &OutlineTemplate, |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
21 |
play_box: Rect, |
14099 | 22 |
size: Size, |
14090 | 23 |
random_numbers: &mut I, |
24 |
) -> Self { |
|
25 |
Self { |
|
14099 | 26 |
play_box, |
27 |
size, |
|
14090 | 28 |
islands: outline_template |
29 |
.islands |
|
30 |
.iter() |
|
31 |
.map(|i| { |
|
32 |
i.iter() |
|
33 |
.zip(random_numbers.tuples()) |
|
34 |
.map(|(rect, (rnd_a, rnd_b))| { |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
35 |
play_box.top_left() + rect.quotient(rnd_a as usize, rnd_b as usize) |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
36 |
}) |
14146 | 37 |
.collect::<Vec<_>>() |
38 |
.into() |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
39 |
}) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
40 |
.collect(), |
16073
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
41 |
walls: outline_template |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
42 |
.walls |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
43 |
.iter() |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
44 |
.map(|i| { |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
45 |
i.iter() |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
46 |
.zip(random_numbers.tuples()) |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
47 |
.map(|(rect, (rnd_a, rnd_b))| { |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
48 |
play_box.top_left() + rect.quotient(rnd_a as usize, rnd_b as usize) |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
49 |
}) |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
50 |
.collect::<Vec<_>>() |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
51 |
.into() |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
52 |
}) |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
53 |
.collect(), |
14090 | 54 |
fill_points: outline_template.fill_points.clone(), |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
55 |
intersections_box: Rect::at_origin(size) |
14146 | 56 |
.with_margin(size.to_square().width as i32 * -2), |
14090 | 57 |
} |
58 |
} |
|
59 |
||
60 |
pub fn total_len(&self) -> usize { |
|
14145 | 61 |
self.islands.iter().map(|i| i.edges_count()).sum::<usize>() + self.fill_points.len() |
14090 | 62 |
} |
63 |
||
14121 | 64 |
pub fn iter(&self) -> impl Iterator<Item = &Point> { |
65 |
self.islands |
|
66 |
.iter() |
|
14145 | 67 |
.flat_map(|p| p.iter()) |
16073
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
68 |
.chain(self.walls.iter().flat_map(|p| p.iter())) |
14121 | 69 |
.chain(self.fill_points.iter()) |
70 |
} |
|
71 |
||
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
72 |
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> { |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
73 |
self.islands |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
74 |
.iter_mut() |
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
75 |
.flat_map(|i| i.iter_mut()) |
16073
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
76 |
.chain(self.walls.iter_mut().flat_map(|p| p.iter_mut())) |
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
77 |
.chain(self.fill_points.iter_mut()) |
14090 | 78 |
} |
79 |
||
80 |
fn divide_edge<I: Iterator<Item = u32>>( |
|
81 |
&self, |
|
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
82 |
segment: Line, |
14121 | 83 |
distance_divisor: u32, |
16061 | 84 |
distortion_limiting_factor: u32, |
14090 | 85 |
random_numbers: &mut I, |
86 |
) -> Option<Point> { |
|
14121 | 87 |
#[inline] |
14152 | 88 |
fn intersects(ray: &Ray, edge: &Line) -> bool { |
89 |
ray.orientation(edge.start) != ray.orientation(edge.end) |
|
14121 | 90 |
} |
91 |
||
92 |
#[inline] |
|
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
93 |
fn solve_intersection( |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
94 |
intersections_box: &Rect, |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
95 |
ray: &Ray, |
15850 | 96 |
edge: &Line, |
97 |
) -> Option<(i32, u32)> { |
|
14152 | 98 |
let edge_dir = edge.scaled_direction(); |
99 |
let aqpb = ray.direction.cross(edge_dir) as i64; |
|
14121 | 100 |
|
101 |
if aqpb != 0 { |
|
15850 | 102 |
let mut iy = ((((edge.start.x - ray.start.x) as i64 * ray.direction.y as i64 |
103 |
+ ray.start.y as i64 * ray.direction.x as i64) |
|
14152 | 104 |
* edge_dir.y as i64 |
105 |
- edge.start.y as i64 * edge_dir.x as i64 * ray.direction.y as i64) |
|
14121 | 106 |
/ aqpb) as i32; |
14146 | 107 |
|
108 |
// is there better way to do it? |
|
109 |
if iy < intersections_box.top() { |
|
110 |
iy = intersections_box.top(); |
|
111 |
} else if iy > intersections_box.bottom() { |
|
112 |
iy = intersections_box.bottom(); |
|
113 |
} |
|
114 |
||
14152 | 115 |
let ix = if ray.direction.y.abs() > edge_dir.y.abs() { |
14163 | 116 |
ray.start.x + ray.direction.cotangent_mul(iy - ray.start.y) |
14121 | 117 |
} else { |
14163 | 118 |
edge.start.x + edge_dir.cotangent_mul(iy - edge.start.y) |
14121 | 119 |
}; |
120 |
||
14153 | 121 |
let intersection_point = Point::new(ix, iy).clamp(intersections_box); |
14152 | 122 |
let diff_point = ray.start - intersection_point; |
123 |
let t = ray.direction.dot(diff_point); |
|
14153 | 124 |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
125 |
if diff_point.max_norm() >= std::i16::MAX as i32 { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
126 |
Some((t, std::i32::MAX as u32)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
127 |
} else { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
128 |
let d = diff_point.integral_norm(); |
14121 | 129 |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
130 |
Some((t, d)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
131 |
} |
14121 | 132 |
} else { |
133 |
None |
|
134 |
} |
|
135 |
} |
|
136 |
||
16057
509ecce37522
Make outline template generator behave more like old engine
unC0Rr
parents:
15956
diff
changeset
|
137 |
let min_distance = distance_divisor as i32; |
14102 | 138 |
// new point should fall inside this box |
139 |
let map_box = self.play_box.with_margin(min_distance); |
|
140 |
||
14152 | 141 |
let normal = segment.scaled_normal(); |
142 |
let normal_len = normal.integral_norm(); |
|
14102 | 143 |
let mid_point = segment.center(); |
144 |
||
14152 | 145 |
if (normal_len < min_distance as u32 * 3) || !map_box.contains_inside(mid_point) { |
14102 | 146 |
return None; |
147 |
} |
|
148 |
||
14152 | 149 |
let normal_ray = Ray::new(mid_point, normal); |
14102 | 150 |
let mut dist_left = (self.size.width + self.size.height) as u32; |
151 |
let mut dist_right = dist_left; |
|
152 |
||
153 |
// find distances to map borders |
|
14152 | 154 |
if normal.x != 0 { |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
155 |
// where the normal line intersects the left map border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
156 |
let left_intersection = Point::new( |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
157 |
map_box.left(), |
14163 | 158 |
mid_point.y + normal.tangent_mul(map_box.left() - mid_point.x), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
159 |
); |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
160 |
dist_left = (mid_point - left_intersection).integral_norm(); |
14102 | 161 |
|
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
162 |
// same for the right border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
163 |
let right_intersection = Point::new( |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
164 |
map_box.right(), |
15850 | 165 |
mid_point.y + normal.tangent_mul(map_box.right() - mid_point.x), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
166 |
); |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
167 |
dist_right = (mid_point - right_intersection).integral_norm(); |
14102 | 168 |
|
14152 | 169 |
if normal.x > 0 { |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
170 |
std::mem::swap(&mut dist_left, &mut dist_right); |
14102 | 171 |
} |
172 |
} |
|
173 |
||
14152 | 174 |
if normal.y != 0 { |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
175 |
// where the normal line intersects the top map border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
176 |
let top_intersection = Point::new( |
14163 | 177 |
mid_point.x + normal.cotangent_mul(map_box.top() - mid_point.y), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
178 |
map_box.top(), |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
179 |
); |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
180 |
let dl = (mid_point - top_intersection).integral_norm(); |
14102 | 181 |
|
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
182 |
// same for the bottom border |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
183 |
let bottom_intersection = Point::new( |
14163 | 184 |
mid_point.x + normal.cotangent_mul(map_box.bottom() - mid_point.y), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
185 |
map_box.bottom(), |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14142
diff
changeset
|
186 |
); |
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
187 |
let dr = (mid_point - bottom_intersection).integral_norm(); |
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
188 |
|
14152 | 189 |
if normal.y < 0 { |
14102 | 190 |
dist_left = min(dist_left, dl); |
191 |
dist_right = min(dist_right, dr); |
|
192 |
} else { |
|
193 |
dist_left = min(dist_left, dr); |
|
14131
21642eb0ff29
import some clarity into border distance computation
alfadur
parents:
14130
diff
changeset
|
194 |
dist_right = min(dist_right, dl); |
14102 | 195 |
} |
196 |
} |
|
197 |
||
198 |
// now go through all other segments |
|
14121 | 199 |
for s in self.segments_iter() { |
15956 | 200 |
if s != segment && intersects(&normal_ray, &s) { |
201 |
if let Some((t, d)) = solve_intersection(&self.intersections_box, &normal_ray, &s) { |
|
202 |
if t > 0 { |
|
203 |
dist_right = min(dist_right, d); |
|
204 |
} else { |
|
205 |
dist_left = min(dist_left, d); |
|
14121 | 206 |
} |
207 |
} |
|
208 |
} |
|
209 |
} |
|
14102 | 210 |
|
14121 | 211 |
// go through all points, including fill points |
14147 | 212 |
for pi in self.iter().cloned() { |
15956 | 213 |
if pi != segment.start |
214 |
&& pi != segment.end |
|
215 |
&& intersects(&pi.ray_with_dir(normal), &segment) |
|
216 |
{ |
|
217 |
// ray from segment.start |
|
218 |
if let Some((t, d)) = solve_intersection( |
|
219 |
&self.intersections_box, |
|
220 |
&normal_ray, |
|
221 |
&segment.start.line_to(pi), |
|
222 |
) { |
|
223 |
if t > 0 { |
|
224 |
dist_right = min(dist_right, d); |
|
225 |
} else { |
|
226 |
dist_left = min(dist_left, d); |
|
14121 | 227 |
} |
15956 | 228 |
} |
14121 | 229 |
|
15956 | 230 |
// ray from segment.end |
231 |
if let Some((t, d)) = solve_intersection( |
|
232 |
&self.intersections_box, |
|
233 |
&normal_ray, |
|
234 |
&segment.end.line_to(pi), |
|
235 |
) { |
|
236 |
if t > 0 { |
|
237 |
dist_right = min(dist_right, d); |
|
238 |
} else { |
|
239 |
dist_left = min(dist_left, d); |
|
14121 | 240 |
} |
241 |
} |
|
242 |
} |
|
243 |
} |
|
244 |
||
16061 | 245 |
let max_dist = normal_len * 128 / distortion_limiting_factor; |
14121 | 246 |
dist_left = min(dist_left, max_dist); |
247 |
dist_right = min(dist_right, max_dist); |
|
248 |
||
249 |
if dist_right + dist_left < min_distance as u32 * 2 + 10 { |
|
250 |
// limits are too narrow, just divide |
|
251 |
Some(mid_point) |
|
252 |
} else { |
|
14134
be4419243735
fix normal offset for split points and make directions more consistent
alfadur
parents:
14132
diff
changeset
|
253 |
// select distance within [-dist_right; dist_left], keeping min_distance in mind |
be4419243735
fix normal offset for split points and make directions more consistent
alfadur
parents:
14132
diff
changeset
|
254 |
let d = -(dist_right as i32) |
14121 | 255 |
+ min_distance |
256 |
+ random_numbers.next().unwrap() as i32 |
|
257 |
% (dist_right as i32 + dist_left as i32 - min_distance * 2); |
|
258 |
||
14152 | 259 |
Some(mid_point + normal * d / normal_len as i32) |
14121 | 260 |
} |
14090 | 261 |
} |
262 |
||
14121 | 263 |
fn divide_edges<I: Iterator<Item = u32>>( |
264 |
&mut self, |
|
265 |
distance_divisor: u32, |
|
16061 | 266 |
distortion_limiting_factor: u32, |
14121 | 267 |
random_numbers: &mut I, |
268 |
) { |
|
14090 | 269 |
for is in 0..self.islands.len() { |
270 |
let mut i = 0; |
|
14145 | 271 |
while i < self.islands[is].edges_count() { |
272 |
let segment = self.islands[is].get_edge(i); |
|
16058 | 273 |
if let Some(new_point) = self.divide_edge( |
274 |
segment, |
|
275 |
distance_divisor, |
|
16061 | 276 |
distortion_limiting_factor, |
16058 | 277 |
random_numbers, |
278 |
) { |
|
14145 | 279 |
self.islands[is].split_edge(i, new_point); |
14090 | 280 |
i += 2; |
281 |
} else { |
|
282 |
i += 1; |
|
283 |
} |
|
284 |
} |
|
285 |
} |
|
286 |
} |
|
287 |
||
14161 | 288 |
pub fn bezierize(&mut self, segments_number: u32) { |
289 |
for island in &mut self.islands { |
|
290 |
island.bezierize(segments_number); |
|
291 |
} |
|
292 |
} |
|
14090 | 293 |
|
14121 | 294 |
pub fn distort<I: Iterator<Item = u32>>( |
295 |
&mut self, |
|
296 |
distance_divisor: u32, |
|
16064 | 297 |
distortion_limiting_factor: u32, |
14121 | 298 |
random_numbers: &mut I, |
299 |
) { |
|
14090 | 300 |
loop { |
301 |
let old_len = self.total_len(); |
|
16061 | 302 |
self.divide_edges(distance_divisor, distortion_limiting_factor, random_numbers); |
14090 | 303 |
|
14114 | 304 |
if self.total_len() == old_len { |
14090 | 305 |
break; |
306 |
} |
|
307 |
} |
|
308 |
} |
|
309 |
||
15942 | 310 |
pub fn draw<T: Copy + PartialEq + Default>(&self, land: &mut Land2D<T>, value: T) { |
16073
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
311 |
for segment in self.visible_segments_iter() { |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
312 |
land.draw_line(segment, value); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
313 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
314 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
315 |
|
16073
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
316 |
fn visible_segments_iter<'a>(&'a self) -> impl Iterator<Item = Line> + 'a { |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
317 |
self.islands |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
318 |
.iter() |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
319 |
.flat_map(|p| p.iter_edges()) |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
320 |
} |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
321 |
|
14145 | 322 |
fn segments_iter<'a>(&'a self) -> impl Iterator<Item = Line> + 'a { |
16073
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
323 |
self.islands |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
324 |
.iter() |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
325 |
.flat_map(|p| p.iter_edges()) |
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
326 |
.chain(self.walls.iter().flat_map(|p| p.iter_edges())) |
14090 | 327 |
} |
14116 | 328 |
|
329 |
pub fn mirror(&mut self) { |
|
14121 | 330 |
let r = self.size.width as i32 - 1; |
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
331 |
|
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
332 |
self.iter_mut().for_each(|p| p.x = r - p.x); |
14116 | 333 |
} |
334 |
||
335 |
pub fn flip(&mut self) { |
|
14121 | 336 |
let t = self.size.height as i32 - 1; |
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
337 |
|
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
338 |
self.iter_mut().for_each(|p| p.y = t - p.y); |
14116 | 339 |
} |
14090 | 340 |
} |
14097
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14090
diff
changeset
|
341 |
|
15777 | 342 |
#[test] |
14099 | 343 |
fn points_test() { |
14156
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14155
diff
changeset
|
344 |
let size = Size::square(100); |
14099 | 345 |
let mut points = OutlinePoints { |
346 |
islands: vec![ |
|
14145 | 347 |
Polygon::new(&[Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)]), |
348 |
Polygon::new(&[Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)]), |
|
14099 | 349 |
], |
16073
5c941f5deeec
* Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents:
16064
diff
changeset
|
350 |
walls: vec![], |
14099 | 351 |
fill_points: vec![Point::new(1, 1)], |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
352 |
play_box: Rect::at_origin(size).with_margin(10), |
14099 | 353 |
size: Size::square(100), |
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14156
diff
changeset
|
354 |
intersections_box: Rect::at_origin(size), |
14099 | 355 |
}; |
356 |
||
357 |
let segments: Vec<Line> = points.segments_iter().collect(); |
|
358 |
assert_eq!( |
|
359 |
segments.first(), |
|
360 |
Some(&Line::new(Point::new(0, 0), Point::new(20, 0))) |
|
361 |
); |
|
362 |
assert_eq!( |
|
363 |
segments.last(), |
|
364 |
Some(&Line::new(Point::new(20, 15), Point::new(10, 15))) |
|
365 |
); |
|
366 |
||
14155
09f62bb046ef
actually there is a way to preserve mutable polygon iterator
alfadur
parents:
14154
diff
changeset
|
367 |
points.iter_mut().for_each(|p| p.x = 2); |
14146 | 368 |
|
14099 | 369 |
assert_eq!(points.fill_points[0].x, 2); |
14145 | 370 |
assert_eq!(points.islands[0].get_edge(0).start.x, 2); |
14099 | 371 |
} |