Created
November 6, 2011 09:54
-
-
Save mharju/1342709 to your computer and use it in GitHub Desktop.
Just a quick test to make fancy images out of b/w triangles. I fully agree on the lack of mathematical elegance. Maybe I'll clean this up sometime. Or then not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Patchwork | |
void setup() | |
{ | |
size(640, 640, P3D); | |
noStroke(); | |
initialize(); | |
} | |
void initialize() | |
{ | |
int count = (int) (1 / s); | |
count *= count; | |
current = new ArrayList( count ); | |
cx = width / 2; cy = height / 2; | |
for(int i=0;i<count;i++) | |
{ | |
current.add(i, 0); | |
} | |
} | |
void triangle() | |
{ | |
pushMatrix(); | |
beginShape(TRIANGLES); | |
fill(255); | |
vertex(width, height, 0); | |
vertex(0, height, 0); | |
vertex(0, 0, 0); | |
fill(0); | |
vertex(width, 0, 0); | |
endShape(); | |
popMatrix(); | |
} | |
float cx, cy; | |
void triangleWithPosition(int p) | |
{ | |
pushMatrix(); | |
translate(cx, cy, 0); | |
rotateZ(TWO_PI / 4. * p); | |
//rotateZ(radians(frameCount)); | |
translate(-cx, -cy, 0); | |
triangle(); | |
popMatrix(); | |
} | |
void setPosition(int tile) | |
{ | |
current.set(tile, ((Integer)current.get(tile) + 1) % 4); | |
} | |
void mouseClicked() | |
{ | |
int numTiles = (int)(1 / s); | |
int x = (int)Math.floor(((float)mouseX / width) * numTiles); | |
int y = (int)Math.floor(((float)mouseY / height) * numTiles); | |
setPosition( y * numTiles + x ); | |
} | |
void keyPressed() | |
{ | |
if(key == 'a') | |
{ | |
s *= 0.5; | |
initialize(); | |
} else if(key == 'z' && s < 1) | |
{ | |
s /= 0.5; | |
initialize(); | |
} else if(keyCode == KeyEvent.VK_SPACE) | |
{ | |
int n = (int)random(0, current.size()); | |
setPosition(n); | |
} | |
} | |
ArrayList current; | |
float s = 0.5; | |
void draw() | |
{ | |
int c = 0; | |
background(0); | |
scale(s, s); | |
for(int j=0;j<height;j+=height*s) { | |
for(int i=0;i<width;i+=width*s,c++) { | |
pushMatrix(); | |
translate(i/s, j/s, 0); | |
triangleWithPosition( (Integer)current.get(c) ); | |
popMatrix(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://play.taiste.fi/random/triangles.png for a sample image.