rust/landgen/src/outline.rs
author unc0rr
Sun, 14 Oct 2018 19:04:51 +0200
changeset 13929 9a1f1e8170f2
child 13942 939641cdb94f
permissions -rw-r--r--
Start work on landgen rust lib
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13929
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     1
pub struct Point {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     2
    x: i32,
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     3
    y: i32,
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     4
}
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     5
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     6
pub struct Outline {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     7
    points: Vec<Point>,
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     8
}
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     9
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    10
fn check_intersect(v1: &Point, v2: &Point, v3: &Point, v4: &Point) -> bool {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    11
    let dm: i32 = (v4.y - v3.y) * (v2.x - v1.x) - (v4.x - v3.x) * (v2.y - v1.y);
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    12
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    13
    if dm == 0 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    14
        return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    15
    }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    16
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    17
    let c1: i32 = (v4.x - v3.x) * (v1.y - v3.y) - (v4.y - v3.y) * (v1.x - v3.x);
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    18
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    19
    if dm > 0 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    20
        if (c1 < 0) || (c1 > dm) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    21
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    22
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    23
    } else {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    24
        if (c1 > 0) || (c1 < dm) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    25
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    26
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    27
    }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    28
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    29
    let c2: i32 = (v2.x - v3.x) * (v1.y - v3.y) - (v2.y - v3.y) * (v1.x - v3.x);
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    30
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    31
    if dm > 0 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    32
        if (c2 < 0) || (c2 > dm) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    33
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    34
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    35
    } else {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    36
        if (c2 > 0) || (c2 < dm) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    37
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    38
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    39
    }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    40
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    41
    true
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    42
}
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    43
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    44
impl Outline {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    45
    fn check_intersects_self_at_index(&self, index: usize) -> bool {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    46
        if index <= 0 || index > self.points.len() {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    47
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    48
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    49
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    50
        for i in 1..=self.points.len() - 3 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    51
            if i <= index - 1 || i >= index + 2 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    52
                if i != index - 1 && check_intersect(
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    53
                    &self.points[index],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    54
                    &self.points[index - 1],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    55
                    &self.points[i],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    56
                    &self.points[i - 1],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    57
                ) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    58
                    return true;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    59
                }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    60
                if i != index + 2 && check_intersect(
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    61
                    &self.points[index],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    62
                    &self.points[index + 1],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    63
                    &self.points[i],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    64
                    &self.points[i - 1],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    65
                ) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    66
                    return true;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    67
                }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    68
            }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    69
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    70
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    71
        false
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    72
    }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    73
}
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    74
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    75
#[cfg(test)]
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    76
#[test]
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    77
fn intersection() {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    78
    let p1 = Point{x: 0, y: 0};
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    79
    let p2 = Point{x: 0, y: 10};
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    80
    let p3 = Point{x: -5, y: 5};
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    81
    let p4 = Point{x: 5, y: 5};
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    82
    let p5 = Point{x: 5, y: 16};
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    83
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    84
    assert!(check_intersect(&p1, &p2, &p3, &p4));
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    85
    assert!(!check_intersect(&p1, &p2, &p3, &p5));
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    86
}