Category Archives: teaching_
Microwave Festival Workshop
I’m in Hong Kong teaching a one week workshop modeled off of the nature of code course.
I’m a little jetlagged, but ready to go!
GA Shakespeare in Finnish
My genetic algorithm example from the nature of code has been translated to Finnish and posted at http://www.nokturno.org/index.php?poeetta=shiffman. What a great site! Lots of interesting text-based digital work there.
Writing and Blinking
Finally started cranking on what will hopefully become an intro level book on programming with Processing. Yet, the distraction of 144 LEDs keeps staring me in the face, taunting and seducing me. . .
MovieMaker Processing Library
Update: MovieMaker is now part of Processing’s core video library so this post is now irrelevant!
Based on some code from Shawn Van Every and Dan O’Sullivan, I’ve created a Processing library that allows you to take any pixel array (from the window or from a PImage) and create a quicktime movie.
E-mail me bug reports, questions, comments, etc.!
Example code:
import moviemaker.*; MovieMaker mm; float theta = 0; void setup() { size(200,200); mm = new MovieMaker(this,width,height,"p5test.mov", MovieMaker.JPEG, MovieMaker.HIGH,30); } void draw() { background(sin(theta)*127+127,255,cos(theta)*127+127); theta += 0.05; loadPixels(); mm.addFrame(pixels,width,height); } public void mousePressed() { mm.finishMovie(); }
Available constants:
code: RAW, JPEG, CINEPAK, SORENSON, VIDEO
quality: LOW, MEDIUM, HIGH, BEST
ITP Press
After a long battle to the death with Adobe InDesign and breaking the heart of our good friend, the Phaser 8400, the inaugural publication of the newly founded “ITP Press” is finally here. The booklet documents the work of students in my Programming from A to Z class.
Simple JPG creation from Processing
By popular demand. . .
// Daniel Shiffman // http ://www.shiffman.net // May 2006 // Create JPG import javax.imageio.*; PImage img; void setup() { size(200,200); // Draw a blank PImage img = new PImage(200,200); img.loadPixels(); for (int i = 0; i < img.pixels.length; i++) img.pixels[i] = color(100,50,250); img.updatePixels(); saveJPG(img,"image.jpg"); image(img,0,0); noLoop(); } public void saveJPG(PImage img, String f) { String filename = f; // Create BufferedImage from PImage BufferedImage bImg = new BufferedImage(img.width, img.height, BufferedImage.TYPE_INT_RGB); bImg.setRGB(0,0,img.width,img.height,img.pixels,0,img.width); String filepath = sketchPath + "/" + filename; File file = new File(filepath); try { ImageIO.write(bImg, "jpg", file); System.out.println("Created JPG: " + filepath); } catch (IOException e) { System.out.println("Problem creating JPG: " + filepath); e.printStackTrace(); } }
Optimize this

If you look at my nature of code flocking example, you’ll notice that the algorithm involves calculating the distance between all of the system’s elements, i.e. for every “boid”, check and see how far away it is from every other boid. This works quite well for, say, around 100 elements.
100 x 100 = 10,000 cycles. No problem.
To check 1,000 elements:
1,000 x 1,000 = 1,000,000 cycles. Ok, but getting a bit slow. . .
Let’s try 2,000 elements:
2,000 x 2,000 elements = 4,000,000 cycles. Now, we’re really getting slow. Really, see how slow it is.
What if we could divide the screen into a grid of arbitrary size, take all 2,000 elements, and each object’s location within that grid? Then, we could simply test the objects that were in the same relative area at any given moment. Imagine a 10 x 10 grid. In a system of 2,000 elements, on average, approximately 20 elements would be found in each cell (20 x 10 x 10 = 2,000).
Each cell would then require 20 x 20 = 400 cycles. With 100 cells, we’d have 100 x 400 = 40,000 cycles, a massive savings over 4,000,000.
Here’s an example that implements this optimization. It’s a great deal more efficient than this slow, pathetic version. The code could use some fixing up and could be improved to be more modular / object oriented, but it’s a nice start for solving these types of problems. We could also use a potentially more efficient data structure than an ArrayList for every cell.
Vector3D Library
So I’m really a doo-doo head for not bothering to demonstrate this sooner. . . but you can now use my Vector3D class as a library. Of course, this isn’t nearly as sophisticated as what you might, say, be able to do with traer.physics, but since this class is used in virtually all of my Nature of Code examples, it’s not such a nice thing to have to keep adding it to each project over and over and over and over again. . .
Threads and P5 libraries
Now online: tutorial on threads and making Processing libraries that invoke event methods in the parent applet. Can you find the most errors?






