Processing.org: Sick Sine Wave
Armin's Forays into Processing
Recently, I have started spending some of my evenings playing around with Processing, just because it's AWESOME!
Note: Java has to be enabled to see the applets. The applets might not run on all computers. It depends very much on the JAVA version installed. (It works on all my Macs, but I needed to update JAVA on one of the PCs in the lab to make it work.)
Sick Sine Wave
My very first interactive applet created in Processing. The speed and spatial frequency of this sinusoidal grating can be influenced by moving the mouse over the animation. X controls speed, Y controls the number of periods shown. I also made a version (based on Tim Igoe's code) that listens to serial input from an Arduino attached to my computer to contol the speed of the motion. I find it very exciting to be able to control what's happening on my computer screen by turning the knob of a potentiometer attached to my Arduino...
Java Applet
Note: You might have to click onto the applet to interact with it.
Source code: Sick_Sine_Wave
Built with Processing
Source code
You can play around with this code by copying the code below into your Processing IDE (download Processing from http://processing.org/.)
float scaleVal = 150; // max value for the resulting sine wave (255 = white) float periods = 4; // how many periods per window width (will be controlled by mouse Y) float colorValue; // holds the result of the sine wave color for each position float sine_phase = PI; float increment = PI/8; // how much the phase should shift / redraw; controlled by mouse X. void setup () { //size(screen.width, screen.height); // uncomment: go full screen size(300,300); smooth(); // anti-aliasing frameRate(30); // set frame rate } void draw() { displaySine(sine_phase, periods); // call the display function increment = map(mouseX, 0, width, PI/4,-PI/4); // mouse X controls speed of wave periods = map(mouseY, 0, height, 2, 12); // mouse Y controls how many periods / screen width sine_phase = sine_phase + increment; // increment phase for next drawing loop if (sine_phase >= 2*PI) { sine_phase = 0.0; } } // function that displays the sine wave (takes phase and periods as arguments): void displaySine(float sine_phase, float periods) { for (int x = 0; x < (width); x++) { float xValue = x/float(width)*periods; // location along x axis colorValue= ( (sin(2*PI*xValue + sine_phase) * scaleVal) + scaleVal ) / 2.0; // calculate grey value for each x position stroke(colorValue); // set color to that value line(x,0, x, height); // draw vertical line for each x } }