rust/landgen/src/outline.rs
author alfadur
Thu, 18 Oct 2018 06:46:32 +0300
changeset 13959 1fa905aa4cdb
parent 13942 939641cdb94f
permissions -rw-r--r--
move point struct into integral-geometry and use it to refactor a bit
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13942
diff changeset
     1
use integral_geometry::Point;
13929
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     2
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     3
pub struct Outline {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     4
    points: Vec<Point>,
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     5
}
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
     6
13942
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
     7
fn check_intersect(
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
     8
    segment1_start: &Point,
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
     9
    segment1_end: &Point,
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    10
    segment2_start: &Point,
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    11
    segment2_end: &Point,
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    12
) -> bool {
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    13
    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
    14
        - (segment2_end.x - segment2_start.x) * (segment1_end.y - segment1_start.y);
13929
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    15
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    16
    if dm == 0 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    17
        return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    18
    }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    19
13942
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    20
    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
    21
        - (segment2_end.y - segment2_start.y) * (segment1_start.x - segment2_start.x);
13929
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    22
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    23
    if dm > 0 {
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
    } else {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    28
        if (c1 > 0) || (c1 < dm) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    29
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    30
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    31
    }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    32
13942
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    33
    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
    34
        - (segment1_end.y - segment2_start.y) * (segment1_start.x - segment2_start.x);
13929
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    35
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    36
    if dm > 0 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    37
        if (c2 < 0) || (c2 > dm) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    38
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    39
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    40
    } else {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    41
        if (c2 > 0) || (c2 < dm) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    42
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    43
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    44
    }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    45
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    46
    true
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
impl Outline {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    50
    fn check_intersects_self_at_index(&self, index: usize) -> bool {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    51
        if index <= 0 || index > self.points.len() {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    52
            return false;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    53
        }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    54
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    55
        for i in 1..=self.points.len() - 3 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    56
            if i <= index - 1 || i >= index + 2 {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    57
                if i != index - 1 && check_intersect(
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    58
                    &self.points[index],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    59
                    &self.points[index - 1],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    60
                    &self.points[i],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    61
                    &self.points[i - 1],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    62
                ) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    63
                    return true;
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    64
                }
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    65
                if i != index + 2 && check_intersect(
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    66
                    &self.points[index],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    67
                    &self.points[index + 1],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    68
                    &self.points[i],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    69
                    &self.points[i - 1],
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    70
                ) {
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    71
                    return true;
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
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    76
        false
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
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    80
#[cfg(test)]
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    81
#[test]
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    82
fn intersection() {
13942
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    83
    let p1 = Point { x: 0, y: 0 };
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    84
    let p2 = Point { x: 0, y: 10 };
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    85
    let p3 = Point { x: -5, y: 5 };
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    86
    let p4 = Point { x: 5, y: 5 };
939641cdb94f Make parameters meaning more obvious
unc0rr
parents: 13929
diff changeset
    87
    let p5 = Point { x: 5, y: 16 };
13929
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    88
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    89
    assert!(check_intersect(&p1, &p2, &p3, &p4));
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    90
    assert!(!check_intersect(&p1, &p2, &p3, &p5));
9a1f1e8170f2 Start work on landgen rust lib
unc0rr
parents:
diff changeset
    91
}