McNeel Wiki
Calculate the angle between two vectors
edit · print · help · all topics
Main Pages

AccuRender

Bongo

Brazil r/s

Developer

Flamingo

Penguin

Rhino Blogs

Rhino

Search

Languages

Česky

Deutsch

English

Español

Français

Italiano

Polish

日本語

한국어

中文(繁體)

 
.
DeveloperC++, .NET

The following example code demonstrates how to calculate the angle between two 3-D vectors.

C++

  // Description: Calculates the angle between two 3-D vectors.
  // Parameters:
  //  v0 - [in] - the first angle.
  //  v1 - [in] - the second angle.
  // Returns: The angle in radians.
  double ON_3dVectorAngle( ON_3dVector v0, ON_3dVector v1 )
  {
    // Unitize the input vectors
    v0.Unitize();
    v1.Unitize();


    double dot = ON_DotProduct( v0, v1 );


    // Force the dot product of the two input vectors to 
    // fall within the domain for inverse cosine, which 
    // is -1 <= x <= 1. This will prevent runtime 
    // "domain error" math exceptions.
    dot = ( dot < -1.0 ? -1.0 : ( dot > 1.0 ? 1.0 : dot ) );


    return acos( dot );
  }

C# (Rhino 4)

  public static double ON_3dVectorAngle(On3dVector v0, On3dVector v1)
  {
    // Unitize the input vectors
    v0.Unitize();
    v1.Unitize();
    double dot = OnUtil.ON_DotProduct(v0, v1);


    // Force the dot product of the two input vectors to 
    // fall within the domain for inverse cosine, which 
    // is -1 <= x <= 1. This will prevent runtime 
    // "domain error" math exceptions.
    dot = (dot < -1.0 ? -1.0 : (dot > 1.0 ? 1.0 : dot));


    return System.Math.Acos(dot);
  }

VB.NET (Rhino 4)

  Public Shared Function ON_3dVectorAngle(ByVal v0 As On3dVector, ByVal v1 As On3dVector) _
    As Double
    ' Unitize the input vectors
    v0.Unitize()
    v1.Unitize()
    Dim dot As Double = OnUtil.ON_DotProduct(v0, v1)


    ' Force the dot product of the two input vectors to 
    ' fall within the domain for inverse cosine, which 
    ' is -1 <= x <= 1. This will prevent runtime 
    ' "domain error" math exceptions.
    If (dot < -1.0) Then dot = -1.0
    If (dot > 1.0) Then dot = 1.0
    Return System.Math.Acos(dot)
  End Function
rename · changes · history · subscriptions · lost and found · references · file upload