From 323fca77ae81ff3a35c87337ff0b748a8725e919 Mon Sep 17 00:00:00 2001 From: benfry Date: Sat, 4 Sep 2010 14:00:42 +0000 Subject: [PATCH] fix a special case causing NaN in PVector.angleBetween() (issue #340) --- core/src/processing/core/PVector.java | 11 ++++++++++- core/todo.txt | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/core/src/processing/core/PVector.java b/core/src/processing/core/PVector.java index 1fb4f1f76..59db99def 100644 --- a/core/src/processing/core/PVector.java +++ b/core/src/processing/core/PVector.java @@ -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); } diff --git a/core/todo.txt b/core/todo.txt index 639269b7c..0eecae3cf 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -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).