]> git.scottworley.com Git - nt3d/commitdiff
Explicitly bounds-check in angle_between()
authorScott Worley <ScottWorley@ScottWorley.com>
Mon, 31 Dec 2012 06:56:34 +0000 (22:56 -0800)
committerScott Worley <ScottWorley@ScottWorley.com>
Mon, 31 Dec 2012 06:56:34 +0000 (22:56 -0800)
This avoids introducing NaNs due to numerical error while measuring
extreme angles -- angles near 0 or pi radians.

nt3d.js

diff --git a/nt3d.js b/nt3d.js
index 9eee5045617c0a612bd1eed527f98845e7e89845..038994678f9328de72fca780f4a8b639a58e915b 100644 (file)
--- a/nt3d.js
+++ b/nt3d.js
@@ -259,7 +259,14 @@ nt3d = {
                        point[2] + offset[2]];
        },
        angle_between: function(a, b) { // a and b must be unit vectors
                        point[2] + offset[2]];
        },
        angle_between: function(a, b) { // a and b must be unit vectors
-               return Math.acos(this.dot(a, b));
+               var the_dot = this.dot(a, b);
+               if (the_dot <= -1) {
+                       return Math.PI;
+               }
+               if (the_dot >= 1) {
+                       return 0;
+               }
+               return Math.acos(the_dot);
        },
        rotate_about_origin: function(points, axis, angle) { // axis must be a unit vector
                // From http://inside.mines.edu/~gmurray/ArbitraryAxisRotation/
        },
        rotate_about_origin: function(points, axis, angle) { // axis must be a unit vector
                // From http://inside.mines.edu/~gmurray/ArbitraryAxisRotation/