2017-02-23 21:32:25 +00:00
|
|
|
define(function () {
|
2017-03-10 13:39:06 +00:00
|
|
|
var self = {};
|
2017-02-23 21:32:25 +00:00
|
|
|
|
2017-03-10 13:39:06 +00:00
|
|
|
self.distance = function distance(a, b) {
|
2017-08-18 18:46:13 +00:00
|
|
|
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
|
2017-03-10 13:39:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.distancePoint = function distancePoint(a, b) {
|
|
|
|
return Math.sqrt(self.distance(a, b));
|
|
|
|
};
|
2017-02-23 21:32:25 +00:00
|
|
|
|
2017-03-10 13:39:06 +00:00
|
|
|
self.distanceLink = function distanceLink(p, a, b) {
|
|
|
|
/* http://stackoverflow.com/questions/849211 */
|
|
|
|
var l2 = self.distance(a, b);
|
|
|
|
if (l2 === 0) {
|
|
|
|
return self.distance(p, a);
|
2017-02-23 21:32:25 +00:00
|
|
|
}
|
2017-03-10 13:39:06 +00:00
|
|
|
var t = ((p.x - a.x) * (b.x - a.x) + (p.y - a.y) * (b.y - a.y)) / l2;
|
|
|
|
if (t < 0) {
|
|
|
|
return self.distance(p, a);
|
|
|
|
} else if (t > 1) {
|
|
|
|
return self.distance(p, b);
|
|
|
|
}
|
|
|
|
return self.distancePoint(p, {
|
|
|
|
x: a.x + t * (b.x - a.x),
|
|
|
|
y: a.y + t * (b.y - a.y)
|
|
|
|
});
|
2017-02-23 21:32:25 +00:00
|
|
|
};
|
2017-03-10 13:39:06 +00:00
|
|
|
|
|
|
|
return self;
|
2017-02-23 21:32:25 +00:00
|
|
|
});
|