Comparing version 0.2.0 to 0.2.1
/** | ||
* @license Angles.js v0.2.0 08/04/2016 | ||
* @license Angles.js v0.2.1 08/04/2016 | ||
* | ||
@@ -12,8 +12,8 @@ * Copyright (c) 2015, Robert Eisele (robert@xarg.org) | ||
var TAU = 2 * Math.PI; | ||
var EPS = 1e-15; | ||
const TAU = 2 * Math.PI; | ||
const EPS = 1e-15; | ||
var DIRECTIONS = [ | ||
"N", "NE", "E", "SE", "S", "SW", "W", "NW" | ||
]; | ||
// const DIRECTIONS = ["N", "E", "S", "W"]; | ||
const DIRECTIONS = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]; | ||
// const DIRECTIONS = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]; | ||
@@ -31,3 +31,3 @@ /** | ||
var Angles = { | ||
const Angles = { | ||
'SCALE': 360, | ||
@@ -42,4 +42,4 @@ /** | ||
var c = this['SCALE']; | ||
var h = c / 2; | ||
const c = this['SCALE']; | ||
const h = c / 2; | ||
@@ -56,3 +56,3 @@ return mod(n + h, c) - h; | ||
var c = this['SCALE']; | ||
const c = this['SCALE']; | ||
@@ -70,7 +70,9 @@ return mod(n, c); | ||
var z = from - to; | ||
const z = from - to; | ||
// mod(-z, 360) < mod(z, 360) <=> mod(z + 180, 360) < 180 , for all z \ 180 | ||
// if (mod(-z, 360) < mod(z, 360)) { | ||
if (this['normalizeHalf'](z) < 0) { | ||
if (from === to) { | ||
return 0; | ||
// if (mod(-z, 360) < mod(z, 360)) { | ||
} else if (this['normalizeHalf'](z) < 0) { | ||
return -1; // Left | ||
@@ -91,3 +93,3 @@ } else { | ||
var c = this['SCALE']; | ||
const c = this['SCALE']; | ||
n = mod(n, c); | ||
@@ -120,4 +122,4 @@ a = mod(a, c); | ||
var m = this['SCALE']; | ||
var h = m / 2; | ||
const m = this['SCALE']; | ||
const h = m / 2; | ||
@@ -127,3 +129,3 @@ // One-Liner: | ||
var diff = this['normalizeHalf'](a - b); | ||
let diff = this['normalizeHalf'](a - b); | ||
@@ -174,4 +176,4 @@ if (diff > h) | ||
var s = this['SCALE']; | ||
var angle = (1 + Math.acos(cos) / TAU) * s; | ||
const s = this['SCALE']; | ||
let angle = (1 + Math.acos(cos) / TAU) * s; | ||
@@ -192,4 +194,4 @@ if (sin < 0) { | ||
var s = this['SCALE']; | ||
var angle = (TAU + Math.atan2(p2[1] - p1[1], p2[0] - p1[0])) % TAU; | ||
const s = this['SCALE']; | ||
const angle = (TAU + Math.atan2(p2[1] - p1[1], p2[0] - p1[0])) % TAU; | ||
@@ -209,3 +211,3 @@ return angle / TAU * s; | ||
var s = this['SCALE']; | ||
const s = this['SCALE']; | ||
@@ -232,3 +234,3 @@ if (k === undefined) | ||
var phi = (Math.atan2(y, x) + TAU) / TAU; | ||
const phi = (Math.atan2(y, x) + TAU) / TAU; | ||
@@ -244,12 +246,18 @@ if (Math.abs(phi * s % (s / k)) < EPS) { | ||
* | ||
* @param {number} angle | ||
* @param {number} course | ||
* @returns {string} | ||
*/ | ||
'compass': function(angle) { | ||
'compass': function(course) { | ||
var s = this['SCALE']; | ||
var k = DIRECTIONS.length; | ||
// 0° = N | ||
// 90° = E | ||
// 180° = S | ||
// 270° = W | ||
var dir = Math.round((angle / s) * k); | ||
const s = this['SCALE']; | ||
const k = DIRECTIONS.length; | ||
// floor((2ck + s) / (2s)) = round((c / s) * k) | ||
const dir = Math.round(course / s * k); | ||
return DIRECTIONS[mod(dir, k)]; | ||
@@ -268,5 +276,5 @@ }, | ||
var c = this['SCALE']; | ||
a = mod(a, c); | ||
b = mod(b, c); | ||
const s = this['SCALE']; | ||
a = mod(a, s); | ||
b = mod(b, s); | ||
@@ -276,14 +284,11 @@ if (a === b) | ||
if (dir === 1) { | ||
if (a < b) | ||
return mod(a - p * (a - b + c), c); | ||
else | ||
return mod(a - p * (a - b), c); | ||
} else { | ||
// dir becomes an offset if we have to add a full revolution (=scale) | ||
if (!dir) | ||
dir =-s; | ||
else if ((dir === 1) === (a < b)) | ||
dir*= s; | ||
else | ||
dir = 0; | ||
if (a < b) | ||
return mod(a + p * (b - a), c); | ||
else | ||
return mod(a + p * (b - a + c), c); | ||
} | ||
return mod(a + p * (b - a - dir), s); | ||
} | ||
@@ -297,3 +302,6 @@ }; | ||
} else if (typeof exports === "object") { | ||
module["exports"] = Angles; | ||
Object.defineProperty(exports, "__esModule", {'value': true}); | ||
module['exports'] = Angles; | ||
exports['Angles'] = Angles; | ||
exports['default'] = Angles; | ||
} else { | ||
@@ -300,0 +308,0 @@ root['Angles'] = Angles; |
/* | ||
Angles.js v0.2.0 08/04/2016 | ||
Angles.js v0.2.1 08/04/2016 | ||
Copyright (c) 2015, Robert Eisele (robert@xarg.org) | ||
Dual licensed under the MIT or GPL Version 2 licenses. | ||
Copyright (c) 2015, Robert Eisele (robert@xarg.org) | ||
Dual licensed under the MIT or GPL Version 2 licenses. | ||
*/ | ||
'use strict';(function(l){function f(a,b){return(a%b+b)%b}var g=2*Math.PI,k="N NE E SE S SW W NW".split(" "),h={SCALE:360,normalizeHalf:function(a){var b=this.SCALE,c=b/2;return f(a+c,b)-c},normalize:function(a){return f(a,this.SCALE)},shortestDirection:function(a,b){return 0>this.normalizeHalf(a-b)?-1:1},between:function(a,b,c){var e=this.SCALE;a=f(a,e);b=f(b,e);c=f(c,e);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, | ||
e=c/2,d=this.normalizeHalf(a-b);d>e&&(d=d-c);return Math.abs(d)},toRad:function(a){return a/this.SCALE*g},toDeg:function(a){return a/this.SCALE*360},toGon:function(a){return a/this.SCALE*400},fromSinCos:function(a,b){var c=this.SCALE,e=(1+Math.acos(b)/g)*c;0>a&&(e=c-e);return f(e,c)},fromSlope:function(a,b){return(g+Math.atan2(b[1]-a[1],b[0]-a[0]))%g/g*this.SCALE},quadrant:function(a,b,c,e){var d=this.SCALE;void 0===c&&(c=4);void 0===e&&(e=0);a=(Math.atan2(b,a)+g)/g;return 1E-15>Math.abs(a*d%(d/c))? | ||
0:1+f(Math.floor(c*e/d+c*a),c)},compass:function(a){var b=k.length;return k[f(Math.round(a/this.SCALE*b),b)]},lerp:function(a,b,c,e){var d=this.SCALE;a=f(a,d);b=f(b,d);return a===b?a:1===e?a<b?f(a-c*(a-b+d),d):f(a-c*(a-b),d):a<b?f(a+c*(b-a),d):f(a+c*(b-a+d),d)}};"function"===typeof define&&define.amd?define([],function(){return h}):"object"===typeof exports?module.exports=h:l.Angles=h})(this); | ||
(function(l){function f(a,b){return(a%b+b)%b}var g=2*Math.PI,k="N NE E SE S SW W NW".split(" "),h={SCALE:360,normalizeHalf:function(a){var b=this.SCALE,c=b/2;return f(a+c,b)-c},normalize:function(a){return f(a,this.SCALE)},shortestDirection:function(a,b){return a===b?0:0>this.normalizeHalf(a-b)?-1:1},between:function(a,b,c){var d=this.SCALE;a=f(a,d);b=f(b,d);c=f(c,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=c/2, | ||
e=this.normalizeHalf(a-b);e>d&&(e-=c);return Math.abs(e)},toRad:function(a){return a/this.SCALE*g},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)/g)*c;0>a&&(d=c-d);return f(d,c)},fromSlope:function(a,b){return(g+Math.atan2(b[1]-a[1],b[0]-a[0]))%g/g*this.SCALE},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)+g)/g;return 1E-15>Math.abs(a*e%(e/c))?0:1+ | ||
f(Math.floor(c*d/e+c*a),c)},compass:function(a){var b=k.length;return k[f(Math.round(a/this.SCALE*b),b)]},lerp:function(a,b,c,d){var e=this.SCALE;a=f(a,e);b=f(b,e);return a===b?a:f(a+c*(b-a-(d?1===d===a<b?d*e:0:-e)),e)}};"function"===typeof define&&define.amd?define([],function(){return h}):"object"===typeof exports?(Object.defineProperty(exports,"__esModule",{value:!0}),module.exports=h,exports.Angles=h,exports["default"]=h):l.Angles=h})(this); |
{ | ||
"name": "angle", | ||
"main": "angles.js", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"homepage": "https://github.com/infusion/Angles.js", | ||
@@ -10,2 +10,7 @@ "description": "A function collection for working with angles", | ||
], | ||
"moduleType": [ | ||
"amd", | ||
"globals", | ||
"node" | ||
], | ||
"authors": [ | ||
@@ -12,0 +17,0 @@ "Robert Eisele <robert@xarg.org> (http://www.xarg.org/)" |
{ | ||
"name": "angles", | ||
"title": "angles.js", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"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", "lerp", "radians", "direction", "geo", "compass", "cardinal", "slope", "angle", "angles", "angular", "between"], | ||
"keywords": [ | ||
"degree", | ||
"degrees", | ||
"convert", | ||
"lerp", | ||
"radians", | ||
"direction", | ||
"geo", | ||
"compass", | ||
"cardinal", | ||
"slope", | ||
"angle", | ||
"angles", | ||
"angular", | ||
"between" | ||
], | ||
"author": "Robert Eisele <robert@xarg.org> (http://www.xarg.org/)", | ||
@@ -10,0 +25,0 @@ "main": "angles", |
@@ -71,3 +71,3 @@ # Angles.js | ||
lerp(a, b, p[, dir=1]) | ||
lerp(a, b, p[, dir=-1]) | ||
--- | ||
@@ -94,3 +94,3 @@ Calculates the linear interpolation of two angles | ||
--- | ||
Calculates the angle given by two points (2 element arrays) | ||
Calculates the angle between the x-axis and the line formed by two points. | ||
@@ -107,3 +107,3 @@ fromSinCos(sin, cos) | ||
--- | ||
Translates the angle to a point of the compass ("N", "NE", "E", "SE", "S", "SW", "W", "NW"). If you want to want to have the major directions only, remove every second element from the array `DIRECTIONS`. | ||
Translates the angle to a point of the compass ("N", "NE", "E", "SE", "S", "SW", "W", "NW") in the normal windrose way (N=0, E=90, S=180, W=270). If you want to want to have the major directions only, remove every second element from the array `DIRECTIONS`. | ||
@@ -120,3 +120,3 @@ Installation | ||
``` | ||
npm install --save angles | ||
npm install angles | ||
``` | ||
@@ -160,3 +160,3 @@ | ||
=== | ||
Copyright (c) 2016, Robert Eisele (robert@xarg.org) | ||
Copyright (c) 2016, [Robert Eisele](http://www.xarg.org/) | ||
Dual licensed under the MIT or GPL Version 2 licenses. |
@@ -33,2 +33,3 @@ | ||
{m: 'shortestDirection', p: [60, 50], r: 1, s: 360}, | ||
{m: 'shortestDirection', p: [50, 50], r: 0, s: 360}, | ||
{m: 'shortestDirection', p: [60 + 360 * 3, 50 + 360 * 7], r: 1, s: 360}, | ||
@@ -35,0 +36,0 @@ {m: 'shortestDirection', p: [60 - 360 * 3, 50 - 360 * 7], r: 1, s: 360}, |
19866
425