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

AccuRender

Bongo

Brazil r/s

Developer

Flamingo

Flamingo

Penguin

Rhino Blogs

Rhino

Rhino Labs

Search

Languages

Česky

Deutsch

English

Español

Français

Italiano

Polish

日本語

한국어

中文(繁體)

中文(简体)

 
.
DeveloperRhinoScript

Question

I know that I can calculate the angle between two lines using RhinoScript's Angle2 method. But, when I am dealing with 3-D vectors, I always have to calculate the points that define the two lines. Is there a way to determine the angle between two 3-D vectors without having to do this?

Answer

Yes there is. See the following example function.

  ' Description: 
  '  Calculates the angle between two 3-D vectors.
  ' Parameters:
  '   v0 - [in] - the first vector.
  '   v1 - [in] - the second vector.
  ' Returns: 
  '   the angle in degrees.


  Function VectorAngle(v0, v1)


    Dim u0  : u0  = Rhino.VectorUnitize(v0)
    Dim u1  : u1  = Rhino.VectorUnitize(v1)  
    Dim dot : dot = Rhino.VectorDotProduct(u0, u1)


    ' 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
    ElseIf (dot > 1.0) Then
      dot = 1.0
    End If


    VectorAngle = Rhino.ToDegrees(Rhino.ACos(dot))


  End Function
rename · changes · history · subscriptions · lost and found · references · file upload