tags:

views:

181

answers:

4

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;
                    }
                }
            }
        }           
+1  A: 

Have a look at Math.sin().

Graham Perks
How could you use Math.sin to return a number (for example the current position of the y-axis) as it follows the sine curve? Then for example, if Y had a range of 0-200 it would return the number 0, 25, 50, etc while it followed the sine curve.
Selzier
In updateNumbers() above, the numbers go up and down between 0 and 200, also in Justin's example below. I'm looking for the same behavior but for the number to be pulled from Y axis of a Sine wavelength.
Selzier
+1  A: 

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
jjnguy
This is a great improvement to my current logic. I'm looking to get this same behavior with a Sine function implemented it in this manner, so I'll actually be getting a step function that follows the sine curve (instead of just adding 50, the numbers returned come from the Sine curve).
Selzier
@Selzier, ahhh, well, you could use this solution, but, take Number between 0 and 360, and then use that as input to the sine function.
jjnguy
@Selz, I have the solution. Updating my answer.
jjnguy
Justin, thanks for the help on this one. Working with your code helped me grasp the logic of using a Sine Wave in Java. Excellent examples. Anyone searching to learn how to implement a simple Sin in Java can benefit from your answers here.
Selzier
@Selzier, np. Walter's answer is much better than mine.
jjnguy
A: 

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?

John Engelman
Thanks John. Yes, you are right I'm looking to get a number returned to me, as in Justin's code above, except like you said- the number would be following a sine curve and I would be pulling the number each time updateNumber() is run.
Selzier
In updateNumbers() above, the numbers go up and down between 0 and 200, also in Justin's example below. I'm looking for the same behavior but for the number to be pulled from Y axis of a Sine wavelength.
Selzier
+1  A: 

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

Walter Mundt
Thanks for the answer Walter. This code works perfectly to achieve a simple oscillating number via Sine.
Selzier