-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbrute.js
74 lines (68 loc) · 1.65 KB
/
brute.js
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
import intersectSegments from './src/intersectSegments';
/**
* This is a brute force solution with O(n^2) performance.
* (`n` is number of segments).
*
* Use this when number of lines is low, and number of intersections
* is high.
*/
export default function brute(lines, options) {
var results = [];
var reportIntersection = (options && options.onFound) ||
defaultIntersectionReporter;
var asyncState;
return {
/**
* Execute brute force of the segment intersection search
*/
run,
/**
* Access to results array. Works only when you use default onFound() handler
*/
results,
/**
* Performs a single step in the brute force algorithm ()
*/
step
}
function step() {
if (!asyncState) {
asyncState = {
i: 0
}
}
var test = lines[asyncState.i];
for (var j = asyncState.i + 1; j < lines.length; ++j) {
var other = lines[j];
var pt = intersectSegments(test, other);
if (pt) {
if (reportIntersection(pt, [test, other])) {
return;
}
}
}
asyncState.i += 1;
return asyncState.i < lines.length;
}
function run() {
for(var i = 0; i < lines.length; ++i) {
var test = lines[i];
for (var j = i + 1; j < lines.length; ++j) {
var other = lines[j];
var pt = intersectSegments(test, other);
if (pt) {
if (reportIntersection(pt, [test, other])) {
return;
}
}
}
}
return results;
}
function defaultIntersectionReporter(p, interior) {
results.push({
point: p,
segments: interior
});
}
}