/* This example is a bit more mathematically complicated than previous ones. Here we define parameters that allow us to graph an equation in 2 dimensions. To do so we need to define the relationship between the cartensian plane of the equation and the screen. */ void setup() { // setup our environment size(300, 300); } void draw() { // load the pixels[] array loadPixels(); // scale our mouse X position to a number within our cartesian plane float action = (mouseX * 10.0) / width; // define the width and height of our cartesian plane float w = 16.0f; float h = 16.0f; // determine what the width and height of a single pixel is in our // plane float dx = w / width; float dy = h / height; // start at the left of our cartesian plane float x = -w / 2; // iterate over the width of the screen for (int i = 0; i < width; i++) { // start at the top of our cartesian plane float y = -h / 2; // iterate over the height of our screen for (int j = 0; j < height; j++) { // map our cartesian plane into polar coordinates so // we can do some interesting things with sin and cosine float r = sqrt( (x * x) + (y * y)); float theta = atan2(y, x); // choose an equation we want to graph //float val = cos(r); //float val = sin(theta); //float val = sin(r); //float val = cos(theta); float val = sin(action * cos(r) + 5 * theta); // scale the result of our equation (-1 to 1) to a color value (0 to 255) // and set our pixel to that grayscale color pixels[i + j * width] = color( (val + 1.0f) * 255.0f / 2.0); // move down in our cartesian plane y += dy; } // move right in our plane x += dx; } // draw our pixels[] array to the screen updatePixels(); }