Processing Code Snippets

Processing is an open source programming language and environment for people who want to create images, animations, and interactions. Initially developed to serve as a software sketchbook and to teach fundamentals of computer programming within a visual context, Processing also has evolved into a tool for generating finished professional work. Today, tens of thousands of students, artists, designers, researchers, and hobbyists who use Processing for learning, prototyping, and production.

/*
Like most graphics systems, the canvas element differs from the usual mathematical coordinate plane:

The origin is in the top left corner.
The code above compensates by assuming that h, and k are actually relative to the top left.
The y axis is inverted. Positive y is down the screen, not up. To correct for this, the k variable (the y coordinate of the center) must be positive to place the center some way down the screen.

Also the y calculation has to subtract the sin(x) term instead of add.
Marked in the code as Note 1.

Note 2. The step size is set to an exact division of 2π to avoid gaps or over-runs in the circle. This code divides the circle into exactly 20 segments.
Note too that as in most computer languages, the trig functions operate in radians, not degrees.
360° = 2π radians.
*/

// We must keep track of our position along the curve float arclength = 0;

// For every box
for (int i = 0; i < message.length(); i++)
{ // Instead of a constant width, we check the width of each character. char currentChar = message.charAt(i); float w = textWidth(currentChar);

// Each box is centered so we move half the width
arclength += w/2; // Angle in radians is the arclength divided by the radius
// Starting on the left side of the circle by adding PI float theta = PI + arclength / r;

//https://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-processing/
//16. high resolution output
//Sometimes you only need to draw to the screen. Sometimes you want to save that output.
//Sometimes you need to have that output in a very high resolution, for example when you
//want to print it. There are different ways to generate high resolution output.
//One can use the PDF export options. Or one could use one of the many different ‘hacks’
//to create a high resolution copy of the regular draw() loop.
//Here is an example of one such technique. voidsetup() {
size(100, 100);
}

//pixel array method
img.loadPixels(); //address the pixels directly, hello pixels!!!for (int p=0; p<img.width*img.height; p++) { //loop through all the pixelsif (img.pixels[p]==checkColor) {//if the pixel has the color we are checking for
img.pixels[p]=setColor; //replace it with the new color
}
}
img.updatePixels(); //then update the pixelsimage(img, 0, 0); //now show the imagesave("new-grid.gif");
}