-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptri.js
49 lines (41 loc) · 1.16 KB
/
ptri.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
importScripts('bower_components/poly2tri/dist/poly2tri.js');
importScripts('bower_components/underscore/underscore.js');
var polygons = [];
var points = [];
function ptri(pointList, canvas) {
var contour = [
new poly2tri.Point(0, 0),
new poly2tri.Point(canvas.width, 0),
new poly2tri.Point(canvas.width, canvas.height),
new poly2tri.Point(0, canvas.height)
];
var swctx = new poly2tri.SweepContext(contour);
// use original points
/*
points = pointList.map(function (point) {
return new poly2tri.Point(point.x, point.y);
});
swctx.addPoints(points);
*/
swctx.addPoints(pointList);
var triangles = swctx.triangulate().getTriangles();
polygons = [];
for (var tri of triangles) {
polygons.push({points: tri.getPoints()});
}
var closest = pointList[100];
var poly = _.find(polygons, function (poly) {
var point = _.find(poly.points, function (point) {
return point == closest;
});
if (point) {
return poly;
}
});
return [polygons, pointList, poly];
}
self.onmessage = function (event) {
var results = ptri(event.data[0], event.data[1]);
self.postMessage(results);
self.close();
};