[vtkusers] Angle between two vectors

David Gobbi david.gobbi at gmail.com
Sat Jan 4 11:11:36 EST 2014


It is generally not a good idea to use acos() to compute angles. The
reason is as follows: the derivative of acos(x) approaches infinity as
x approaches either +1 or -1.  Basic error analysis states that for a
function y = f(x), if there is a small error in x, the resulting error
in y will be equal to the error in x multiplied by the derivative of
f.  Still following me?   So acos(dot(v1,v2)) gives poor results when
dot(v1,v2) is close to 1, i.e. when the angle is close to zero.

Angles should be computed with the atan2(y,x) function.  So to compute
the angle between two vectors, you should do the following:

theta = atan2(norm(cross(v1,v2)), dot(v1,v2))

where cross() is the cross product.  If you use this formula, no
clamping is required.  It is robust and well-behaved for any inputs,
and it does not require the vectors to be normalized beforehand.

  David


On Sat, Jan 4, 2014 at 8:33 AM, David Cole <dlrdave at aol.com> wrote:
> I looked a while back and couldn't find any compute angle methods... that's
> why I had the sample code to send along in my first reply.
>
> It sure does seem like it would be a great candidate for an additional
> vtkMath method. I'm sure a contribution to that effect would be accepted if
> you would like to submit a gerrit topic to add such a method.
>
>
> Cheers,
>
> D
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers


More information about the vtkusers mailing list