[vtkusers] Angle between two vectors

David Cole dlrdave at aol.com
Fri Jan 3 09:52:34 EST 2014


Your "get_angle_new" is about as simple as it gets.

I would do something like this, though:

    double dot = vtkMath::Dot(v1, v2);

     // Expect v1 and v2 to be normalized unit vectors, but sometimes 
the dot product
    // exceeds 1 by just enough to cause errors when calling acos.
    // If it's greater than 1.0, or less than -1.0, clamp it:
    if (dot > 1.0)
      {
      dot = 1.0;
      }
    else if (dot < -1.0)
      {
      dot = -1.0;
      }

    double angle = acos(dot);


HTH,
David C.



More information about the vtkusers mailing list