我如何更改方程以将线上的数字调制为在正弦曲线上调制的数字?

发布于 2024-10-13 01:53:05 字数 608 浏览 9 评论 0原文

我正在尝试调整我在 Android 上构建的 Java 应用程序中的 alpha 值。现在它是这样的:

if (goingUp) {
    newAlpha = oldAlpha + rateOfChange;
    if (newAlpha > maxAlpha) {
        newAlpha = maxAlpha;
        goingUp = false;
    }
} else {
    newAlpha = oldAlpha - rateOfChange;
    if (newAlpha < minAlpha) {
        newAlpha = minAlpha;
        goingUp = true;
    }
}

其中 rateOfChange 是一个不能大于 maxAlpha 的任意 int。该方程计算线程中的每个时钟周期并且与时间无关。

有没有办法仅使用给定的变量 + Math.PI 和其他数学元素(我假设 Math.Sine 将在那里)来使 newAlpha 成为正弦上的数字?

我认为 min 和 max 是波的放大器,rateOfChange 是正弦函数的乘积,我只是不明白它们是如何结合在一起的。

I'm trying to modulate an alpha value in a 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昨迟人 2024-10-20 01:53:05

您的方程将如下所示:

alt text

y 是时间 t 时的垂直位置,A 是幅度,f 是频率,t 是时间(或 Android 时钟的滴答声)。

Your equation will look like this:

alt text

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).

旧时模样 2024-10-20 01:53:05

你为什么不考虑这个呢?

...在类定义的顶部,包括:-

import java.lang.*;

...以及在您的函数中,在分配给 newAlpha 之后,

newAlpha = Math.sin(newAlpha%(2*Math.PI));

如果您希望 newAlpha 作为 sin() 函数在 [-1,1] 范围内是

OR

...在你的函数中,在分配给 newAlpha 之后,

newAlpha = Math.asin(newAlpha%3 - 1);

如果你希望 newAlpha 在 [-1,1] 范围内,因为 sin() 函数是

我不确定你的 newAlpha 是什么数据类型,但是我假设它不会影响这个表达式的答案 - 就像 newAlpha 是 double 类型一样。

通常,将模数应用于您拥有的任何表达式,即获得一定范围内的数字的方法。 expr%N 结果是 [0,N-1] 范围内的数字。

希望这有帮助。

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.

追风人 2024-10-20 01:53:05

根据达菲莫的一般方程,我必须一直追溯到我的 TI-83 日子(字面意思是,将应用程序放在我的手机上)。但我能够将所有部分放在一起,所以它最终看起来像这样:

newAlpha = (int)((alphaMax - alphaMin) * 0.5 * Math.sin(rateOfChange * ticks + randomPhaseOffset) + (alphaMin + (alphaMax - alphaMin) * 0.5))

FMI:http://en .wikipedia.org/wiki/Sine_wave

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文