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