High precision trigonometry?

gerth6000

Member
Licensed User
Longtime User
Not sure if my problem is because of the Sin/Cos functions or with the DrawCircle function....

Is there a library with Sin/Cos functions with high precision (10 digits or more)??

Or a DrawCircle function with high precision?



Explanation:
I'm working on an interactive 2-dimensional star map.

A particular star is placed in space with a set of x,y coordinates.

Its planet is then placed by using the radius of its orbit and its place in the orbit in radians, ie. I use Cos and Sin functions to calculate the planet's x,y coordinates.

I use the same star's coordinates and the orbit's radius to DrawCircle the orbit.

When zoomed out, that I can see planet and star on screen it looks perfect. Planet is on the circle. When zoomed in, that I can see the planet and its moons, the planet is off the circle by up to half a screen width.

I can calculate the misplacement as roughly 0.2% (2 in 1000) but it is not consistant. Bigger orbits have bigger misplacements.

For average applications I would think this error margin would be acceptable, but I use a zoom factor of up to 1 to 10 million (space is pretty big). Where 2 in 1000 is quite noticable.
 

gerth6000

Member
Licensed User
Longtime User
I'm thinking that DrawCircle doesn't draw a perfect circle, but a many-pointed polygon... That way the precision can never be guaranteed...?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you use Double variables the precision is at least 15 digits.
How do you draw the big circles ?
Could you post your project as a zip file (IDE menu Files / Export As Zip) so we could have a look at it ?
Are you sure that the orbits are circles and not ellipses ?

Best regards.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Never checked the precision of sin/cos etc, but if they're just approximations, why not recalculating them using Taylor's series? Just a thought.
 
Upvote 0

Hubert Brandel

Active Member
Licensed User
Longtime User
Never checked the precision of sin/cos etc, but if they're just approximations, why not recalculating them using Taylor's series? Just a thought.
could you explain how this works (building own sin, cos functions) because within a not b4a programm I do need this. ;)
 
Upvote 0

gerth6000

Member
Licensed User
Longtime User
You should remove the 0.99763 factor in lines 270-280.

It was an attempt to get closer to the orbit, but it still wasn't good enough.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
True, no reason for this factor. I see now the problem, I'll have a closer look, I hope other friends here can help.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Just a thought: if it is a matter of precision, perhaps you could try setting factors as long you zoom in, manually. I mean, the way you set the 0.99763 factor, but altering it for different zoom scales.
 
Upvote 0

gerth6000

Member
Licensed User
Longtime User
I used the factor because I thought it was a consistant deviation. What I found is the deviation was different on small and large orbits, not really related to the zoom. If i match one of the outer planets, the inner planets get thrown off orbit.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would suggest following:
- can you reduce the number of planets to draw let's say two green planets.
- in the orbit drawing routine draw also every one or two degrees a small dot to see if there is a difference in position between the circle function and sin/cos drawing.

Best regards.
 
Upvote 0

gerth6000

Member
Licensed User
Longtime User
I haven't tried your suggestion yet, Klaus, but I will.

I took screenshots of various planets so I could measure the deviation in pixels and calculate how much.

b4a-180812.png


The data shows no correlation between the circle's radius and the amount of deviation.
 
Upvote 0

gerth6000

Member
Licensed User
Longtime User
Here is your "version", Klaus.

At every pi/4 (45 degrees) the planet meets the orbit. In between the planets form a circle-arc with a radius smaller than the orbit.

So one of the two sets is not a perfect circle.

EDIT: I *guees* I could measure the deviation at pi/8 and assume the deviation is linearly progressive between 0 and pi/8 and pi/4.... but to do a correction such as this is definately not pretty programming :)
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Playing a bit with the Taylor's cos series, I've noticed some very tiny deviation. Could this help?
B4X:
'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
Dim lv As ListView 
End Sub

Sub Activity_Create(FirstTime As Boolean)
lv.Initialize ("lv")
Dim maxD As Double
maxD=0
For k=0 To 359
Dim a,b,curD As Double 
a=CosD(k)
b=cosd2(k)
curD=a/b
lv.AddSingleLine (k & "/" & a & " / " & b & "/" & curD)
maxD=Max(maxD,curD)
Next
Activity.AddView (lv,0,0,Activity.Width,Activity.Height )
Msgbox(maxD,"maxD")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub cosd2(degrees As Double) As Double 
Dim n As Int 
Dim sumTaylor,x As Double 
x=cPI*degrees/180
sumTaylor=0
For n=0 To 20
    Dim nf As Long
    nf=1
    If n>0 Then
        For i=1 To 2*n
            nf=nf*i
        Next
    End If
    sumTaylor=sumTaylor+Power(-1,n)*Power(x,2*n)/nf
Next
Return sumTaylor
End Sub
Sub lv_itemclick(position As Int,value As Object)
Msgbox(value,"ok")
End Sub
 
Upvote 0
Top