Archive for the 'code snippets' Category

Processsing Snippet

Wednesday, March 17th, 2010
JAVA:
  1. import processing.pdf.*;
  2.  
  3. int pointNum = 14;
  4. Point[] points = new Point[pointNum];
  5. void setup(){
  6.   size(500,500, JAVA2D)
  7.   beginRecord(PDF, "shape.pdf");
  8.   translate(width / 2, height / 2);
  9.   background(255);
  10.   strokeWeight(0.5);
  11.   smooth();
  12.   noFill();
  13.   addPoints();
  14.   connectPoints();
  15.   endRecord();
  16. }
  17.  
  18. void addPoints(){
  19.   float step = TWO_PI / pointNum;
  20.   float theta = step / 2;
  21.   float radius = 200;
  22.   for (int i = 0; i<pointNum; i++){
  23.      float x = radius * cos(theta);
  24.      float y = radius * sin(theta);
  25.      points[i] = new Point(x, y);
  26.      theta += step;
  27.   }
  28. }
  29. void connectPoints(){
  30.    for (int i = 0; i<pointNum; i++){
  31.      Point a = points[i];
  32.      for (int j = 0; j<pointNum; j++){
  33.         Point b = points[j];
  34.         if (a != b){
  35.            //if (int(a.x) == int(b.x) || int(a.y) == int(b.y)){
  36.              line(a.x, a.y, b.x, b.y);
  37.           // }
  38.         }
  39.      }
  40.    }
  41. }
  42.  
  43. class Point{
  44.   float x;
  45.   float y;
  46.   Point(float x, float y){
  47.     this.x = x;
  48.     this.y = y;
  49.    // ellipse(x,y, 10, 10);
  50.   }
  51. }

Connect the dots, try changing pointNum.

Actually have a good deal more to say about this snippet... maybe in the next day or two.

Gesture Capture

Thursday, March 11th, 2010

Gesture Capture is a mini-drawing program that allows the user to draw single gestures (shapes, letters etc...) - the program then randomly scales, rotates, tints and translates these gestures repeatedly on the canvas. The user may continue to draw as it does this, the more gesture drawn, the more the program will have to randomly choose from.


Try Gesture Capture:

I created Gesture Capture as an entry for actionsnippet and something made me think of it today. I was playing with it for awhile and decided it was worth mentioning here.

The original post and code can be found here

Polar Coordinates, Sine, Cosine…

Monday, October 13th, 2008

One of my many favorite equations is the conversion from polar coordinates to cartesian... in actionscript it looks like this:

Actionscript:
  1. x = radius * Math.cos(theta);
  2. y = radius * Math.sin(theta);

I was teaching the ins and outs of polar coordinates to my students yesterday and thought of a good assignment for them... "Use Graphics or BitmapData to create an interactive sketch involving the polar to cartesian conversion." Now, when I first encounted this equation, I didn't really know what it was or why it worked... I used it for circular motion in Director... I also tried randomly tweaking it to see what kind of results I could get.... things like this:

Actionscript:
  1. // sort of like a figure 8
  2. x = radius * Math.cos(theta);
  3. y = radius * Math.sin(theta/2);
  4.  
  5. // an oval
  6. x = radius/2 * Math.cos(theta);
  7. y = radius * Math.sin(theta);
  8.  
  9. // etc...
  10. x = radius * Math.cos(theta);
  11. y = radius * Math.sin(theta) + radius * Math.cos(theta * 2);

I advocate this kind of random experimentation when I teach game design or talk to students interested in generative visuals. Playing with the numbers just improves understanding... even if you don't know what your playing with at first...

When I got home yesterday I created this small sketch:


To see the flash version continue reading this post....

Read the rest of this entry »