SketchUcation Premium Membership

 

 

subtract two angle

subtract two angle

Postby macgile » Mon Feb 13, 2012 10:43 pm

Hi all,

I have a strange result when I subtract two angle in radians!!!!

example :
Code: Select all
axe = Geom::Vector3d.new(-0.999782867743517, -0.0208378829670674, 0.0)
up = Geom::Vector3d.new(0.00335712544250249, -0.161071856847676, 0.98693697196954)
axe.normalize!
up.normalize!
# (90 deg) = 1.5707963267948966 in rad
#(Math::PI/2.0) = 90.0 in degrees, 1.5707963267949 in radians

# angle between up-axe = 90.0 in degrees, 1.5707963267949 in radians
angle = (Math::PI/2.0) + (up.angle_between axe)
puts angle.radians  #=> 180 degrees, 3.14159265358979 radians GOOD !!!

angle = (Math::PI/2.0) - (up.angle_between axe)
puts angle.radians  #=> 1.27222187258541e-014 BAD,  2.22044604925031e-016 in radians BAD  


can you help me ?
Last edited by thomthom on Tue Feb 14, 2012 9:52 am, edited 1 time in total.
Reason: Wrapped the code sample in a [code] block
macgile
 
Posts: 33
Joined: Sat Jan 19, 2008 10:59 am

Re: subtract two angle

Postby Dan Rathbun » Mon Feb 13, 2012 10:55 pm

What version Sketchup and build number, are you running ???

The angle_between method was bugged in early v7 releases.

But it was later fixed.
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4102
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: subtract two angle

Postby macgile » Mon Feb 13, 2012 11:06 pm

thank's Dan Rathbun


i have sketchup 8 !!!

it's not angle between, but the subtraction.
because the calculation of the angle is good.
macgile
 
Posts: 33
Joined: Sat Jan 19, 2008 10:59 am

Re: subtract two angle

Postby TIG » Mon Feb 13, 2012 11:29 pm

Why not use 90.degrees etc and return the angle.radians to see what it is in degrees too... skip all of the PI and radian confusion...
Why are you making your axe/up vectors so complex ?
Your figures are off too...
Code: Select all
axe = Geom::Vector3d.new(-0.999782867743517, -0.0208378829670674, 0.0)
Vector3d(-0.999783, -0.0208379, 0)
axe.normalize!
Vector3d(-0.999783, -0.0208379, 0)
up = Geom::Vector3d.new(0.00335712544250249, -0.161071856847676, 0.98693697196954)
Vector3d(0.00335713, -0.161072, 0.986937)
up.normalize!
Vector3d(0.00335713, -0.161072, 0.986937)
90.degrees
1.5707963267949
Math
::PI/2.0
1.5707963267949
up
.angle_between(axe)
1.5707963267949
### So far ALL the same ???
angle = (Math::PI/2.0) - (up.angle_between(axe))
2.22044604925031e-016
### Which is a very tiny number that's ALMOST zero
This is a a simple tolerance issue...
angle==0
false
### BUT to test for tiny values use something like:
### a 'rounding' method - here to 6dp.
angle=(angle*10**6).round.to_f/10**6
angle
==0
true
### This will then trap for these tiny inaccuracies...  

...
Last edited by thomthom on Tue Feb 14, 2012 9:52 am, edited 1 time in total.
Reason: Wrapped the code sample in a [code] block
TIG
User avatar
TIG
Global Moderator
 
Posts: 14011
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: subtract two angle

Postby Dan Rathbun » Mon Feb 13, 2012 11:30 pm

It's a tiny tiny wee little smidgen Floating Point Error.

Round your numbers off to a reasonable number of decimal places.

Your inputting double precision numbers, but Sketchup's internal tolerance for geometry is 0.001 inch.

You can use the Length class to test if two numbers are equal within Sketchup's tolerance, even though they are angles: (your not changing the objects, your creating two new Length objects for comparison.)

(Math::PI/2.0).to_l == up.angle_between(axe).to_l
>> true
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4102
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: subtract two angle

Postby macgile » Tue Feb 14, 2012 12:11 am

TIG wrote:Why not use 90.degrees etc and return the angle.radians to see what it is in degrees too... skip all of the PI and radian confusion...

=====> I tested with 90.degrees, but I thought it was the conversion that was bad


Why are you making your axe/up vectors so complex ?
Your figures are off too...
axe = Geom::Vector3d.new(-0.999782867743517, -0.0208378829670674, 0.0)

=====> because it is real vectors returned with axe.inspect
I was given a real example.



### Which is a very tiny number that's ALMOST zero
This is a a simple tolerance issue...

======>Tolerance has importance here because it is cumulative.

### BUT to test for tiny values use something like:
### a 'rounding' method - here to 6dp.
angle=(angle*10**6).round.to_f/10**6
angle==0
true
### This will then trap for these tiny inaccuracies...
...
macgile
 
Posts: 33
Joined: Sat Jan 19, 2008 10:59 am

Re: subtract two angle

Postby Dan Rathbun » Tue Feb 14, 2012 12:41 am

User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4102
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: subtract two angle

Postby thomthom » Tue Feb 14, 2012 9:54 am

Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17688
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Need SketchUp Books, Models, Styles or Textures? Check out our One Stop Shop for SketchUp.

Premium Members get 20% discount!

Ad Machine
Robot
 
Posts: 2012


Return to Developers' Forum

Who is online

Users browsing this forum: Bing [Bot], Google Bot and 4 guests