Circles
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
I developed a new formula for circles
I came up with a new sine formula , not perfect but hits just about every degree. more even spanning of the units.
to adjust the increment just change:
inc = .01 to inc = ???
to adjust the increment just change:
inc = .01 to inc = ???
Code: Select all
' testing new sine formula
dim pi as double = 4 * atn(1)
dim DtoR as double = pi / 180
dim RtoD as double = 180/pi
dim value1 as double
dim c_square as double
dim sine as double
dim cosine as double
dim deg as double
dim in as string
dim xctr as double = 205
dim yctr as double = 205
dim radius as double = 200
dim as double x,y
dim as integer toggle
dim inc as double
screen 19
deg = 0
toggle = 0
inc = .01
do
line(xctr,yctr)-(xctr+x,yctr+ -y),1
value1 = (.5 / (90/deg))
c_square = ((value1 * value1)*2)
sine = sqr(c_square)
cosine = sqr(1- sine*sine)
x = radius * cosine
y = radius * sine
line(xctr,yctr)-(xctr+x,yctr+ -y),1
circle(xctr,yctr),radius+1,2
line(xctr,yctr)-(xctr + x,yctr + -y),14
line(xctr,yctr)-(xctr + x,yctr + y),14
line(xctr,yctr)-(yctr+ -y,xctr+ x),14
line(xctr,yctr)-(yctr+ -y,xctr+ -x),14
line(xctr,yctr)-(xctr+ -x,yctr+ y),14
line(xctr,yctr)-(xctr+ -x,yctr+ -y),14
line(xctr,yctr)-(yctr+ y,xctr+ x),14
line(xctr,yctr)-(yctr+ y,xctr+ -x),14
if toggle = 0 then
deg = deg + inc
if deg > 90 then toggle = 1
end if
if toggle = 1 then
deg = deg - inc
if deg < 0 then toggle = 0
end if
locate 30,1:print "Press q to quit.";
locate 31,1:print space(60);
locate 31,1:print "step angle = "; atan2(sine,cosine) * RtoD
loop until inkey$ ="q"
Looks good in the Swedish national colours, but you are only sweeping your sine approximation over 0 to 45 degrees which is the easy bit, even though your deg varies from 0 to 90.
Your 4 lines of code;
are mathematically identical to, and do exactly the same as these two lines.
which are faster to compute and easier to understand.
Your 4 lines of code;
Code: Select all
value1 = (.5 / (90/deg))
c_square = ((value1 * value1)*2)
sine = Sqr(c_square)
cosine = Sqr(1- sine*sine)
Code: Select all
sine = deg * Sqr(2) / 180
cosine = Sqr(1- sine*sine)
This one works a little better but still some wide units around 45 degrees.
almost perfect!!!!
you can alter the increment to ????
inc = .5 (just above the do loop)
almost perfect!!!!
you can alter the increment to ????
inc = .5 (just above the do loop)
Code: Select all
' testing new sine formula
dim pi as double = 4 * atn(1)
dim DtoR as double = pi / 180
dim RtoD as double = 180/pi
dim value1 as double
dim c_square as double
dim sine as double
dim cosine as double
dim deg as double
dim in as string
dim xctr as double = 205
dim yctr as double = 205
dim radius as double = 200
dim as double x,y
dim as integer toggle
dim inc as double
screen 19
deg = 0
toggle = 0
inc = .5
do
line(xctr,yctr)-(xctr+x,yctr+ -y),1
value1 = (.5 / (90/deg))
c_square = ((value1 * value1)*2)
if deg < 90 and deg > 0 then c_square = c_square + (c_square / deg)
sine = sqr(c_square)
cosine = sqr(1- sine*sine)
x = radius * cosine
y = radius * sine
line(xctr,yctr)-(xctr+x,yctr+ -y),1
circle(xctr,yctr),radius+1,2
line(xctr,yctr)-(xctr + x,yctr + -y),14
line(xctr,yctr)-(xctr + x,yctr + y),14
line(xctr,yctr)-(yctr+ -y,xctr+ x),14
line(xctr,yctr)-(yctr+ -y,xctr+ -x),14
line(xctr,yctr)-(xctr+ -x,yctr+ y),14
line(xctr,yctr)-(xctr+ -x,yctr+ -y),14
line(xctr,yctr)-(yctr+ y,xctr+ x),14
line(xctr,yctr)-(yctr+ y,xctr+ -x),14
if toggle = 0 then
deg = deg + inc
if deg > 90 then toggle = 1
end if
if toggle = 1 then
deg = deg - inc
if deg < 0 then toggle = 0
end if
locate 30,1:print "Press q to quit.";
locate 31,1:print space(60);
locate 31,1:print "step angle = "; atan2(sine,cosine) * RtoD
loop until inkey$ ="q"
@ albert
The red line is the true sine function. The white line is an inverted parabola. Your latest approximation for sine, it is the yellow diagonal line. As you can see, it still has a bit of a problem.
The red line is the true sine function. The white line is an inverted parabola. Your latest approximation for sine, it is the yellow diagonal line. As you can see, it still has a bit of a problem.
Code: Select all
Screen 19
Window (-2,-.1)-(92, 1.1)
Const As Double Pi = 4 * Atn(1)
Const As Double PiOn2 = Pi / 2
Dim As Double x, y, deg
line ( 0, 0 ) - (+90, 0 ), 7 ' x axis
line ( 0,.7071) - (+90,.7071), 7 ' y = .7071
line ( 0, +1 ) - (+90, +1 ), 7 ' y = +1
for deg = 0 to 90 step 45
line (deg, 0) - (deg, 1), 7
next deg
draw string (0,0),"0",15
draw string (45,0),"45",15
draw string (90,0),"90",15
draw string (0,.7071),".7071",15
draw string (0,1),"+1",15
' the reference sine function
For deg = 0 To 180 step .1
y = sin(deg * Pi / 180)
Pset (deg, y), 12 ' red
Next deg
' inverted parabola sine approximation
For deg = 0 To 180 Step .1
y = 1-(deg/90-1)^2
Pset (deg, y), 15 ' white
Next deg
' Albert's sine approximation
dim as double value1, s, c_square
For deg = 0 To 180 Step .1
value1 = (.5 / (90 / deg))
c_square = ((value1 * value1)*2)
If deg < 90 And deg > 0 Then c_square = c_square + (c_square / deg)
y = Sqr(c_square)
Pset (deg, y), 14 ' yellow
Next deg
Sleep
You forgot my formula only spans 45 degrees so i had to do a:
PSET( deg/2 , y )
and it almost creates a perfect sine approximation.
its better than the inverted parabola.
Thanks for this program i can use it to dial in my formula.
PSET( deg/2 , y )
and it almost creates a perfect sine approximation.
its better than the inverted parabola.
Thanks for this program i can use it to dial in my formula.
Code: Select all
Screen 19
Window (-2,-.1)-(92, 1.1)
Const As Double Pi = 4 * Atn(1)
Const As Double PiOn2 = Pi / 2
Dim As Double x, y, deg
Line ( 0, 0 ) - (+90, 0 ), 7 ' x axis
Line ( 0,.7071) - (+90,.7071), 7 ' y = .7071
Line ( 0, +1 ) - (+90, +1 ), 7 ' y = +1
For deg = 0 To 90 Step 45
Line (deg, 0) - (deg, 1), 7
Next deg
Draw String (0,0),"0",15
Draw String (45,0),"45",15
Draw String (90,0),"90",15
Draw String (0,.7071),".7071",15
Draw String (0,1),"+1",15
' the reference sine function
For deg = 0 To 180 Step .1
y = Sin(deg * Pi / 180)
Pset (deg, y), 12 ' red
Next deg
' inverted parabola sine approximation
For deg = 0 To 180 Step .1
y = 1-(deg/90-1)^2
Pset (deg, y), 15 ' white
Next deg
' Albert's sine approximation
Dim As Double value1, s, c_square
For deg = 0 To 90 Step .1
value1 = (.5 / (90 / deg))
c_square = ((value1 * value1)*2)
If deg < 90 And deg > 0 Then c_square = c_square + (c_square / deg)
y = Sqr(c_square)
Pset (deg/2, y), 14 ' yellow
Next deg
Sleep
Got my formula almost complete.
Richard, I got it pretty much dialed in.
its more uniform across the segments now.
I scaled up your program to 45 deg across 12 units in the window.
The white line is the inverted parabola.
The red line is the true sine curve.
The purple line is my formula without modification.
The yellow line is my formula with modifications.
I'm trying to devise a true applicable formula to apply to the degrees rather than all the 10 additions.
its more uniform across the segments now.
I scaled up your program to 45 deg across 12 units in the window.
The white line is the inverted parabola.
The red line is the true sine curve.
The purple line is my formula without modification.
The yellow line is my formula with modifications.
Code: Select all
Screen 19
Window (-2,-.1)-(12, 1.1)
Const As Double Pi = 4 * Atn(1)
Const As Double PiOn2 = Pi / 2
Dim As Double x, y, deg
Line ( 0, 0 ) - (+12, 0 ), 7 ' x axis
Line ( 0,.7071) - (+12,.7071), 7 ' y = .7071
Line ( 0, +1 ) - (+12, +1 ), 7 ' y = +1
For deg = 0 To 90 Step 11.25/4
Line (deg, 0) - (deg, 1), 7
Next deg
Draw String (0,0),"0",15
Draw String (2.8125,0),"11.25",15
Draw String (5.625 ,0),"22.5",15
Draw String (8.4375,0),"33.75",15
Draw String (11.25,0),"45",15
Draw String (90,0),"90",15
Draw String (0,.7071),".7071",15
Draw String (0,1),"+1",15
' the reference sine function
For deg = 0 To 180 Step .01
y = Sin(deg * Pi / 180)
Pset (deg/4, y), 12 ' red
Next deg
' inverted parabola sine approximation
For deg = 0 To 180 Step .01
y = 1-(deg/90-1)^2
Pset (deg/4, y), 15 ' white
Next deg
' Albert's sine approximation
Dim As Double value1, s, c_square,value2
For deg = 0 To 90 Step .01
value1 = (.5 / (90 / deg))
c_square = ((value1 * value1)*2)
y = Sqr(c_square)
pset(deg/8, y), 13 ' purple
Next deg
' Albert's sine approximation with modifications
pset(0,0),14
For deg = 0 To 90 Step .01
value1 = (.5 / (90 / deg))
c_square = ((value1 * value1)*2)
value2 = sqr(.5)/150 * deg / 90
if deg < 90 and deg > 0 then c_square = c_square + value2
if deg < 87 and deg > 45 then c_square = c_square + value2
if deg < 85 and deg > 5 then c_square = c_square + value2
if deg < 83 and deg > 50 then c_square = c_square + value2
if deg < 80 and deg > 10 then c_square = c_square + value2
if deg < 75 and deg > 15 then c_square = c_square + value2
if deg < 70 and deg > 20 then c_square = c_square + value2
if deg < 65 and deg > 25 then c_square = c_square + value2
if deg < 60 and deg > 30 then c_square = c_square + value2
if deg < 55 and deg > 35 then c_square = c_square + value2
y = Sqr(c_square)
line -(deg/8, y), 14 ' yellow
Next deg
Sleep
If a piecewise approximation from 0 to 45 degrees is acceptable then this beats yours for accuracy.
Code: Select all
'---------------------------------------------------------
' Richard's piecewise sine approximation
Dim As Double deg, y
For deg = 0 To 45 Step .01
if deg < 5 Then : y = deg * 0.017431 + 0.000000
Elseif deg < 10 Then : y = deg * 0.017298 + 0.000663
Elseif deg < 15 Then : y = deg * 0.017034 + 0.003306
Elseif deg < 20 Then : y = deg * 0.016640 + 0.009216
Elseif deg < 25 Then : y = deg * 0.016120 + 0.019628
Elseif deg < 30 Then : y = deg * 0.015476 + 0.035710
Elseif deg < 35 Then : y = deg * 0.014715 + 0.058541
Elseif deg < 40 Then : y = deg * 0.013842 + 0.089098
Else y = deg * 0.012864 + 0.128234
End If
Pset (deg, y), 13 ' pink
Next deg
I'm sorry, I did not notice or adjust any numbers. I just wrote and ran the following crude code. Then I edited the first and second last lines.
Code: Select all
Const As Double Pi = 4 * Atn(1)
Dim As Double deg, s, c, m, last=0, dx = 5
For deg = dx To 45 Step dx
s = Sin (deg * Pi / 180)
m = (s - last) / dx
c = last - (deg-dx) * m
last = s
Print Using "Elseif deg < ## Then :"; deg;
Print Using " y = deg * #.###### _+ #.######"; m; c
Next deg
Print "End If"
Sleep
Here is the sine graph again with my new formulas.
it required 5 separate formulas.
one for 0 - 45 units ( 0 - 22.5 deg)
one for >45 to 80 (22.5 - 40 deg)
one for >80 to 85 (40 - 42.5 deg)
and one for >85 to <88 ( 42.5 - 45 deg)
there is a little bit of stepping in the formulas above 40 degrees.
when drawing a circle with it it produces a small deg .5 ???
it required 5 separate formulas.
one for 0 - 45 units ( 0 - 22.5 deg)
one for >45 to 80 (22.5 - 40 deg)
one for >80 to 85 (40 - 42.5 deg)
and one for >85 to <88 ( 42.5 - 45 deg)
there is a little bit of stepping in the formulas above 40 degrees.
when drawing a circle with it it produces a small deg .5 ???
Code: Select all
Screen 19
Window (-2,-.1)-(24, 1.1)
Const As Double Pi = 4 * Atn(1)
Const As Double PiOn2 = Pi / 2
Dim As Double x, y, deg
Line ( 0, 0 ) - (+24, 0 ), 7 ' x axis
Line ( 0,.7071) - (+24,.7071), 7 ' y = .7071
Line ( 0, +1 ) - (+24, +1 ), 7 ' y = +1
y = abs(sin(22.5))
line( 0 , y) - (22.5 , y),7
For deg = 0 To 45 Step 11.25 / 2
Line (deg, 0) - (deg, 1), 7
Next deg
Draw String (0,0),"0",15
Draw String (5.625,0),"11.25",15
Draw String (11.25 ,0),"22.5",15
Draw String (16.875,0),"33.75",15
Draw String (22.5,0),"45",15
Draw String (90,0),"90",15
Draw String (0,.7071),".7071",15
Draw String (0,1),"+1",15
' the reference sine function
For deg = 0 To 90 Step .01
y = Sin(deg * Pi / 180)
Pset (deg/2, y), 12 ' red
Next deg
' inverted parabola sine approximation
For deg = 0 To 90 Step .01
y = 1-(deg/90-1)^2
Pset (deg/2, y), 15 ' white
Next deg
'---------------------------------------------------------
' Richard's piecewise sine approximation
'Dim As Double deg, y
'For deg = 0 To 45 Step .01
' If deg < 5 Then : y = deg * 0.017431 + 0.000000
' Elseif deg < 10 Then : y = deg * 0.017298 + 0.000663
' Elseif deg < 15 Then : y = deg * 0.017034 + 0.003306
' Elseif deg < 20 Then : y = deg * 0.016640 + 0.009216
' Elseif deg < 25 Then : y = deg * 0.016120 + 0.019628
' Elseif deg < 30 Then : y = deg * 0.015476 + 0.035710
' Elseif deg < 35 Then : y = deg * 0.014715 + 0.058541
' Elseif deg < 40 Then : y = deg * 0.013842 + 0.089098
' Else y = deg * 0.012864 + 0.128234
' End If
' Pset (deg/2, y), 11 ' pink
'Next deg
' Albert's sine approximation
Dim As Double value1, s, c_square,value2
For deg = 0 To 90 Step .01
value1 = (.5 / (90 / deg))
c_square = ((value1 * value1)*2)
y = Sqr(c_square)
pset(deg/4, y), 13 ' purple
Next deg
' Albert's sine approximation with modifications
'pset(0,0),14
'For deg = 0 To 90 Step .01
' value1 = (.5 / (90 / deg))
' c_square = ((value1 * value1)*2)
' value2 = sqr(.5)/150 * deg / 90'
'
' if deg < 90 and deg > 0 then c_square = c_square + value2
' if deg < 87 and deg > 45 then c_square = c_square + value2
' if deg < 85 and deg > 5 then c_square = c_square + value2
' if deg < 83 and deg > 50 then c_square = c_square + value2
'
' if deg < 80 and deg > 10 then c_square = c_square + value2
' if deg < 75 and deg > 15 then c_square = c_square + value2
' if deg < 70 and deg > 20 then c_square = c_square + value2
' if deg < 65 and deg > 25 then c_square = c_square + value2
' if deg < 60 and deg > 30 then c_square = c_square + value2
' if deg < 55 and deg > 35 then c_square = c_square + value2
' y = Sqr(c_square)
' line -(deg/4, y), 14 ' yellow
'Next deg
' Albert's sine approximation with modifications
pset(0,0),14
For deg = 0 To 90 Step .01
value1 = (.5 / (90 / deg))
c_square = ((value1 * value1)*2)
if deg > 0 and deg <= 45 then value2 = sqr(.5)/150 * (deg * deg / 400)
if deg > 45 and deg <= 75 then value2 = sqr(.5)/150 * (5 / deg * deg)
if deg > 75 and deg <= 80 then value2 = sqr(.5)/150 * (4 / deg * deg)
if deg > 80 and deg <= 85 then value2 = sqr(.5)/150 * (3 / deg * deg)
if deg > 85 and deg <= 88 then value2 = sqr(.5)/150 * (1.5 / deg * deg)
if deg > 88 and deg <= 90 then value2 = 0
c_square = c_square + value2
y = Sqr(c_square)
line -(deg/4, y), 14 ' yellow
Next deg
Sleep
Would it not be easier just to use one smooth equation that works for all 0 to 90 degrees?
Code: Select all
For deg = 0 To 90 Step .01
y = deg*(1.745e-2+deg*(-8.828e-7+deg*1.232e-11*deg)*deg)
Pset (deg, y), 14 ' yellow
Next deg