开发者

How to use a Sine / Cosine wave to return an oscillating number

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;     

  pub开发者_开发问答lic 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;
                    }
                }
            }
        }           


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


*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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜