I'm trying to modulate an alpha value in a开发者_如何学Go Java application I'm building on Android. Right now it goes like this:
if (goingUp) {
newAlpha = oldAlpha + rateOfChange;
if (newAlpha > maxAlpha) {
newAlpha = maxAlpha;
goingUp = false;
}
} else {
newAlpha = oldAlpha - rateOfChange;
if (newAlpha < minAlpha) {
newAlpha = minAlpha;
goingUp = true;
}
}
Where rateOfChange is an arbitrary int that cannot be greater than maxAlpha. The equation is evaluate every tick in a thread and is independent of time.
Is there a way using only the variables given + Math.PI and other Math elements (I'm assuming Math.Sine will be in there) to get newAlpha to be a number on a Sine?
I'm thinking min and max would be the amp of the wave and rateOfChange would be a product of the Sine function, I just can't figure out how it all goes together.
Your equation will look like this:
y is vertical position at time t, A is the amplitude, f is the frequency, and t is the time (or ticks of your Android clock).
Why don't you consider this?
...at the top of your class definition, include:-
import java.lang.*;
...and within your function, after the assignment to newAlpha,
newAlpha = Math.sin(newAlpha%(2*Math.PI));
if you would like newAlpha to be in the range [-1,1] as the sin() function is
OR
...within your function, after the assignment to newAlpha,
newAlpha = Math.asin(newAlpha%3 - 1);
if you would like newAlpha to be in the range [-1,1] as the sin() function is
I'm not sure what datatype your newAlpha is, but I'm going to assume it will not affect the answer for this expression - like newAlpha is of type double.
It is generally the way to get a number within a certain range that you apply modulus to whatever expression you have ie. expr%N results in a number in range [0,N-1].
Hope this helps.
Based on duffymo's general equation I had to go all the way back to my TI-83 days (literally, put the app on my phone). But I was able to put all the pieces together so it ended up looking like this:
newAlpha = (int)((alphaMax - alphaMin) * 0.5 * Math.sin(rateOfChange * ticks + randomPhaseOffset) + (alphaMin + (alphaMax - alphaMin) * 0.5))
FMI: http://en.wikipedia.org/wiki/Sine_wave
精彩评论