fix a special case causing NaN in PVector.angleBetween() (issue #340)

This commit is contained in:
benfry
2010-09-04 14:00:42 +00:00
parent 54b3811894
commit 323fca77ae
2 changed files with 12 additions and 1 deletions
+10 -1
View File
@@ -537,7 +537,16 @@ public class PVector {
double dot = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
double v1mag = Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
double v2mag = Math.sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z);
return (float) Math.acos(dot / (v1mag * v2mag));
// This should be a number between -1 and 1, since it's "normalized"
double amt = dot / (v1mag * v2mag);
// But if it's not due to rounding error, then we need to fix it
// http://code.google.com/p/processing/issues/detail?id=340
// Otherwise if outside the range, acos() will return NaN
// http://www.cppreference.com/wiki/c/math/acos
if (amt <= -1 || amt >= 1) {
return PConstants.PI;
}
return (float) Math.acos(amt);
}
+2
View File
@@ -2,6 +2,8 @@
X fix background(PImage) for OpenGL
X http://code.google.com/p/processing/issues/detail?id=336
X skip null entries with trim(String[])
X NaN with PVector.angleBetween
X http://code.google.com/p/processing/issues/detail?id=340
Hi all, just thought I'd share something.
For a while now, I've been having unexpected frame rate drops with JOGL when switching into fullscreen-exclusive mode (it halved no matter what I did - resolution switching, reducing how much I drew, etc).