rust/landgen/src/outline.rs
author unc0rr
Sat, 03 Nov 2018 00:02:03 +0100
changeset 14108 8556937ae338
parent 14105 4d22be35cfa2
child 14109 e2b6ff0421ec
permissions -rw-r--r--
Fix typo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     1
use itertools::Itertools;
14086
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
     2
use std::cmp::min;
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     3
14083
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
     4
use integral_geometry::{Line, Point, Rect, Size};
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     5
use land2d::Land2D;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     6
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     7
use outline_template::OutlineTemplate;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     8
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
     9
pub struct OutlinePoints {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    10
    pub islands: Vec<Vec<Point>>,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    11
    pub fill_points: Vec<Point>,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    12
    pub size: Size,
14083
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
    13
    pub play_box: Rect,
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    14
}
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    15
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    16
impl OutlinePoints {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    17
    pub fn from_outline_template<I: Iterator<Item = u32>>(
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    18
        outline_template: &OutlineTemplate,
14083
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
    19
        play_box: Rect,
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
    20
        size: Size,
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    21
        random_numbers: &mut I,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    22
    ) -> Self {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    23
        Self {
14083
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
    24
            play_box,
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
    25
            size,
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    26
            islands: outline_template
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    27
                .islands
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    28
                .iter()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    29
                .map(|i| {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    30
                    i.iter()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    31
                        .zip(random_numbers.tuples())
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    32
                        .map(|(rect, (rnd_a, rnd_b))| {
14083
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
    33
                            rect.top_left()
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
    34
                                + Point::new(
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
    35
                                    (rnd_a % rect.width) as i32,
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
    36
                                    (rnd_b % rect.height) as i32,
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
    37
                                )
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
    38
                                + play_box.top_left()
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    39
                        }).collect()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    40
                }).collect(),
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    41
            fill_points: outline_template.fill_points.clone(),
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    42
        }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    43
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    44
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    45
    pub fn total_len(&self) -> usize {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    46
        self.islands.iter().map(|i| i.len()).sum::<usize>() + self.fill_points.len()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    47
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    48
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    49
    pub fn iter(&self) -> impl Iterator<Item = &Point> {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    50
        self.islands
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    51
            .iter()
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    52
            .flat_map(|i| i.iter())
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    53
            .chain(self.fill_points.iter())
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    54
    }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    55
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    56
    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    57
        self.islands
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    58
            .iter_mut()
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    59
            .flat_map(|i| i.iter_mut())
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    60
            .chain(self.fill_points.iter_mut())
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    61
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    62
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    63
    fn divide_edge<I: Iterator<Item = u32>>(
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    64
        &self,
14081
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
    65
        segment: Line,
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    66
        distance_divisor: u32,
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    67
        random_numbers: &mut I,
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
    68
    ) -> Option<Point> {
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    69
        #[inline]
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    70
        fn intersect(p: &Point, m: &Point, p1: &Point, p2: &Point) -> bool {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    71
            let t1 = (m.x - p1.x) * p.y - p.x * (m.y - p1.y);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    72
            let t2 = (m.x - p2.x) * p.y - p.x * (m.y - p2.y);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    73
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    74
            (t1 > 0) != (t2 > 0)
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    75
        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    76
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    77
        #[inline]
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    78
        fn solve_intersection(p: &Point, m: &Point, s: &Point, e: &Point) -> Option<(i32, u32)> {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    79
            let f = *e - *s;
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    80
            let aqpb = (p.x * f.y - f.x * p.y) as i64;
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    81
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    82
            if aqpb != 0 {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    83
                let iy = ((((s.x - m.x) as i64 * p.y as i64 + m.y as i64 * p.x as i64)
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    84
                    * f.y as i64
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    85
                    - s.y as i64 * f.x as i64 * p.y as i64)
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    86
                    / aqpb) as i32;
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    87
                let ix = if p.y.abs() > f.y.abs() {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    88
                    (iy - m.y) * p.x / p.y + m.x
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    89
                } else {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    90
                    (iy - s.y) * f.x / f.y + s.x
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    91
                };
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    92
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    93
                let intersection_point = Point::new(ix, iy);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    94
                let diff_point = *m - intersection_point;
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    95
                let d = diff_point.integral_norm();
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    96
                let t = p.y * diff_point.y + p.x * diff_point.x;
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    97
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    98
                Some((t, d))
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
    99
            } else {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   100
                None
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   101
            }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   102
        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   103
14086
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   104
        let min_distance = 40;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   105
        // new point should fall inside this box
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   106
        let map_box = self.play_box.with_margin(min_distance);
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   107
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   108
        let p = Point::new(
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   109
            segment.end.y - segment.start.y,
14108
8556937ae338 Fix typo
unc0rr
parents: 14105
diff changeset
   110
            segment.start.x - segment.end.x,
14086
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   111
        );
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   112
        let mid_point = segment.center();
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   113
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   114
        if (p.integral_norm() < min_distance as u32 * 3) || !map_box.contains_inside(p) {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   115
            return None;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   116
        }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   117
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   118
        let full_box = Rect::from_size(Point::zero(), self.size).with_margin(min_distance);
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   119
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   120
        let mut dist_left = (self.size.width + self.size.height) as u32;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   121
        let mut dist_right = dist_left;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   122
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   123
        // find distances to map borders
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   124
        if p.x != 0 {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   125
            // check against left border
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   126
            let iyl = (map_box.left() - mid_point.x) * p.y / p.x + mid_point.y;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   127
            let dl = Point::new(mid_point.x - map_box.left(), mid_point.y - iyl).integral_norm();
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   128
            let t = p.x * (mid_point.x - full_box.left()) + p.y * (mid_point.y - iyl);
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   129
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   130
            if t > 0 {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   131
                dist_left = dl;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   132
            } else {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   133
                dist_right = dl;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   134
            }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   135
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   136
            // right border
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   137
            let iyr = (map_box.right() - mid_point.x) * p.y / p.x + mid_point.y;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   138
            let dr = Point::new(mid_point.x - full_box.right(), mid_point.y - iyr).integral_norm();
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   139
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   140
            if t > 0 {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   141
                dist_right = dr;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   142
            } else {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   143
                dist_left = dr;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   144
            }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   145
        }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   146
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   147
        if p.y != 0 {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   148
            // top border
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   149
            let ixl = (map_box.top() - mid_point.y) * p.x / p.y + mid_point.x;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   150
            let dl = Point::new(mid_point.y - map_box.top(), mid_point.x - ixl).integral_norm();
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   151
            let t = p.y * (mid_point.y - full_box.top()) + p.x * (mid_point.x - ixl);
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   152
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   153
            if t > 0 {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   154
                dist_left = min(dist_left, dl);
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   155
            } else {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   156
                dist_right = min(dist_right, dl);
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   157
            }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   158
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   159
            // bottom border
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   160
            let ixr = (map_box.bottom() - mid_point.y) * p.x / p.y + mid_point.x;
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   161
            let dr = Point::new(mid_point.y - full_box.bottom(), mid_point.x - ixr).integral_norm();
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   162
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   163
            if t > 0 {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   164
                dist_right = min(dist_right, dr);
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   165
            } else {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   166
                dist_left = min(dist_left, dr);
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   167
            }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   168
        }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   169
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   170
        // now go through all other segments
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   171
        for s in self.segments_iter() {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   172
            if s != segment {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   173
                if intersect(&p, &mid_point, &s.start, &s.end) {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   174
                    if let Some((t, d)) = solve_intersection(&p, &mid_point, &s.start, &s.end) {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   175
                        if t > 0 {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   176
                            dist_left = min(d, dist_left);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   177
                        } else {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   178
                            dist_right = min(d, dist_right);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   179
                        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   180
                    }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   181
                }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   182
            }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   183
        }
14086
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 14083
diff changeset
   184
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   185
        // go through all points, including fill points
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   186
        for pi in self.iter() {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   187
            if *pi != segment.start && *pi != segment.end {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   188
                if intersect(&p, &pi, &segment.start, &segment.end) {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   189
                    // ray from segment.start
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   190
                    if let Some((t, d)) = solve_intersection(&p, &mid_point, &segment.start, &pi) {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   191
                        if t > 0 {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   192
                            dist_left = min(d, dist_left);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   193
                        } else {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   194
                            dist_right = min(d, dist_right);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   195
                        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   196
                    }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   197
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   198
                    // ray from segment.end
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   199
                    if let Some((t, d)) = solve_intersection(&p, &mid_point, &segment.end, &pi) {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   200
                        if t > 0 {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   201
                            dist_left = min(d, dist_left);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   202
                        } else {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   203
                            dist_right = min(d, dist_right);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   204
                        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   205
                    }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   206
                }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   207
            }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   208
        }
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   209
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   210
        let max_dist = p.integral_norm() * 100 / distance_divisor;
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   211
        dist_left = min(dist_left, max_dist);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   212
        dist_right = min(dist_right, max_dist);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   213
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   214
        if dist_right + dist_left < min_distance as u32 * 2 + 10 {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   215
            // limits are too narrow, just divide
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   216
            Some(mid_point)
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   217
        } else {
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   218
            // select distance within [-dist_left; dist_right], keeping min_distance in mind
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   219
            let d = -(dist_left as i32)
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   220
                + min_distance
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   221
                + random_numbers.next().unwrap() as i32
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   222
                    % (dist_right as i32 + dist_left as i32 - min_distance * 2);
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   223
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   224
            Some(Point::new(
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   225
                mid_point.x + p.x * d / distance_divisor as i32,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   226
                mid_point.y + p.y * d / distance_divisor as i32,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   227
            ))
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   228
        }
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   229
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   230
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   231
    fn divide_edges<I: Iterator<Item = u32>>(
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   232
        &mut self,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   233
        distance_divisor: u32,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   234
        random_numbers: &mut I,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   235
    ) {
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   236
        for is in 0..self.islands.len() {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   237
            let mut i = 0;
14081
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   238
            let mut segment;
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   239
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   240
            loop {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   241
                {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   242
                    let island = &self.islands[is];
14081
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   243
                    let mut end_point;
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   244
                    if i < island.len() {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   245
                        end_point = if i + 1 < island.len() {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   246
                            island[i + 1]
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   247
                        } else {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   248
                            island[0]
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   249
                        };
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   250
                    } else {
14081
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   251
                        break;
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   252
                    }
14081
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   253
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   254
                    segment = Line::new(island[i], end_point);
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   255
                }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   256
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   257
                if let Some(new_point) = self.divide_edge(segment, distance_divisor, random_numbers)
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   258
                {
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   259
                    self.islands[is].insert(i + 1, new_point);
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   260
                    i += 2;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   261
                } else {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   262
                    i += 1;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   263
                }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   264
            }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   265
        }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   266
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   267
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   268
    pub fn bezierize(&mut self) {}
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   269
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   270
    pub fn distort<I: Iterator<Item = u32>>(
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   271
        &mut self,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   272
        distance_divisor: u32,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   273
        random_numbers: &mut I,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   274
    ) {
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   275
        loop {
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   276
            let old_len = self.total_len();
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   277
            self.divide_edges(distance_divisor, random_numbers);
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   278
14098
dbaa125a0fe9 fix infinite loop in landgen
alfadur
parents: 14086
diff changeset
   279
            if self.total_len() == old_len {
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   280
                break;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   281
            }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   282
        }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   283
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   284
        self.bezierize();
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   285
    }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   286
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   287
    pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) {
14081
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   288
        for segment in self.segments_iter() {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   289
            land.draw_line(segment, value);
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   290
        }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   291
    }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   292
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   293
    fn segments_iter(&self) -> OutlineSegmentsIterator {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   294
        OutlineSegmentsIterator {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   295
            outline: self,
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   296
            island: 0,
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   297
            index: 0,
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   298
        }
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   299
    }
14100
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14098
diff changeset
   300
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14098
diff changeset
   301
    pub fn mirror(&mut self) {
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   302
        let r = self.size.width as i32 - 1;
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   303
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   304
        self.iter_mut().for_each(|p| p.x = r - p.x);
14100
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14098
diff changeset
   305
    }
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14098
diff changeset
   306
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14098
diff changeset
   307
    pub fn flip(&mut self) {
14105
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   308
        let t = self.size.height as i32 - 1;
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   309
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14102
diff changeset
   310
        self.iter_mut().for_each(|p| p.y = t - p.y);
14100
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14098
diff changeset
   311
    }
14074
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents:
diff changeset
   312
}
14081
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   313
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   314
struct OutlineSegmentsIterator<'a> {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   315
    outline: &'a OutlinePoints,
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   316
    island: usize,
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   317
    index: usize,
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   318
}
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   319
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   320
impl<'a> Iterator for OutlineSegmentsIterator<'a> {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   321
    type Item = Line;
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   322
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   323
    fn next(&mut self) -> Option<Self::Item> {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   324
        if self.island < self.outline.islands.len() {
14083
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   325
            if self.index + 1 < self.outline.islands[self.island].len() {
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   326
                let result = Some(Line::new(
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   327
                    self.outline.islands[self.island][self.index],
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   328
                    self.outline.islands[self.island][self.index + 1],
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   329
                ));
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   330
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   331
                self.index += 1;
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   332
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   333
                result
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   334
            } else if self.index + 1 == self.outline.islands[self.island].len() {
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   335
                let result = Some(Line::new(
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   336
                    self.outline.islands[self.island][self.index],
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   337
                    self.outline.islands[self.island][0],
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   338
                ));
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   339
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   340
                self.island += 1;
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   341
                self.index = 0;
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   342
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   343
                result
14081
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   344
            } else {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   345
                self.island += 1;
14083
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   346
                self.index = 0;
14081
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   347
                self.next()
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   348
            }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   349
        } else {
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   350
            None
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   351
        }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   352
    }
e5904ead4864 Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents: 14074
diff changeset
   353
}
14083
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   354
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   355
#[test()]
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   356
fn points_test() {
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   357
    let mut points = OutlinePoints {
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   358
        islands: vec![
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   359
            vec![Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)],
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   360
            vec![Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)],
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   361
        ],
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   362
        fill_points: vec![Point::new(1, 1)],
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   363
        play_box: Rect::from_box(0, 100, 0, 100).with_margin(10),
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   364
        size: Size::square(100),
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   365
    };
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   366
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   367
    let segments: Vec<Line> = points.segments_iter().collect();
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   368
    assert_eq!(
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   369
        segments.first(),
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   370
        Some(&Line::new(Point::new(0, 0), Point::new(20, 0)))
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   371
    );
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   372
    assert_eq!(
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   373
        segments.last(),
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   374
        Some(&Line::new(Point::new(20, 15), Point::new(10, 15)))
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   375
    );
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   376
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   377
    points.iter_mut().for_each(|p| p.x = 2);
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   378
    assert_eq!(points.fill_points[0].x, 2);
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   379
    assert_eq!(points.islands[0][0].x, 2);
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14082
diff changeset
   380
}