Code Examples

Dec
24

Goodbye 2009


EastRiverWinter

Thank you all for a wonderful semester. I saw some really great work. Together we discussed typography, some graphic design, and of course Processing. Ah… data visualization. I hope you’ve felt productive and have a wonderful break. And waste some time with Browser Pong.

Code Examples
Here’s a reverse-chronological list (hopefully somewhat complete) of code examples from this past semester. This may serve as a helpful reference for you when trying to quickly solve (Rubik’s Cube style) problems in the future. Maybe.

Midpoint Circle Algorithm
Thank you for posting this Chris. See also http://en.wikipedia.org/wiki/Midpoint_circle_algorithm

2D clicks in a 3D world
This has come up a few times in class, “How do I do a mouseover or mouseclick on a rotating / spinning object in 3D space?” The above is just a collection of links to Processing topics that will help you answer that question.

Textures [via Processing]
Processing’s guide to textures.

Presenter
Paths, Agents, Scenes… This little package allows you to really build up a presentation that can translate, rotate, and animate along nested timelines. Great for stitching together multiple complex visualizations.

Data Ripping
Using fuzzy found image data to your benefit. Hue and Saturation are usually key here.

Making a Mystery Image
A brief program for making the scrambled RGB (XYZ) data image for the previous homework assignment.

Decomposing Images
This is the “Mystery Image” (“Space Neighbors”) assignment. PImages, loadImages(), OpenGL, etc.

MSAFuild Dynamics
(Thanks for pointing out this fluid dynamics library Peter.)

Weather Example 2
Use a Class and ArrayList to create many objects. Grab weather XML, animate many objects in 3D, pause, save frames, etc.

Tim’s QualityCam
An ITP homebrew camera library for Processing courtesy of Tims.

Peasy 3D Camera
Processing Library for manipulating the 3D camera.

Weather Example 1
Grab weather XML, animate a simple shape in 3D, pause, save frames, etc.

Push / Pop Matrix
Use pushMatrix() and popMatrix() to add layers of control to your rotations and translations in 3D space.

Translate and Rotate
Position and orient objects in 3D space using OpenGL.

Weather RSS
Grab an RSS (XML) feed and search for Content and Attribute values inside.

Robert Penner’s Easing
Processing library for easing motions like ease-in, ease-out, bounce, etc.

Text and CSV
Parse CSV files from your sketch’s “/data” directory.

Theme and Variation
Visualize two hard-coded variables.

And of course, this should be bookmarked in your browser :
http://processing.org/reference

Dec
03

MOTHER F*CKING MID POOOOOOIINTTT!!!


Ok, first off, here is my issue:

1) I have two distinct points on a globe and i want to draw a line between them at a nice arc.

From what I understand there are two solutions:

a) calculate the x, y, z of those boxes and then draw an arc between them.  This turns out to be incredibly difficult, but using screenX, screenY, and screenZ is a start.

You use the mid point equation:

xMid = (x1 + x2)/2
yMid = (y1 + y2)/2
zMid = (z1 + z2)/2

and *snoooooorreeee*

f*ck math, who cares…

2) My way, abstractly, check code for exact solution, is to calculate the midpoint of the long, lat coordinates and recursively plot points over and over till you have a line.  Here is the meat of that:

float lat1 = radians( -34 ); float lon1 = 151; // sydney
float lat2 = radians( 37 ); float lon2 = 127; // seol

float dLon = radians( lon2 - lon1 );

float Bx = cos( lat2 ) * cos( dLon );
float By = cos( lat2 ) * sin( dLon );

float lat3 =  atan2( sin( lat1 ) + sin( lat2 ),
                   sqrt( ( cos( lat1 ) + Bx ) *
                   ( cos( lat1 ) + Bx ) + By * By ) );

float lon3 = radians( lon1 ) + atan2( By, cos( lat1 ) + Bx );

At this point you essentially done. Just rotate twice and translate once and draw the f*cking box or point or whatever.

To finish off the line just do this over and over again recalculating the midpoint. You might calculate the number of times to recurse based on the distance which is calculated:

float dLon = radians( lon2 - lon1 );

Animate it? No f*cking problem. Just store longs and lats as you go and animate the drawing of the boxes!

– chris allick

Nov
21

The Midpoint Circle Algorithm


A few classes ago I mentioned this alrogirthm as an alternative method for drawing a circle. It’s very interesting and very very fast.

http://en.wikipedia.org/wiki/Midpoint_circle_algorithm

– chris allick

Nov
12

A 2D Click in a 3D World


A few of us were discussing clicking on objects or activating rollovers using the 2D mouse coordinates (via mouseX and mouseY) in a 3D Processing sketch. The answer? It’s screenX and screenY. (There’s also a screenZ but…) And here’s an old discussion of the problem from 2005.

Also of interest, modelX, modelY, and modelZ.

Nov
08

Textures


We haven’t talked much about textures, but you may find this technique useful for your presentation. Here’s Processing’s guide to Textures. Note also that you can load PNGs with transparency as textures, allowing you to do some pretty interesting stuff.

Nov
07

Presentor


presenter

Here’s a simple demo that uses Scenes: presentor1.zip

Recall our discussion last class that Scenes are made of Agents, Agents are made of Paths, and this allows everything to flow. Scenes are great because they allow you to program separate layers of animation with their own internal timelines. You can take that entire scene and move it in XYZ space, rotate it, and play with the timeline.

Continue reading →

Nov
07

Data Ripping


dataRipping2

How about another super quick demo of grabbing a “found” data image and displaying it differently? If the code seems overly familiar it’s because we’re not far off from a re-hashing of the Space Neighbors sample code. (That should be cause for comfort, no?) One important thing to note is how I’m using the built-in hue() function to sample data from the image. (I highly recommend looking at Wikipedia’s description of HSB color space.) Here it is: dataRipping.zip

Oct
29

Making a Mystery Image


makeMystery

There was a request in class to see the code I wrote for making the “mystery.png” image used in the last homework assignment. It’s quite brief. I chose 48 × 48 because it seemed like it would run reasonably speedily on most machines. (Rather than say, 1920 × 1080 pixels, which would bring everything to a halt!)

makeMystery.zip
Also see Rune’s version; My Own Image.

Oct
23

Homework: Decomposing Images


imageAsData

This week’s assignment focuses on some programming techniques in Processing. Some of you have already created or used Classes in your programs, represented as additional tabs in your Processing sketch. (My latter Weather Example Code used a Class called “Agent” to create and draw many moving red boxes.) For this assignment Classes (and Objects) are a requirement. If you’re still not comfortable with Classes and Objects please read the Processing.org Tutorial on Objects. And don’t hesitate to email me with questions.

Continue reading →

Oct
16

Sample Code Roundup


For convenience here’s a roundup of the sample code we’ve passed around so far this semester. If I’ve missed something just add a link in the comments.

Continue reading →