diff --git a/js/index.js b/js/index.js index f30380f..621f6cd 100644 --- a/js/index.js +++ b/js/index.js @@ -1,5 +1,5 @@ var recquire_t = require('recquire'); -var recquire = recquire_t('cg', 'index.js', false, true); +var recquire = recquire_t('cg', 'index.js', 'intro.js', 'outro.js', false, true); recquire(__dirname + '/src/', module.exports, -1); \ No newline at end of file diff --git a/js/src/2d/sin_sign.js b/js/src/2d/sin_sign.js index e10a563..53a5ce5 100644 --- a/js/src/2d/sin_sign.js +++ b/js/src/2d/sin_sign.js @@ -1,7 +1,31 @@ +/** + * Computes the cross product of _ab_ and _ac_, + * can be interpreted as the sinus sign in a "right-handed" coordinate system + * (i.e. clockwise angle values). + * + *

+ * Originally implemented as + * + * a[1] * (c[0] - b[0]) + b[1] * (a[0] - c[0]) + c[1] * (b[0] - a[0]) + * + * with + * - 2 add + * - 3 sub + * - 3 mul + * + * Reduced to + * + * (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]) + * + * with + * - 5 sub + * - 2 mul + * + */ var sin_sign = function(a, b, c){ - return a[1] * (c[0] - b[0]) + b[1] * (a[0] - c[0]) + c[1] * (b[0] - a[0]); + return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); }; diff --git a/package.json b/package.json index bac0888..bae6ac8 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "cg", - "version": "0.0.1", + "version": "0.1.0", "description": "cg (computational geometry) algorithms templates for JavaScript", "main": "js/index.js", "dependencies": { - "recquire": "^0.0.0" + "recquire": "~0.1.0" }, "devDependencies": { "qunit": "git://github.com/gotwarlost/node-qunit.git#istanbul-coverage", diff --git a/test/js/src/2d/sin_sign.js b/test/js/src/2d/sin_sign.js index 339a051..a326b4a 100644 --- a/test/js/src/2d/sin_sign.js +++ b/test/js/src/2d/sin_sign.js @@ -5,7 +5,6 @@ test('sin_sign', function (assert) { var p = [ [0, 0], [0, 1], - [0, 2], [1, 0], [1, 1], [1, 2], @@ -15,14 +14,14 @@ test('sin_sign', function (assert) { [-1, 0] ]; - ok(cg.sin_sign(p[0], p[1], p[2]) === 0, '0'); - ok(cg.sin_sign(p[0], p[1], p[3]) < 0, '45'); - ok(cg.sin_sign(p[0], p[1], p[4]) < 0, '90'); - ok(cg.sin_sign(p[0], p[1], p[5]) < 0, '135'); - ok(cg.sin_sign(p[0], p[1], p[6]) === 0, '180'); - ok(cg.sin_sign(p[0], p[1], p[7]) > 0, '225'); - ok(cg.sin_sign(p[0], p[1], p[8]) > 0, '270'); - ok(cg.sin_sign(p[0], p[1], p[9]) > 0, '315'); + ok(cg.sin_sign(p[0], p[1], p[0]) === 0, '0'); + ok(cg.sin_sign(p[0], p[1], p[2]) < 0, '315'); + ok(cg.sin_sign(p[0], p[1], p[3]) < 0, '270'); + ok(cg.sin_sign(p[0], p[1], p[4]) < 0, '225'); + ok(cg.sin_sign(p[0], p[1], p[5]) === 0, '180'); + ok(cg.sin_sign(p[0], p[1], p[6]) > 0, '135'); + ok(cg.sin_sign(p[0], p[1], p[7]) > 0, '90'); + ok(cg.sin_sign(p[0], p[1], p[8]) > 0, '45');