-
Notifications
You must be signed in to change notification settings - Fork 17
/
simplifygeometry-0.0.2.js
132 lines (74 loc) · 2.96 KB
/
simplifygeometry-0.0.2.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
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
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.simplifyGeometry=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var Line = require('./line');
var simplifyGeometry = function(points, tolerance){
var dmax = 0;
var index = 0;
for (var i = 1; i <= points.length - 2; i++){
var d = new Line(points[0], points[points.length - 1]).perpendicularDistance(points[i]);
if (d > dmax){
index = i;
dmax = d;
}
}
if (dmax > tolerance){
var results_one = simplifyGeometry(points.slice(0, index), tolerance);
var results_two = simplifyGeometry(points.slice(index, points.length), tolerance);
var results = results_one.concat(results_two);
}
else if (points.length > 1) {
results = [points[0], points[points.length - 1]];
}
else {
results = [points[0]];
}
return results;
}
module.exports = simplifyGeometry;
},{"./line":2}],2:[function(require,module,exports){
var Line = function(p1, p2){
this.p1 = p1;
this.p2 = p2;
};
Line.prototype.rise = function() {
return this.p2[1] - this.p1[1];
};
Line.prototype.run = function() {
return this.p2[0] - this.p1[0];
};
Line.prototype.slope = function(){
return this.rise() / this.run();
};
Line.prototype.yIntercept = function(){
return this.p1[1] - (this.p1[0] * this.slope(this.p1, this.p2));
};
Line.prototype.isVertical = function() {
return !isFinite(this.slope());
};
Line.prototype.isHorizontal = function() {
return this.p1[1] == this.p2[1];
};
Line.prototype._perpendicularDistanceHorizontal = function(point){
return Math.abs(this.p1[1] - point[1]);
};
Line.prototype._perpendicularDistanceVertical = function(point){
return Math.abs(this.p1[0] - point[0]);
};
Line.prototype._perpendicularDistanceHasSlope = function(point){
var slope = this.slope();
var y_intercept = this.yIntercept();
return Math.abs((slope * point[0]) - point[1] + y_intercept) / Math.sqrt((Math.pow(slope, 2)) + 1);
};
Line.prototype.perpendicularDistance = function(point){
if (this.isVertical()) {
return this._perpendicularDistanceVertical(point);
}
else if (this.isHorizontal()){
return this._perpendicularDistanceHorizontal(point);
}
else {
return this._perpendicularDistanceHasSlope(point);
}
};
module.exports = Line;
},{}]},{},[1])(1)
});