## How do I use the function sin (x) and cos (x) -do not understand the algorithm

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
pieh-ejdsch
Posts: 223
Joined: 04 Mar 2014 11:14
Location: germany

### How do I use the function sin (x) and cos (x) -do not understand the algorithm

First of all, I would like to say thank you for the help of you, which I have received here.

A function to determine four directions on a map depends on the type of map,
And whether I give absolute or relative directions.

Map 1 - split the rows and pack each row into individual arrays.
Map 2 - depending on the maximum length of the array: which contains as many lines as possible.
90 lines x 90 columns (8100 coordinates, maximum of 8192 coordinates)
The maps differ only in the number and length of the array (s).

For four directions the new coordinates correspond to a certain calculation.
The directional change requires only the position and the angle to the new position.
The relative direction is added to the old direction and determined by the sinus and the cosine on a map.
Eg .: to the left
- take the old angle add to the new angle (0 + = - 90)
- take the sinus of 0 ° and the cosine of 0 °, compute X and Y coordinate
- the negative sine determines Y
- The cosine determines X

An example (on map 2):

Code: Select all

``set /a coordinate=103set /a moveX =+1 ^ ,     moveY =+15^ ,     angle =+90^ , direction =0  ^ ,   "circle =360"^ ,     "Left =-(Right=angle)"^ , negativeY =-180set "direction=(direction=Left +direction %%circle)"set /a "coordinate+=(((%direction% +negativeY) /angle %%2) *moveY + ((direction -angle) /angle %%2) *moveX)"``

I know it looks somewhat complicated.

With sinus and cosinus, I can calculate eight directions when the angle is 45 °
[ posY -=sin(direction) *1.4122 *moveY ]
[ posX =cos(direction) *1.4122 *moveX ]

Code: Select all

``Set / a posY =%sin(x):x=direction% * 141422/100000 *moveY``

Cosine is mirrored at 45 ° to the sinus
Cos(x) = sin( (-x-45) * -1 + 45 )

But I do not come with the sinusfunktion on the result of the respective angle.
Among others I have read the following articles:
viewtopic.php?f=3&t=5819&hilit=cos
viewtopic.php?f=3&t=5824&p=42648&hilit=cos
viewtopic.php?f=3&t=4896&p=49822&hilit=fastes+sin#p49822

The results are actually between minus 1 and plus 1. Either I have not understood the use of the function, or the decisive part of the calculation is still missing.

Code: Select all

``setlocal enableDelayedExpansion  REM APA mod: Convert final sine calculation into a long "function" expression  REM http://www.dostips.com/forum/viewtopic.php?f=3&t=6744set "a=(a=(x)%%62832)"set "c=(c=(%a%>>31|1)*a)"set "t=(t=((%c%-47125)>>31)+1)"set "a=a-=%t%*((a>>31|1)*62832) +^^^^^^^!t*((((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a))"set "SIN(x)=%a%,a-a*a/1920*a/312500+a*a/1920*a/15625*a/15625*a/2560000-a*a/18752"^ "*a/15360*a/15625*a/15625*a/16000*a/44800000"for %%i in (a c t) do set "%%i="set /a "posY-=%sin(x):x=40%"set /a "posX=%sin(x):x=(-40-45)*-1+45%``

Is that the conversion from sin to cosinus?
15708-angle

Because where I am straight I unfortunately come no further. Is there a simple explanation of how I can use the function sinus in batch?
Phil

einstein1969
Expert
Posts: 774
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

### Re: How do I use the function sin (x) and cos (x) -do not understand the algorithm

Hi phil,

I have waited that Antonio Acini answered because him is very expert and I have depression and my mental health is not good.

but I probe to explain:

- The SIN function uses in input INTEGER number in form 10000*RADIANT and output from -10000 to 10000.
Uses a FIXED POINT like math where ONE=1.0=10000

- If you need use DEGREE instead of RADIANT you need convert using this math equivalence:

- Then I use COS(X)=SIN(PI/2-X)

example use from dos batch raicast (turn left 90 degree):

Code: Select all

`` set /A PI=31416, PI_div_2=31416/2 & rem 15708 set /A Angle=90 & rem 90 degree set /A "StepY=%SIN(x):x=!Angle! * PI / 180%" & rem sin(x) set /A "StepX=%SIN(x):x=PI_div_2 - (!Angle! * PI / 180)%" & rem cos(x)``

This return Stepy=sin(90)=10000, Stepx=cos(90)=0

Francesco

pieh-ejdsch
Posts: 223
Joined: 04 Mar 2014 11:14
Location: germany

### Re: How do I use the function sin (x) and cos (x) -do not understand the algorithm

Hello Francesco,
i thank you for your very good explanation.
I have also found a good help at http://www.robvanderwoude.com/cosine.php.
Now I have also an Aha effect.

I hope you get well soon. I recommend you T'ai Chi Ch'uan. It has many positive effects on body, mind and soul. The form is a good remedy for many illness at soul. I also practice it autodidact. It helps me to alleviate my complicated migraine with aura (with all neurological symptoms).
A very recommendable book: https://www.amazon.com/Tai-Chi-Chuan-Becoming-Martial/dp/0804837643 I have the German edition of it. I also practice other Chinese arts.

Phil

einstein1969
Expert
Posts: 774
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

thanks phil!