如何使用正弦/余弦波返回振荡数
我是 Java 编程新手,我正在使用 Android 编写 Java 1.6。
我有一个简单的函数,可以使数字在 0 到 200 之间上下波动。我想将其放入正弦函数中,但我一直在尝试时不断出错。
我希望我的程序通过正弦波 y 轴更新 int (Number1)。
有什么想法可以将以下逻辑更改为正弦函数吗? (忽略第二个数字)
代码:
private int Number1 = 150;
private int Number2 = 0;
private int counter = 0;
public void updateNumbers() {
if (counter == 0) {
if (Number1 < 200) {
Number1 = Number1 + 50;
Number2 = Number2 - 50;
if (Number1 >= 200) {
counter = 1;
}
}
} else if (counter == 1) {
if (Number2 < 200) {
Number1 = Number1 - 50;
Number2 = Number2 + 50;
if (Number2 >= 200) {
counter = 0;
}
}
}
}
I'm new to Java programming, I am programming Java 1.6 with Android.
I have a simple function that makes a number go up and down between 0 and 200. I would like to put this into a Sine function but keep getting errors with what I've been trying.
I want my program to update an int (Number1) via a sine wave y axis.
Any ideas change the following logic into a Sine function? (disregard the 2nd number)
code:
private int Number1 = 150;
private int Number2 = 0;
private int counter = 0;
public void updateNumbers() {
if (counter == 0) {
if (Number1 < 200) {
Number1 = Number1 + 50;
Number2 = Number2 - 50;
if (Number1 >= 200) {
counter = 1;
}
}
} else if (counter == 1) {
if (Number2 < 200) {
Number1 = Number1 - 50;
Number2 = Number2 + 50;
if (Number2 >= 200) {
counter = 0;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,那么您想要做的是构建一个介于 0 到 200 之间的正弦波,但是周期是多少?您希望它每 8 次调用就循环一次吗?
怎么样,利用内置的 Java
Math.sin
函数:基本上,我们保留一个变量来计算我们已完成的更新次数,并对其进行缩放以匹配正弦波的周期, 2*PI。它充当“真实”正弦函数的输入,为我们提供介于 -1 和 1 之间但具有正确频率的值。然后,要实际设置数字,我们只需将其缩放到 -100 到 100 之间,然后添加 100 将其移动到您从一开始就想要的 0-200 范围内。
(如果 double 适合你,你不必将数字转换为 int,我只是遵循你上面所写的精神。)
Okay, so what you want to do is build a sine wave that goes between 0 and 200, but with what period? Did you want it to loop about every 8 calls?
How about this, leveraging the built-in Java
Math.sin
function:Basically, we keep a variable that counts how many updates we've done, and scale that to match the period of a sine wave, 2*PI. That acts as the input to the 'real' sin function, giving us something that goes between -1 and 1 but has the right frequency. Then, to actually set the number, we just scale that to be between -100 and 100 and then add 100 to move it to be in the 0-200 range you wanted from the beginning.
(You don't have to cast the number to an int if a double works for you, I was just keeping with the spirit of what you wrote above.)
*更新以产生正弦波**
这应该可以满足您的要求。第一部分只是振荡正弦函数的角度输入。
这部分使用密切值,并将其输入到正弦函数中,然后进行一些计算以拟合从
0
到200
的值。结果将如下所示:
*Updated to produce a Sine wave**
This should do what you want. The first part just oscillates the angel input to the Sine function.
This part uses the osculating value, and inputs it into a Sine function, and then does some calculation to fit the values from
0
to200
.The results will look like:
所以你正在考虑离散步骤?正弦/余弦是连续函数,因此如果您尝试以这种方式实现它,您实际上会得到一个遵循正弦/余弦曲线的阶跃函数。
每次通过该函数都会增加 50,因此在循环之前您只会获得值 {1, 51, 101, 151}(我假设 counter = 1 行应该是 Number1 = 1)。
您能否提供更多信息以便我们解答?
So you're looking at discrete steps? Sine/Cosine are continuous functions so if you try to implement it in this manner, you'll actually be getting a step function that follows the sine/consine curve.
You're incrementing by 50 each time through the function so the you'd only get the values {1, 51, 101, 151} before it cycles (I'm assuming the counter = 1 line should be Number1 = 1).
Can you provide some more information for us to answer?