# Rotate a sinusoidal wave (2D traslation)

Hello everybody

This program simulate a sinusoidal wave running along x axis

``

``````int xspacing = 16;   // How far apart should each horizontal location be spaced
int w;              // Width of entire wave

float theta = 0.0;  // Start angle at 0
float amplitude = 75.0;  // Height of wave
float period = 500.0;  // How many pixels before the wave repeats
float dx;  // Value for incrementing X, a function of period and xspacing
float[] yvalues;  // Using an array to store height values for the wave

void setup() {
size(640, 360);
w = width+16;
dx = (TWO_PI / period) * xspacing;
yvalues = new float[w/xspacing];
}

void draw() {
background(255);
calcWave();
renderWave();
}

void calcWave() {
// Increment theta (try different values for 'angular velocity' here
theta += 0.02;

// For every x value, calculate a y value with sine function
float x = theta;
for (int i = 0; i < yvalues.length; i++) {
yvalues[i] = sin(x)*amplitude;
x+=dx;
}
}

void renderWave() {
noStroke();
fill(0);

// A simple way to draw the wave with an ellipse at each location
for (int x = 0; x < yvalues.length; x++) {
ellipse(x*xspacing, height/2+yvalues[x], 16, 16);
}
}
``````
``````
My purpose is to know how I can rotate the movement PI/4, being the axis of the movement from (-1,-1) to (1,1), i.e. upper right diagonl.

Somebody can help me for doing this.

Many thanks ti all``````

I‘m not quite sure what exactly you Need, But if you want to change the Position, frequency, height and some other things, use Desmos Graph. Just add in this sin(x) as a Variable and then change parameters. I did this some Time days ago and it works splendidly.

(sin(x * PI + (0.5 * PI)) / 2) + 0.5

(sin(x * frequency + xOffset)*amplitude)+yOffset

With these modifications how is the new code?

We encourage Users to figure out how to implement the suggestions we make, to improve their abilities and give an opportunity to improve their problem solving Skills.

But just add it where you had the other sin(x) value Applied.

Thanks four your effort, but replacing my sin expression by yours I obtain only an horizontal line. I know that I’m an absolute begginer, but I try as much as I can for learming more.

I tried with rotate and matrix push and pull. They do their work but only in a small place of the size

Could you Post, how you implemented it? Just the modified part.

``````  float x = theta;
for (int i = 0; i < yvalues.length; i++) {
yvalues[i] = (sin(x * PI + (0.5 * PI)) / 2) + 0.5*amplitude;
x+=dx;
}
}
``````

``````void draw() {
background(255);
calcWave();
translate(width/2,height/2);
rotate(PI/4);
translate(-width/2,-height/2);
renderWave();
}
``````
1 Like

Ok, maybe i misunderstood your question… Do you want to modify the sin curve?

Bingo… That is the correct one. Sin equation as original, and I hve the diagonal sinusoid

And the Upper code i Sent was just an example of how to use it with values. What you should have added was the bottom one with variables representing the various modifications you can make to it.
Ah ok, then i misunderstood what you wanted Dont’ worry. Thanks for your effort

1 Like

now you can change from sinus to triangle wave? 