-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomputation.js
87 lines (83 loc) · 2.58 KB
/
computation.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
75
76
77
78
79
80
81
82
83
84
85
86
87
// @ts-check
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
getDistance(p) {
return Math.sqrt(
(this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y)
);
}
}
var minDistance = 5; // The min distance between 2 points (we get from mouse drag)
var path;
// approx. equal
function equal(a, b) {
return Math.abs(a - b) < 3 * minDistance;
}
// used for sorting
function compareFunction(a, b) {
return a.distance - b.distance;
}
function granulatePoints(points) {
var granulatedPoints = [];
if (points.length == 0) {
return granulatedPoints;
}
granulatedPoints.push(points[0]);
for (var i = 1; i < points.length; i++) {
var currentDistance = points[i].getDistance(points[i - 1]);
if (currentDistance > minDistance) {
var cosTheta = (points[i].x - points[i - 1].x) / currentDistance;
var sinTheta = (points[i].y - points[i - 1].y) / currentDistance;
for (var j = 5; j < currentDistance; j += 5) {
granulatedPoints.push(
new Point(points[i].x + j * cosTheta, points[i].y + j * sinTheta)
);
}
}
granulatedPoints.push(points[i]);
}
return granulatedPoints;
}
function computeSquare(points) {
/*
For a pair of points, they can be opposite ends of a square give we have 2 other points as diagonal. Coordinates of those points can be determined directly. Now the problem remains is to determine whether those points lie on the curve?
Steps :
1. Granulate the path (points), into points which are tool.minDistance apart
2. for each pair of points, check whether they can be opposite ends of diagonal
*/
console.log("granulating");
var granulatedPoints = granulatePoints(points);
console.log("finding square");
for (var i = 0; i < granulatedPoints.length; i++) {
for (var j = i + 1; j < granulatedPoints.length; j++) {
var a = granulatedPoints[i];
var c = granulatedPoints[j];
var b = new Point(
(a.x + c.x + c.y - a.y) / 2,
(a.y + c.y + a.x - c.x) / 2
);
var d = new Point(
(a.x + c.x + a.y - c.y) / 2,
(a.y + c.y + c.x - a.x) / 2
);
var diagonalLength = a.getDistance(c);
var foundB = false;
var foundD = false;
for (var k = 0; k < granulatedPoints.length; k++) {
if (granulatedPoints[k].getDistance(b) / diagonalLength < 0.01) {
foundB = true;
}
if (granulatedPoints[k].getDistance(d) / diagonalLength < 0.01) {
foundD = true;
}
}
if (foundB && foundD) {
return [a, b, c, d];
}
}
}
return null;
}