Page 1 of 1

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

Posted: 06 May 2017 14:33
by pieh-ejdsch
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=103
set /a moveX =+1 ^
 ,     moveY =+15^
 ,     angle =+90^
 , direction =0  ^
 ,   "circle =360"^
 ,     "Left =-(Right=angle)"^
 , negativeY =-180

set "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.
I have already read all posts about sinus and kosinus.
Among others I have read the following articles:

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

set "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"^
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?

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?

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

Posted: 08 May 2017 08:31
by einstein1969
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:
360:degrees=2*PI:Radiants where PI=3.1416*10000=31416
from previus we have Radiants=degree*2*PI/360=degree*PI/180

- 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

this help you?


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

Posted: 08 May 2017 13:12
by pieh-ejdsch
Hello Francesco,
i thank you for your very good explanation.
I have also found a good help at
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: I have the German edition of it. I also practice other Chinese arts.


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

Posted: 11 May 2017 09:21
by einstein1969
thanks phil!