KeatonMill
10th December 2002 00:54 UTC
Wrapping Variables
Hey guys. I've got a question. Is there a way to wrap a variable in superscopes? Specifically, I want to wrap something from -1 to 1, so that if the value is say 1.5, the wrapper will change it to -.5. I've tried different combinations of trig and arctrig functions, but haven't gotten anything to work yet.
Can someone help?
Thanks,
Keaton
UnConeD
10th December 2002 01:26 UTC
You can use a hack with the % operator to wrap with a certain precision... for example to wrap 'a' between 0 and 5 to a precision of 0.01:
a=((a*100)%500)*.01
To wrap between -5 and 5 with a precision of 0.1:
a=(((a+5)*10)%100)*.1-5
(add 5, scale by 10, wrap between 0-100, scale by 0.1, subtract 5)
This isn't perfect and has some troubles near zero, but it works for most purposes.
This technique will cause the numbers to jump back to the lower limit when they reach the upper limit.
There's a different technique which has the numbers bounce between the upperlimit and lowerlimit (ping-pong style):
asin(sin(var*(pi/2)))/(pi/2)
This resembles a sine-wave, except it's completely linear. Naturally you should replace pi/2 and 1/(pi/2) by constants, but I left them in for clarity.
Jaheckelsafar
10th December 2002 06:33 UTC
You could go the simple rout.
a=a+.1;
a=if(above(a,1),a-2,a);
a=if(below(a,-1),a+2,a);
It might no be the fastest, but it works. I have proof.
UnConeD
10th December 2002 07:23 UTC
Jaheckelsafar, your method works for variables that increase/decrease less than one period of the wrapping. Mine works for wrapping any number into the range.
So, choose whichever one you need. Jaheckelsafar's is faster of course.
fsk
13th December 2002 11:08 UTC
this might sound silly but what is wraping?
nixa
13th December 2002 18:53 UTC
FSK: Wraping means to get any variable value to be between some min and max values. Moastly this is used whith angles alpha=alpha+2*pi=alpha+4*pi=alpha+6*pi=alpha+2*k*pi,(k element of Z)
so if you have some angle alpha=7pi you will want to write it as alpha=pi
jheriko
14th December 2002 17:03 UTC
if you want to use trig functions for wrapping then you could use atan(tan(poo)); and it will wrap poo between about -1.6 and 1.6
dirkdeftly
14th December 2002 23:50 UTC
asin(sin(x*pi*a)/pi/a) wraps from -a to a. If it's not working try asin(sin(x*pi/a)/pi*a).