-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathoverlap.ts
More file actions
166 lines (152 loc) · 4.14 KB
/
Copy pathoverlap.ts
File metadata and controls
166 lines (152 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// =============================================================================
// Euclid.js | Overlap utilities
// (c) Mathigon
// =============================================================================
import {Circle} from './circle';
import {intersections} from './intersection';
import {Line, Segment} from './line';
import {Point} from './point';
import {Polygon} from './polygon';
import {GeoElement, GeoShape} from './utilities';
/** Whether two GeoElements overlap */
export function overlap(a: GeoShape | Point | GeoElement, b: GeoShape | Point | GeoElement) {
if (isGeoShape(a)) {
if (isGeoShape(b)) {
return geoShapeOverlap(a, b);
} else {
return contains(a, b);
}
}
if (a instanceof Point) {
if (b instanceof Point) {
return Point.equals(a, b);
}
if (isGeoShape(b)) {
return contains(b, a);
}
}
return false;
}
export function contains(a: GeoShape, b: GeoElement) {
if (b instanceof Point) {
return a.contains(b);
} else {
return false;
}
}
function geoShapeOverlap(a: GeoShape, b: GeoShape) {
if (a instanceof Polygon) {
if (b instanceof Polygon) {
for (const bEdge of b.edges) {
for (const aEdge of a.edges) {
if (lineLineOverlap(bEdge, aEdge)) return true;
}
}
}
if (b instanceof Line) {
for (const edge of a.edges) {
if (lineLineOverlap(edge, b)) return true;
}
}
if (b instanceof Circle) {
return polygonCircleOverlap(a, b);
}
}
if (a instanceof Line) {
if (b instanceof Polygon) {
for (const edge of b.edges) {
if (lineLineOverlap(edge, a)) return true;
}
}
if (b instanceof Line) {
return lineLineOverlap(a, b);
}
if (b instanceof Circle) {
return b.intersect(a);
}
}
if (a instanceof Circle) {
if (b instanceof Polygon) {
return polygonCircleOverlap(b, a);
}
if (b instanceof Line) {
return a.intersect(b);
}
if (b instanceof Circle) {
return circleCircleOverlap(a, b);
}
}
// TODO: Handle others
return false;
}
function circleCircleOverlap(a: Circle, b: Circle) {
return Point.distance(a.c, b.c) < a.r + b.r;
}
function polygonCircleOverlap(poly: Polygon, circ: Circle) {
for (const edge of poly.edges) {
if (circ.intersect(edge)) return true;
}
return false;
}
function lineLineOverlap<A extends Line, B extends Line>(a: A, b: B): boolean {
if (a instanceof Segment) {
if (b instanceof Segment) {
return segmentSegmentOverlap(a, b);
} else {
return segmentLineOverlap(a, b);
}
} else if (b instanceof Segment) {
return segmentLineOverlap(b, a);
} else if (a.equals(b)) {
return true;
} else {
return intersections(a, b).length > 0;
}
// TODO: handle rays
}
function segmentLineOverlap(a: Segment, b: Line) {
const p1 = b.project(a.p1);
const p2 = b.project(a.p2);
const s = new Segment(p1, p2);
return segmentSegmentOverlap(a, s);
}
function segmentSegmentOverlap(a: Segment, b: Segment) {
const o1 = getOrientation(a.p1, a.p2, b.p1);
const o2 = getOrientation(a.p1, a.p2, b.p2);
const o3 = getOrientation(b.p1, b.p2, a.p1);
const o4 = getOrientation(b.p1, b.p2, a.p2);
if (o1 !== o2 && o3 !== o4) {
return true;
} else if (o1 === 'collinear' && a.contains(b.p1)) {
return true;
} else if (o2 === 'collinear' && a.contains(b.p2)) {
return true;
} else if (o3 === 'collinear' && b.contains(a.p1)) {
return true;
} else if (o4 === 'collinear' && b.contains(a.p2)) {
return true;
} else {
return false;
}
}
function isGeoShape(o: GeoElement | GeoShape): o is GeoShape {
if ('project' in o && typeof o.project === 'function') {
return true;
}
return false;
}
type Orientation =
'clockwise' |
'counterclockwise' |
'collinear';
// Based on: https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
function getOrientation(a: Point, b: Point, c: Point): Orientation {
const val = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
if (val === 0) {
return 'collinear';
} else if (val > 0) {
return 'clockwise';
} else {
return 'counterclockwise';
}
}