rust/landgen/src/outline.rs
changeset 13921 939641cdb94f
parent 13908 9a1f1e8170f2
child 13938 1fa905aa4cdb
equal deleted inserted replaced
13920:7173b702e8db 13921:939641cdb94f
     5 
     5 
     6 pub struct Outline {
     6 pub struct Outline {
     7     points: Vec<Point>,
     7     points: Vec<Point>,
     8 }
     8 }
     9 
     9 
    10 fn check_intersect(v1: &Point, v2: &Point, v3: &Point, v4: &Point) -> bool {
    10 fn check_intersect(
    11     let dm: i32 = (v4.y - v3.y) * (v2.x - v1.x) - (v4.x - v3.x) * (v2.y - v1.y);
    11     segment1_start: &Point,
       
    12     segment1_end: &Point,
       
    13     segment2_start: &Point,
       
    14     segment2_end: &Point,
       
    15 ) -> bool {
       
    16     let dm: i32 = (segment2_end.y - segment2_start.y) * (segment1_end.x - segment1_start.x)
       
    17         - (segment2_end.x - segment2_start.x) * (segment1_end.y - segment1_start.y);
    12 
    18 
    13     if dm == 0 {
    19     if dm == 0 {
    14         return false;
    20         return false;
    15     }
    21     }
    16 
    22 
    17     let c1: i32 = (v4.x - v3.x) * (v1.y - v3.y) - (v4.y - v3.y) * (v1.x - v3.x);
    23     let c1: i32 = (segment2_end.x - segment2_start.x) * (segment1_start.y - segment2_start.y)
       
    24         - (segment2_end.y - segment2_start.y) * (segment1_start.x - segment2_start.x);
    18 
    25 
    19     if dm > 0 {
    26     if dm > 0 {
    20         if (c1 < 0) || (c1 > dm) {
    27         if (c1 < 0) || (c1 > dm) {
    21             return false;
    28             return false;
    22         }
    29         }
    24         if (c1 > 0) || (c1 < dm) {
    31         if (c1 > 0) || (c1 < dm) {
    25             return false;
    32             return false;
    26         }
    33         }
    27     }
    34     }
    28 
    35 
    29     let c2: i32 = (v2.x - v3.x) * (v1.y - v3.y) - (v2.y - v3.y) * (v1.x - v3.x);
    36     let c2: i32 = (segment1_end.x - segment2_start.x) * (segment1_start.y - segment2_start.y)
       
    37         - (segment1_end.y - segment2_start.y) * (segment1_start.x - segment2_start.x);
    30 
    38 
    31     if dm > 0 {
    39     if dm > 0 {
    32         if (c2 < 0) || (c2 > dm) {
    40         if (c2 < 0) || (c2 > dm) {
    33             return false;
    41             return false;
    34         }
    42         }
    73 }
    81 }
    74 
    82 
    75 #[cfg(test)]
    83 #[cfg(test)]
    76 #[test]
    84 #[test]
    77 fn intersection() {
    85 fn intersection() {
    78     let p1 = Point{x: 0, y: 0};
    86     let p1 = Point { x: 0, y: 0 };
    79     let p2 = Point{x: 0, y: 10};
    87     let p2 = Point { x: 0, y: 10 };
    80     let p3 = Point{x: -5, y: 5};
    88     let p3 = Point { x: -5, y: 5 };
    81     let p4 = Point{x: 5, y: 5};
    89     let p4 = Point { x: 5, y: 5 };
    82     let p5 = Point{x: 5, y: 16};
    90     let p5 = Point { x: 5, y: 16 };
    83 
    91 
    84     assert!(check_intersect(&p1, &p2, &p3, &p4));
    92     assert!(check_intersect(&p1, &p2, &p3, &p4));
    85     assert!(!check_intersect(&p1, &p2, &p3, &p5));
    93     assert!(!check_intersect(&p1, &p2, &p3, &p5));
    86 }
    94 }