[BUGFIX] Util math

This commit is contained in:
Martin Geno 2017-03-10 14:39:06 +01:00 committed by Xaver Maierhofer
parent 577f227957
commit f265901036
No known key found for this signature in database
GPG Key ID: 7FDCE23FD2EC9FE8

View File

@ -1,29 +1,31 @@
define(function () { define(function () {
return { var self = {};
distance: function distance(a, b) {
self.distance = function distance(a, b) {
return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2); return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2);
}, };
distancePoint: function distancePoint(a, b) { self.distancePoint = function distancePoint(a, b) {
return Math.sqrt(distance(a, b)); return Math.sqrt(self.distance(a, b));
}, };
distanceLink: function distanceLink(p, a, b) { self.distanceLink = function distanceLink(p, a, b) {
/* http://stackoverflow.com/questions/849211 */ /* http://stackoverflow.com/questions/849211 */
var l2 = distance(a, b); var l2 = self.distance(a, b);
if (l2 === 0) { if (l2 === 0) {
return distance(p, a); return self.distance(p, a);
} }
var t = ((p.x - a.x) * (b.x - a.x) + (p.y - a.y) * (b.y - a.y)) / l2; var t = ((p.x - a.x) * (b.x - a.x) + (p.y - a.y) * (b.y - a.y)) / l2;
if (t < 0) { if (t < 0) {
return distance(p, a); return self.distance(p, a);
} else if (t > 1) { } else if (t > 1) {
return distance(p, b); return self.distance(p, b);
} }
return distancePoint(p, { return self.distancePoint(p, {
x: a.x + t * (b.x - a.x), x: a.x + t * (b.x - a.x),
y: a.y + t * (b.y - a.y) y: a.y + t * (b.y - a.y)
}); });
}
}; };
return self;
}); });