Comparing version 0.0.1 to 0.0.2
/** | ||
* @license Angles.js v1.0.0 08/04/2016 | ||
* @license Angles.js v0.0.2 08/04/2016 | ||
* | ||
@@ -11,5 +11,10 @@ * Copyright (c) 2015, Robert Eisele (robert@xarg.org) | ||
'use strict'; | ||
var TAU = 2 * Math.PI; | ||
var EPS = 1e-15; | ||
var DIRECTIONS = [ | ||
"N", "NE", "E", "SE", "S", "SW", "W", "NW" | ||
]; | ||
var Angles = { | ||
@@ -159,2 +164,76 @@ 'SCALE': 360, | ||
}, | ||
/** | ||
* What is the angle of two points making a line | ||
* | ||
* @param {Array} p1 | ||
* @param {Array} p2 | ||
* @returns {number} | ||
*/ | ||
'fromSlope': function(p1, p2) { | ||
var s = this['SCALE']; | ||
var angle = (1 + Math.atan((p2[1] - p1[1]) / (p2[0] - p1[0])) / TAU) * s; | ||
return (angle % s + s) % s; | ||
}, | ||
/** | ||
* Returns the quadrant | ||
* | ||
* @param {number} x The point x-coordinate | ||
* @param {number} y The point y-coordinate | ||
* @param {number=} k The optional number of regions in the coordinate-system | ||
* @param {number=} shift An optional angle to rotate the coordinate system | ||
* @returns {number} | ||
*/ | ||
'quadrant': function(x, y, k, shift) { | ||
var s = this['SCALE']; | ||
if (k === undefined) | ||
k = 4; // How many regions? 4 = quadrant, 8 = octant, ... | ||
if (shift === undefined) | ||
shift = 0; // Rotate the coordinate system by shift° (positiv = counter-clockwise) | ||
/* shift = PI / k, k = 4: | ||
* I) 45-135 | ||
* II) 135-225 | ||
* III) 225-315 | ||
* IV) 315-360 | ||
*/ | ||
/* shift = 0, k = 4: | ||
* I) 0-90 | ||
* II) 90-180 | ||
* III) 180-270 | ||
* IV) 270-360 | ||
*/ | ||
var phi = (Math.atan2(y, x) + TAU) / TAU; | ||
var tmp = phi * s % (s / k); | ||
if (Math.abs(tmp) < EPS) { | ||
return 0; | ||
} | ||
var tmp = Math.floor(k * shift / s + k * phi); | ||
return 1 + (tmp % k + k) % k; | ||
}, | ||
/** | ||
* Calculates the compass direction of the given angle | ||
* | ||
* @param {number} angle | ||
* @returns {string} | ||
*/ | ||
'compass': function(angle) { | ||
var s = this['SCALE']; | ||
var k = DIRECTIONS.length; | ||
var dir = Math.round((angle / s) * k); | ||
return DIRECTIONS[(dir % k + k) % k]; | ||
} | ||
}; | ||
@@ -161,0 +240,0 @@ |
/* | ||
Angles.js v1.0.0 08/04/2016 | ||
Angles.js v0.0.2 08/04/2016 | ||
@@ -7,3 +7,4 @@ Copyright (c) 2015, Robert Eisele (robert@xarg.org) | ||
*/ | ||
'use strict';(function(g){var f=2*Math.PI,e={SCALE:360,normalizeHalf:function(a){var b=this.SCALE,d=this.SCALE/2;return(a%b+b+d)%b-d},normalize:function(a){var b=this.SCALE;return(a%b+b)%b},shortestDirection:function(a,b){return 0>this.normalizeHalf(a-b)?-1:1},between:function(a,b,d){var c=this.SCALE;a=(a%c+c)%c;b=(b%c+c)%c;d=(d%c+c)%c;return b<d?b<=a&&a<=d:b<=a||a<=d},diff:function(a,b){return Math.abs(b-a)%this.SCALE},distance:function(a,b){var d=this.SCALE,c=this.normalizeHalf(a-b);c>d/2&&(c=c- | ||
d);return Math.abs(c)},toRad:function(a){return a/this.SCALE*f},toDeg:function(a){return a/this.SCALE*360},toGon:function(a){return a/this.SCALE*400},fromSinCos:function(a,b){var d=this.SCALE,c=(1+Math.acos(b)/f)*d;0>a&&(c=d-c);return(c%d+d)%d}};"function"===typeof define&&define.amd?define([],function(){return e}):"object"===typeof exports?module.exports=e:g.Angles=e})(this); | ||
'use strict';(function(h){var f=2*Math.PI,g="N NE E SE S SW W NW".split(" "),e={SCALE:360,normalizeHalf:function(a){var b=this.SCALE,c=this.SCALE/2;return(a%b+b+c)%b-c},normalize:function(a){var b=this.SCALE;return(a%b+b)%b},shortestDirection:function(a,b){return 0>this.normalizeHalf(a-b)?-1:1},between:function(a,b,c){var d=this.SCALE;a=(a%d+d)%d;b=(b%d+d)%d;c=(c%d+d)%d;return b<c?b<=a&&a<=c:b<=a||a<=c},diff:function(a,b){return Math.abs(b-a)%this.SCALE},distance:function(a,b){var c=this.SCALE,d= | ||
this.normalizeHalf(a-b);d>c/2&&(d=d-c);return Math.abs(d)},toRad:function(a){return a/this.SCALE*f},toDeg:function(a){return a/this.SCALE*360},toGon:function(a){return a/this.SCALE*400},fromSinCos:function(a,b){var c=this.SCALE,d=(1+Math.acos(b)/f)*c;0>a&&(d=c-d);return(d%c+c)%c},fromSlope:function(a,b){var c=this.SCALE;return((1+Math.atan((b[1]-a[1])/(b[0]-a[0]))/f)*c%c+c)%c},quadrant:function(a,b,c,d){var e=this.SCALE;void 0===c&&(c=4);void 0===d&&(d=0);a=(Math.atan2(b,a)+f)/f;return 1E-15>Math.abs(a* | ||
e%(e/c))?0:1+(Math.floor(c*d/e+c*a)%c+c)%c},compass:function(a){var b=g.length;return g[(Math.round(a/this.SCALE*b)%b+b)%b]}};"function"===typeof define&&define.amd?define([],function(){return e}):"object"===typeof exports?module.exports=e:h.Angles=e})(this); |
{ | ||
"name": "angle", | ||
"main": "angles.js", | ||
"version": "1.0.0", | ||
"version": "0.0.2", | ||
"homepage": "https://github.com/infusion/Angles.js", | ||
"description": "A function collection for working with angles", | ||
"keywords": [ | ||
"degree", "degrees", "convert", "radians", "direction", "angle", "angles", "angular", "between" | ||
"degree", "degrees", "convert", "radians", "direction", "geo", "compass", "cardinal", "slope", "angle", "angles", "angular", "between" | ||
], | ||
@@ -10,0 +10,0 @@ "authors": [ |
{ | ||
"name": "angles", | ||
"title": "angles.js", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"homepage": "https://github.com/infusion/Angles.js", | ||
"bugs": "https://github.com/infusion/Angles.js/issues", | ||
"description": "A function collection for working with angles", | ||
"keywords": ["degree", "degrees", "convert", "radians", "direction", "angle", "angles", "angular", "between"], | ||
"keywords": ["degree", "degrees", "convert", "radians", "direction", "geo", "compass", "cardinal", "slope", "angle", "angles", "angular", "between"], | ||
"author": "Robert Eisele <robert@xarg.org> (http://www.xarg.org/)", | ||
@@ -10,0 +10,0 @@ "main": "angles", |
@@ -37,7 +37,7 @@ # Angles.js | ||
--- | ||
Normalizes an angle to be in the interval [-180, 180) | ||
Normalizes an angle to be in the interval [-180, 180), if `SCALE` is 360 or [-π, π) if `SCALE` is 2π. | ||
normalize(n) | ||
--- | ||
Normalizes an angle to be in the interval [0, 360) | ||
Normalizes an angle to be in the interval [0, 360), if `SCALE` is 360 or [0, 2π) if `SCALE` is 2π. | ||
@@ -72,2 +72,6 @@ shortestDirection(from, to) | ||
fromSlope(p1, p2) | ||
--- | ||
Calculates the angle given by two points (2 element arrays) | ||
fromSinCos(sin, cos) | ||
@@ -77,2 +81,6 @@ --- | ||
quadrant(x, y[k=4[, shift=0]]) | ||
--- | ||
Calculates the quadrant (with `k=4`, or octant with `k=8`) in which a point with coordinates `x,y` falls. Optionally, the coordinate system can be rotated with the `shift` parameter, which follows the `SCALE`-attribute. A positive value rotates counter-clockwise. | ||
Installation | ||
@@ -79,0 +87,0 @@ === |
@@ -27,2 +27,6 @@ | ||
{m: angles.normalizeHalf, p: [-190], r: 170, s: 360}, | ||
{m: angles.fromSlope, p: [[1, 1], [5, 10]], r: 66.03751102542185, s: 360}, | ||
{m: angles.fromSlope, p: [[124, 8984], [234, 10322]], r: 85.30015415271288, s: 360}, | ||
{m: angles.fromSlope, p: [[424, 8984], [234, 10322]], r: 278.0821360367614, s: 360}, | ||
{m: angles.fromSlope, p: [[345, -78445], [3475890, 8495]], r: 1.4329425927144825, s: 360}, | ||
{m: angles.shortestDirection, p: [50, 60], r: -1, s: 360}, | ||
@@ -38,2 +42,43 @@ {m: angles.shortestDirection, p: [60, 50], r: 1, s: 360}, | ||
for (var i = 0; i <= 360; i += 2) { | ||
if (i % 90 === 0) | ||
kl = 0; | ||
else if (i < 90) | ||
kl = 1; | ||
else if (i < 180) | ||
kl = 2; | ||
else if (i < 270) | ||
kl = 3; | ||
else | ||
kl = 4; | ||
if (i < 22.5 + 45 * 0) { | ||
dir = 'N'; | ||
} else if (i < 22.5 + 45 * 1) { | ||
dir = 'NE'; | ||
} else if (i < 22.5 + 45 * 2) { | ||
dir = 'E'; | ||
} else if (i < 22.5 + 45 * 3) { | ||
dir = 'SE'; | ||
} else if (i < 22.5 + 45 * 4) { | ||
dir = 'S'; | ||
} else if (i < 22.5 + 45 * 5) { | ||
dir = 'SW'; | ||
} else if (i < 22.5 + 45 * 6) { | ||
dir = 'W'; | ||
} else if (i < 22.5 + 45 * 7) { | ||
dir = 'NW'; | ||
} else { | ||
dir = 'N'; | ||
} | ||
tests.push({m: angles.quadrant, p: [ | ||
Math.cos(i / 180 * Math.PI), | ||
Math.sin(i / 180 * Math.PI) | ||
], r: kl, s: 360, label: 'Quadrant of angle ' + i}); | ||
tests.push({m: angles.compass, p: [i], r: dir, s: 360, label: 'Direction of angle ' + i}); | ||
} | ||
describe('Angles', function() { | ||
@@ -45,7 +90,10 @@ | ||
it('Should work with test #' + (i + 1), function() { | ||
it('Should work with test ' + (tests[i].label || "#" + i), function() { | ||
var c = tests[i]; | ||
angles.SCALE = c.s; | ||
c.m.apply(angles, c.p).should.be.approximately(c.r, 1e-15); | ||
if (typeof c.r === 'string') | ||
c.m.apply(angles, c.p).should.be.equal(c.r); | ||
else | ||
c.m.apply(angles, c.p).should.be.approximately(c.r, 1e-15); | ||
}); | ||
@@ -52,0 +100,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16726
9
362
134