如何使用正弦/余弦波返回振荡数

发布于 2024-09-18 03:33:17 字数 1025 浏览 5 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(3

吻风 2024-09-25 03:33:17

好的,那么您想要做的是构建一个介于 0 到 200 之间的正弦波,但是周期是多少?您希望它每 8 次调用就循环一次吗?

怎么样,利用内置的 Java Math.sin 函数:

private static final double PERIOD = 8; // loop every 8 calls to updateNumber
private static final double SCALE = 200; // go between 0 and this

private int _pos = 0;
private int Number1 = 0;

public void updateNumber() {
    _pos++;
    Number1 = (int)(Math.sin(_pos*2*Math.PI/PERIOD)*(SCALE/2) + (SCALE/2));
}

基本上,我们保留一个变量来计算我们已完成的更新次数,并对其进行缩放以匹配正弦波的周期, 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:

private static final double PERIOD = 8; // loop every 8 calls to updateNumber
private static final double SCALE = 200; // go between 0 and this

private int _pos = 0;
private int Number1 = 0;

public void updateNumber() {
    _pos++;
    Number1 = (int)(Math.sin(_pos*2*Math.PI/PERIOD)*(SCALE/2) + (SCALE/2));
}

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

赴月观长安 2024-09-25 03:33:17

*更新以产生正弦波**

这应该可以满足您的要求。第一部分只是振荡正弦函数的角度输入。

// Number starts at middle val
private int Number1 = -180;
// and is shrinking
private int direction = -1;

public void updateNumber() {   
    // if the number is in the acceptable range, 
    //  keep moving in the direction you were going (up or down)
    if (Number1 < 180 && Number1 > -180) {
        Number1 = Number1 + (50 * direction);
    } else { 
        // otherwise, reverse directions
        direction = direction * -1;
        // and start heading the other way
        Number1 = Number1 + (50 * direction);
    }
} 

这部分使用密切值,并将其输入到正弦函数中,然后进行一些计算以拟合从 0200 的值。

for (int i = 0; i < 200; i++){
    System.out.println((100 * (Math.sin((Number1* Math.PI)/180.0)))+100); 
    updateNumber();
}

结果将如下所示:

0.0
0.38053019082543926
1.5192246987791975
3.4074173710931746
6.03073792140917
9.369221296335013
13.397459621556138
18.084795571100827
23.395555688102192
29.28932188134526
35.72123903134607
42.64235636489539
50.00000000000001
57.738173825930055
65.79798566743312
74.11809548974793
82.63518223330696
91.28442572523419
100.0
108.71557427476581
117.36481776669304
125.88190451025207
134.20201433256688
142.26182617406994
150.0
157.3576436351046
164.27876096865393
170.71067811865476
176.6044443118978
181.9152044288992
186.60254037844385
190.630778703665
193.96926207859082
196.59258262890683
198.4807753012208
199.61946980917457
200.0

*Updated to produce a Sine wave**

This should do what you want. The first part just oscillates the angel input to the Sine function.

// Number starts at middle val
private int Number1 = -180;
// and is shrinking
private int direction = -1;

public void updateNumber() {   
    // if the number is in the acceptable range, 
    //  keep moving in the direction you were going (up or down)
    if (Number1 < 180 && Number1 > -180) {
        Number1 = Number1 + (50 * direction);
    } else { 
        // otherwise, reverse directions
        direction = direction * -1;
        // and start heading the other way
        Number1 = Number1 + (50 * direction);
    }
} 

This part uses the osculating value, and inputs it into a Sine function, and then does some calculation to fit the values from 0 to 200.

for (int i = 0; i < 200; i++){
    System.out.println((100 * (Math.sin((Number1* Math.PI)/180.0)))+100); 
    updateNumber();
}

The results will look like:

0.0
0.38053019082543926
1.5192246987791975
3.4074173710931746
6.03073792140917
9.369221296335013
13.397459621556138
18.084795571100827
23.395555688102192
29.28932188134526
35.72123903134607
42.64235636489539
50.00000000000001
57.738173825930055
65.79798566743312
74.11809548974793
82.63518223330696
91.28442572523419
100.0
108.71557427476581
117.36481776669304
125.88190451025207
134.20201433256688
142.26182617406994
150.0
157.3576436351046
164.27876096865393
170.71067811865476
176.6044443118978
181.9152044288992
186.60254037844385
190.630778703665
193.96926207859082
196.59258262890683
198.4807753012208
199.61946980917457
200.0
萝莉病 2024-09-25 03:33:17

所以你正在考虑离散步骤?正弦/余弦是连续函数,因此如果您尝试以这种方式实现它,您实际上会得到一个遵循正弦/余弦曲线的阶跃函数。

每次通过该函数都会增加 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?

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