From 72eac64d07af484314745fd0e9466e0f94915684 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Sun, 30 Dec 2012 22:56:34 -0800 Subject: [PATCH] Explicitly bounds-check in angle_between() This avoids introducing NaNs due to numerical error while measuring extreme angles -- angles near 0 or pi radians. --- nt3d.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/nt3d.js b/nt3d.js index 9eee504..0389946 100644 --- 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 - 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/ -- 2.44.1