rust/landgen/src/outline.rs
changeset 14060 aeac678d7c79
parent 14059 1ac129df8e5b
parent 14054 bf77c4d2294f
child 14061 128fbd36eee4
equal deleted inserted replaced
14059:1ac129df8e5b 14060:aeac678d7c79
     1 use integral_geometry::Point;
       
     2 
       
     3 pub struct Outline {
       
     4     points: Vec<Point>,
       
     5 }
       
     6 
       
     7 fn check_intersect(
       
     8     segment1_start: &Point,
       
     9     segment1_end: &Point,
       
    10     segment2_start: &Point,
       
    11     segment2_end: &Point,
       
    12 ) -> bool {
       
    13     let dm: i32 = (segment2_end.y - segment2_start.y) * (segment1_end.x - segment1_start.x)
       
    14         - (segment2_end.x - segment2_start.x) * (segment1_end.y - segment1_start.y);
       
    15 
       
    16     if dm == 0 {
       
    17         return false;
       
    18     }
       
    19 
       
    20     let c1: i32 = (segment2_end.x - segment2_start.x) * (segment1_start.y - segment2_start.y)
       
    21         - (segment2_end.y - segment2_start.y) * (segment1_start.x - segment2_start.x);
       
    22 
       
    23     if dm > 0 {
       
    24         if (c1 < 0) || (c1 > dm) {
       
    25             return false;
       
    26         }
       
    27     } else {
       
    28         if (c1 > 0) || (c1 < dm) {
       
    29             return false;
       
    30         }
       
    31     }
       
    32 
       
    33     let c2: i32 = (segment1_end.x - segment2_start.x) * (segment1_start.y - segment2_start.y)
       
    34         - (segment1_end.y - segment2_start.y) * (segment1_start.x - segment2_start.x);
       
    35 
       
    36     if dm > 0 {
       
    37         if (c2 < 0) || (c2 > dm) {
       
    38             return false;
       
    39         }
       
    40     } else {
       
    41         if (c2 > 0) || (c2 < dm) {
       
    42             return false;
       
    43         }
       
    44     }
       
    45 
       
    46     true
       
    47 }
       
    48 
       
    49 impl Outline {
       
    50     fn check_intersects_self_at_index(&self, index: usize) -> bool {
       
    51         if index <= 0 || index > self.points.len() {
       
    52             return false;
       
    53         }
       
    54 
       
    55         for i in 1..=self.points.len() - 3 {
       
    56             if i <= index - 1 || i >= index + 2 {
       
    57                 if i != index - 1 && check_intersect(
       
    58                     &self.points[index],
       
    59                     &self.points[index - 1],
       
    60                     &self.points[i],
       
    61                     &self.points[i - 1],
       
    62                 ) {
       
    63                     return true;
       
    64                 }
       
    65                 if i != index + 2 && check_intersect(
       
    66                     &self.points[index],
       
    67                     &self.points[index + 1],
       
    68                     &self.points[i],
       
    69                     &self.points[i - 1],
       
    70                 ) {
       
    71                     return true;
       
    72                 }
       
    73             }
       
    74         }
       
    75 
       
    76         false
       
    77     }
       
    78 }
       
    79 
       
    80 #[cfg(test)]
       
    81 #[test]
       
    82 fn intersection() {
       
    83     let p1 = Point { x: 0, y: 0 };
       
    84     let p2 = Point { x: 0, y: 10 };
       
    85     let p3 = Point { x: -5, y: 5 };
       
    86     let p4 = Point { x: 5, y: 5 };
       
    87     let p5 = Point { x: 5, y: 16 };
       
    88 
       
    89     assert!(check_intersect(&p1, &p2, &p3, &p4));
       
    90     assert!(!check_intersect(&p1, &p2, &p3, &p5));
       
    91 }