Grasshopper

algorithmic modeling for Rhino

Hello

I bumped into something funny while I was trying to run a little VB script.
I don't know VB script to good so I don't know if there is a better way but I calculated the Angle between two Vectors like this :

W = Acos((V1.x * DIR.x + V1.y * Dir.y + V1.z * Dir.z) / (V1.length * Dir.Length))

Acos( dot product(V1,V2) / ( LengthV1*LengthV2 ) )

However here is a bit of the output I get :

Acos(1) =0
Acos(1) =NaN

Anyboody any Idea ?

Views: 4082

Replies to This Discussion

Hi Richard,

when dealing with floating point numbers note that they may carry noise in the low digits. If you type 1.0 as a constant it will be proper, but if 1.0 is the result of an expression, it may well be 0.999999999999984 or 1.000000000000013. Acos is only defined for the strict -1 to +1 range, so if your noise nudges the number just over the 1.0 boundary, you'll get a NaN.

If you're dealing with ACos and you're liable to get numbers slighty over 1.0, I recommend adding a clipping operation prior to the ACos:

If (p > 1.0) Then p = 1.0

--
David Rutten
david@mcneel.com
Poprad, Slovakia
THANKS,
thought that it would be something like this, ...

Is this in C# as well ?
Yes, there is no difference between VB and C# once it has been compiled into MSIL. So in C# you'd get:

if (p > 1.0) { p = 1.0; }

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Sorry this was an absolutly unthoughtful question....

cos(of what ever) is between -1,1 .....

but thanks

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service