rust/landgen/src/outline.rs
author unc0rr
Mon, 15 Oct 2018 19:34:11 +0200
changeset 13942 939641cdb94f
parent 13929 9a1f1e8170f2
child 13959 1fa905aa4cdb
permissions -rw-r--r--
Make parameters meaning more obvious
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
13942
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    10
fn check_intersect(
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    11
    segment1_start: &Point,
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    12
    segment1_end: &Point,
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    13
    segment2_start: &Point,
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    14
    segment2_end: &Point,
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    15
) -> bool {
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    16
    let dm: i32 = (segment2_end.y - segment2_start.y) * (segment1_end.x - segment1_start.x)
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    17
        - (segment2_end.x - segment2_start.x) * (segment1_end.y - segment1_start.y);
13929
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
        return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    21
    }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    22
13942
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    23
    let c1: i32 = (segment2_end.x - segment2_start.x) * (segment1_start.y - segment2_start.y)
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    24
        - (segment2_end.y - segment2_start.y) * (segment1_start.x - segment2_start.x);
13929
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    25
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    26
    if dm > 0 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    27
        if (c1 < 0) || (c1 > dm) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    28
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    29
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    30
    } else {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    31
        if (c1 > 0) || (c1 < dm) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    32
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    33
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    34
    }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    35
13942
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    36
    let c2: i32 = (segment1_end.x - segment2_start.x) * (segment1_start.y - segment2_start.y)
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    37
        - (segment1_end.y - segment2_start.y) * (segment1_start.x - segment2_start.x);
13929
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    38
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    39
    if dm > 0 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    40
        if (c2 < 0) || (c2 > dm) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    41
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    42
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    43
    } else {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    44
        if (c2 > 0) || (c2 < dm) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    45
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    46
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    47
    }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    48
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    49
    true
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    50
}
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    51
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    52
impl Outline {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    53
    fn check_intersects_self_at_index(&self, index: usize) -> bool {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    54
        if index <= 0 || index > self.points.len() {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    55
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    56
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    57
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    58
        for i in 1..=self.points.len() - 3 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    59
            if i <= index - 1 || i >= index + 2 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    60
                if i != index - 1 && 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
                if i != index + 2 && check_intersect(
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    69
                    &self.points[index],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    70
                    &self.points[index + 1],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    71
                    &self.points[i],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    72
                    &self.points[i - 1],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    73
                ) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    74
                    return true;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    75
                }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    76
            }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    77
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    78
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    79
        false
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    80
    }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    81
}
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    82
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    83
#[cfg(test)]
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    84
#[test]
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    85
fn intersection() {
13942
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    86
    let p1 = Point { x: 0, y: 0 };
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    87
    let p2 = Point { x: 0, y: 10 };
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    88
    let p3 = Point { x: -5, y: 5 };
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    89
    let p4 = Point { x: 5, y: 5 };
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    90
    let p5 = Point { x: 5, y: 16 };
13929
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    91
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    92
    assert!(check_intersect(&p1, &p2, &p3, &p4));
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    93
    assert!(!check_intersect(&p1, &p2, &p3, &p5));
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    94
}