This assignment was about sine waves and application in code.
In these 2 sketches Im using the sine to control the shape and location.
Oscillation from Oryan Inbar on Vimeo.
Tail creature
Wave wave;
void setup() {
size(750,200);
// Initialize a wave with starting point, width, amplitude, and period
wave = new Wave(new PVector(width/2, height/2), 100, 20, 500); //location.x,location.y instead of hard coded no.
}
void draw() {
background(0, 130, 210);
// Update and display waves
wave.calculate();
wave.display();
//wave.update();
}
class Wave {
int xspacing = 8; // How far apart should each horizontal location be spaced
int w; // Width of entire wave
PVector origin; // Where does the wave’s first point start
float theta = 0.0; // Start angle at 0
float amplitude; // Height of wave
float period; // How many pixels before the wave repeats
float dx; // Value for incrementing X, to be calculated as a function of period and xspacing
float[] yvalues; // Using an array to store height values for the wave (not entirely necessary)
//vectores:
PVector location;
PVector velocity;
PVector acceleration, mouse;
float mass;
Wave(PVector o, int w_, float a, float p) {
origin = o.get();
w = w_;
period = p;
amplitude = a;
dx = (TWO_PI / period) * 2 * xspacing;
yvalues = new float[w/xspacing];
//make vectores for the movement:
location = new PVector (origin.x, origin.y);
velocity = new PVector(0, 0);
mass = 1;
}
void update() {
mouse = new PVector(mouseX, mouseY);
PVector acceleration = PVector.sub(mouse, location);
acceleration.setMag(0.2);
velocity.add(acceleration);
velocity.limit(5);
location.add(velocity);
origin = location.get();
acceleration.mult(0);
}
void calculate() {
// 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 display() {
float angle = atan2(velocity.y, velocity.x);
// connecting each location by beginShape
beginShape();
for (int x = 0; x < yvalues.length; x++) {
strokeWeight(2);
stroke(100, 30, 40);
vertex(location.x+x*xspacing, location.y+yvalues[x]); //why???
//println(location.x, location.y, origin.x, origin.y);
strokeWeight(6);
stroke(100, 30, 40, 10);
fill(100, 30, 40);
ellipseMode(CENTER);
float breathe = constrain(yvalues[0], 5, 30);
breathe += 30;
ellipse (origin.x+13*xspacing, yvalues[11] + 95, breathe, breathe );
// ellipse(location.x,location.y,48,48);
}
endShape();
}
}
Snake oscillation
class Wave {
int xspacing = 8; // How far apart should each horizontal location be spaced
int w; // Width of entire wave
PVector origin; // Where does the wave’s first point start
float theta = 0.0; // Start angle at 0
float amplitude; // Height of wave
float period; // How many pixels before the wave repeats
float dx; // Value for incrementing X, to be calculated as a function of period and xspacing
float[] yvalues; // Using an array to store height values for the wave (not entirely necessary)
//vectores:
PVector location;
PVector velocity;
PVector acceleration, mouse;
float mass;
Wave(PVector o, int w_, float a, float p) {
origin = o.get();
w = w_;
period = p;
amplitude = a;
dx = (TWO_PI / period) * 2 * xspacing;
yvalues = new float[w/xspacing];
//make vectores for the movement:
location = new PVector (origin.x, origin.y);
velocity = new PVector(0, 0);
mass = 1;
}
void update() {
mouse = new PVector(mouseX, mouseY);
PVector acceleration = PVector.sub(mouse, location);
acceleration.setMag(0.2);
velocity.add(acceleration);
velocity.limit(5);
location.add(velocity);
origin = location.get();
acceleration.mult(0);
}
void calculate() {
// 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 display() {
float angle = atan2(velocity.y, velocity.x);
// connecting each location by beginShape
beginShape();
for (int x = 0; x < yvalues.length; x++) {
strokeWeight(2);
stroke(100, 30, 40);
vertex(location.x+x*xspacing, location.y+yvalues[x]); //why???
//println(location.x, location.y, origin.x, origin.y);
strokeWeight(6);
stroke(100, 30, 40, 10);
fill(100, 30, 40);
ellipseMode(CENTER);
float breathe = constrain(yvalues[0], 5, 30);
breathe += 30;
ellipse (origin.x+13*xspacing, yvalues[11] + 95, breathe, breathe );
// ellipse(location.x,location.y,48,48);
}
endShape();
}
}