123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- ;(function() {
-
- "use strict";
- var Biltong = this.Biltong = {};
- var _isa = function(a) { return Object.prototype.toString.call(a) === "[object Array]"; },
- _pointHelper = function(p1, p2, fn) {
- p1 = _isa(p1) ? p1 : [p1.x, p1.y];
- p2 = _isa(p2) ? p2 : [p2.x, p2.y];
- return fn(p1, p2);
- },
-
- _gradient = Biltong.gradient = function(p1, p2) {
- return _pointHelper(p1, p2, function(_p1, _p2) {
- if (_p2[0] == _p1[0])
- return _p2[1] > _p1[1] ? Infinity : -Infinity;
- else if (_p2[1] == _p1[1])
- return _p2[0] > _p1[0] ? 0 : -0;
- else
- return (_p2[1] - _p1[1]) / (_p2[0] - _p1[0]);
- });
- },
-
- _normal = Biltong.normal = function(p1, p2) {
- return -1 / _gradient(p1, p2);
- },
-
- _lineLength = Biltong.lineLength = function(p1, p2) {
- return _pointHelper(p1, p2, function(_p1, _p2) {
- return Math.sqrt(Math.pow(_p2[1] - _p1[1], 2) + Math.pow(_p2[0] - _p1[0], 2));
- });
- },
-
- _quadrant = Biltong.quadrant = function(p1, p2) {
- return _pointHelper(p1, p2, function(_p1, _p2) {
- if (_p2[0] > _p1[0]) {
- return (_p2[1] > _p1[1]) ? 2 : 1;
- }
- else if (_p2[0] == _p1[0]) {
- return _p2[1] > _p1[1] ? 2 : 1;
- }
- else {
- return (_p2[1] > _p1[1]) ? 3 : 4;
- }
- });
- },
-
- _theta = Biltong.theta = function(p1, p2) {
- return _pointHelper(p1, p2, function(_p1, _p2) {
- var m = _gradient(_p1, _p2),
- t = Math.atan(m),
- s = _quadrant(_p1, _p2);
- if ((s == 4 || s== 3)) t += Math.PI;
- if (t < 0) t += (2 * Math.PI);
-
- return t;
- });
- },
-
- _intersects = Biltong.intersects = function(r1, r2) {
- var x1 = r1.x, x2 = r1.x + r1.w, y1 = r1.y, y2 = r1.y + r1.h,
- a1 = r2.x, a2 = r2.x + r2.w, b1 = r2.y, b2 = r2.y + r2.h;
-
- return ( (x1 <= a1 && a1 <= x2) && (y1 <= b1 && b1 <= y2) ) ||
- ( (x1 <= a2 && a2 <= x2) && (y1 <= b1 && b1 <= y2) ) ||
- ( (x1 <= a1 && a1 <= x2) && (y1 <= b2 && b2 <= y2) ) ||
- ( (x1 <= a2 && a1 <= x2) && (y1 <= b2 && b2 <= y2) ) ||
- ( (a1 <= x1 && x1 <= a2) && (b1 <= y1 && y1 <= b2) ) ||
- ( (a1 <= x2 && x2 <= a2) && (b1 <= y1 && y1 <= b2) ) ||
- ( (a1 <= x1 && x1 <= a2) && (b1 <= y2 && y2 <= b2) ) ||
- ( (a1 <= x2 && x1 <= a2) && (b1 <= y2 && y2 <= b2) );
- },
-
- _encloses = Biltong.encloses = function(r1, r2, allowSharedEdges) {
- var x1 = r1.x, x2 = r1.x + r1.w, y1 = r1.y, y2 = r1.y + r1.h,
- a1 = r2.x, a2 = r2.x + r2.w, b1 = r2.y, b2 = r2.y + r2.h,
- c = function(v1, v2, v3, v4) { return allowSharedEdges ? v1 <= v2 && v3>= v4 : v1 < v2 && v3 > v4; };
-
- return c(x1,a1,x2,a2) && c(y1,b1,y2,b2);
- },
- _segmentMultipliers = [null, [1, -1], [1, 1], [-1, 1], [-1, -1] ],
- _inverseSegmentMultipliers = [null, [-1, -1], [-1, 1], [1, 1], [1, -1] ],
-
- _pointOnLine = Biltong.pointOnLine = function(fromPoint, toPoint, distance) {
- var m = _gradient(fromPoint, toPoint),
- s = _quadrant(fromPoint, toPoint),
- segmentMultiplier = distance > 0 ? _segmentMultipliers[s] : _inverseSegmentMultipliers[s],
- theta = Math.atan(m),
- y = Math.abs(distance * Math.sin(theta)) * segmentMultiplier[1],
- x = Math.abs(distance * Math.cos(theta)) * segmentMultiplier[0];
- return { x:fromPoint.x + x, y:fromPoint.y + y };
- },
-
-
- _perpendicularLineTo = Biltong.perpendicularLineTo = function(fromPoint, toPoint, length) {
- var m = _gradient(fromPoint, toPoint),
- theta2 = Math.atan(-1 / m),
- y = length / 2 * Math.sin(theta2),
- x = length / 2 * Math.cos(theta2);
- return [{x:toPoint.x + x, y:toPoint.y + y}, {x:toPoint.x - x, y:toPoint.y - y}];
- };
- }).call(this);
|