Vector3d.VectorAngle Method implementation

rhino
grasshopper

#1

This might be an esoteric question, but I wanted to know what the exact implementation is for the Rhino.Geometry.Vector3d.VectorAngle(Vector3d, Vector3d) method. I know there are a number of ways of calculating the angle between 3d vectors (using arccos or atan2). In 3d space, my vectors are based on a series of 3 points along a curve (so the first vector is V01, and the second is V12).

Based on testing in Grasshopper, I see that the angle is restricted to the 0 to pi range. I’m implementing a similar method in C++ program to try and replicate some results from a grasshopper script, so I’d like to get an implementation as close to the grasshopper definition as possible.


(Dale Fugier) #2

Hi @MClare,

Here you go:

public static double VectorAngle(Vector3d a, Vector3d b)
{
  if (!a.Unitize() || !b.Unitize())
    return RhinoMath.UnsetValue;

  // compute dot product
  double dot = a.m_x * b.m_x + a.m_y * b.m_y + a.m_z * b.m_z;
  // remove any "noise"
  if (dot > 1.0) dot = 1.0;
  if (dot < -1.0) dot = -1.0;
  double radians = Math.Acos(dot);
  return radians;
}

– Dale