1 use itertools::Itertools; |
1 use itertools::Itertools; |
2 use std::cmp::min; |
2 use std::cmp::min; |
3 |
3 |
4 use integral_geometry::{Line, Ray, Point, Polygon, Rect, Size}; |
4 use integral_geometry::{Line, Ray, Point, Polygon, Rect, RectInclusive, Size}; |
5 use land2d::Land2D; |
5 use land2d::Land2D; |
6 |
6 |
7 use outline_template::OutlineTemplate; |
7 use outline_template::OutlineTemplate; |
8 |
8 |
9 pub struct OutlinePoints { |
9 pub struct OutlinePoints { |
10 pub islands: Vec<Polygon>, |
10 pub islands: Vec<Polygon>, |
11 pub fill_points: Vec<Point>, |
11 pub fill_points: Vec<Point>, |
12 pub size: Size, |
12 pub size: Size, |
13 pub play_box: Rect, |
13 pub play_box: RectInclusive, |
14 intersections_box: Rect, |
14 intersections_box: RectInclusive, |
15 } |
15 } |
16 |
16 |
17 impl OutlinePoints { |
17 impl OutlinePoints { |
18 pub fn from_outline_template<I: Iterator<Item = u32>>( |
18 pub fn from_outline_template<I: Iterator<Item = u32>>( |
19 outline_template: &OutlineTemplate, |
19 outline_template: &OutlineTemplate, |
20 play_box: Rect, |
20 play_box: RectInclusive, |
21 size: Size, |
21 size: Size, |
22 random_numbers: &mut I, |
22 random_numbers: &mut I, |
23 ) -> Self { |
23 ) -> Self { |
24 Self { |
24 Self { |
25 play_box, |
25 play_box, |
36 .collect::<Vec<_>>() |
36 .collect::<Vec<_>>() |
37 .into() |
37 .into() |
38 }) |
38 }) |
39 .collect(), |
39 .collect(), |
40 fill_points: outline_template.fill_points.clone(), |
40 fill_points: outline_template.fill_points.clone(), |
41 intersections_box: Rect::at_origin(size) |
41 intersections_box: RectInclusive::at_origin(size) |
42 .with_margin(size.to_square().width as i32 * -2), |
42 .with_margin(size.to_square().width as i32 * -2), |
43 } |
43 } |
44 } |
44 } |
45 |
45 |
46 pub fn total_len(&self) -> usize { |
46 pub fn total_len(&self) -> usize { |
71 fn intersects(ray: &Ray, edge: &Line) -> bool { |
71 fn intersects(ray: &Ray, edge: &Line) -> bool { |
72 ray.orientation(edge.start) != ray.orientation(edge.end) |
72 ray.orientation(edge.start) != ray.orientation(edge.end) |
73 } |
73 } |
74 |
74 |
75 #[inline] |
75 #[inline] |
76 fn solve_intersection(intersections_box: &Rect, ray: &Ray, edge: &Line) -> Option<(i32, u32)> |
76 fn solve_intersection( |
|
77 intersections_box: &RectInclusive, |
|
78 ray: &Ray, |
|
79 edge: &Line |
|
80 ) -> Option<(i32, u32)> |
77 { |
81 { |
78 let edge_dir = edge.scaled_direction(); |
82 let edge_dir = edge.scaled_direction(); |
79 let aqpb = ray.direction.cross(edge_dir) as i64; |
83 let aqpb = ray.direction.cross(edge_dir) as i64; |
80 |
84 |
81 if aqpb != 0 { |
85 if aqpb != 0 { |
300 } |
304 } |
301 } |
305 } |
302 |
306 |
303 #[test()] |
307 #[test()] |
304 fn points_test() { |
308 fn points_test() { |
|
309 let size = Size::square(100); |
305 let mut points = OutlinePoints { |
310 let mut points = OutlinePoints { |
306 islands: vec![ |
311 islands: vec![ |
307 Polygon::new(&[Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)]), |
312 Polygon::new(&[Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)]), |
308 Polygon::new(&[Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)]), |
313 Polygon::new(&[Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)]), |
309 ], |
314 ], |
310 fill_points: vec![Point::new(1, 1)], |
315 fill_points: vec![Point::new(1, 1)], |
311 play_box: Rect::from_box(0, 100, 0, 100).with_margin(10), |
316 play_box: RectInclusive::at_origin(size).with_margin(10), |
312 size: Size::square(100), |
317 size: Size::square(100), |
313 intersections_box: Rect::from_box(0, 0, 100, 100), |
318 intersections_box: RectInclusive::at_origin(size), |
314 }; |
319 }; |
315 |
320 |
316 let segments: Vec<Line> = points.segments_iter().collect(); |
321 let segments: Vec<Line> = points.segments_iter().collect(); |
317 assert_eq!( |
322 assert_eq!( |
318 segments.first(), |
323 segments.first(), |