author | alfadur |
Thu, 18 Oct 2018 06:46:32 +0300 | |
changeset 13938 | 1fa905aa4cdb |
parent 13921 | 939641cdb94f |
permissions | -rw-r--r-- |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13921
diff
changeset
|
1 |
use integral_geometry::Point; |
13908 | 2 |
|
3 |
pub struct Outline { |
|
4 |
points: Vec<Point>, |
|
5 |
} |
|
6 |
||
13921 | 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); |
|
13908 | 15 |
|
16 |
if dm == 0 { |
|
17 |
return false; |
|
18 |
} |
|
19 |
||
13921 | 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); |
|
13908 | 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 |
||
13921 | 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); |
|
13908 | 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() { |
|
13921 | 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 }; |
|
13908 | 88 |
|
89 |
assert!(check_intersect(&p1, &p2, &p3, &p4)); |
|
90 |
assert!(!check_intersect(&p1, &p2, &p3, &p5)); |
|
91 |
} |