Created
June 1, 2014 17:52
-
-
Save chiral/34b4aa54f3db95089084 to your computer and use it in GitHub Desktop.
Mondorian art in Processing.js.
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
<html> | |
<body> | |
<script src="./processing.min.js"></script> | |
<canvas datasrc="./mondorian.pjs"></canvas> | |
</body> | |
</html> |
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
int W = 800; | |
int H = 600; | |
int count = 1; | |
int margin = 50; | |
void setup(){ | |
size(W,H); | |
frameRate(1); | |
} | |
class Line { | |
int typ,pos; | |
int from,to; | |
Line(int t, int p) { | |
typ = t; | |
pos = p; | |
from = 0; | |
if (t==1) to=H; | |
if (t==2) to=W; | |
} | |
boolean check(ArrayList lines) { | |
ArrayList cross = new ArrayList(); | |
for (int i=0; i<lines.size(); i++) { | |
Line line = lines.get(i); | |
if (line.typ == typ) { | |
if (abs(line.pos-pos) < margin) | |
return false; | |
} else { | |
if (pos>=line.from && pos<=line.to) { | |
cross.add(line.pos); | |
} | |
} | |
} | |
int j = int(random(cross.size()-1)); | |
int k = (j+1)+int(random(cross.size()-1-j)); | |
from = cross.get(j); | |
to = cross.get(k); | |
return true; | |
} | |
void draw() { | |
if (typ==1) { | |
line(pos,from,pos,to); | |
} | |
if (typ==2) { | |
line(from,pos,to,pos); | |
} | |
} | |
void print(String s) { | |
println(s+": type="+typ+",pos="+pos+",from="+from+",to="+to); | |
} | |
} | |
void myfill(ArrayList lines, int w, int x, int y) { | |
Line up,down,left,right; | |
for (int i=0; i<lines.size(); i++) { | |
Line line = lines.get(i); | |
if (line.typ==1) { | |
if (x>line.pos && y>line.from && y<line.to) { | |
if (left==null || left.pos<line.pos) { | |
left = line; | |
} | |
} | |
if (x<line.pos && y>line.from && y<line.to) { | |
if (right==null || right.pos>line.pos) { | |
right = line; | |
} | |
} | |
} else { | |
if (y>line.pos && x>line.from && x<line.to) { | |
if (up==null || up.pos<line.pos) { | |
up = line; | |
} | |
} | |
if (y<line.pos && x>line.from && x<line.to) { | |
if (down==null || down.pos>line.pos) { | |
down = line; | |
} | |
} | |
} | |
} | |
if (up==null || down==null || | |
left==null || right==null) return; | |
int col = int(random(4)); | |
if (col==0) fill(0,0,0); | |
else if (col==1) fill(200,0,0); | |
else if (col==2) fill(0,0,200); | |
else if (col==3) fill(200,200,0); | |
rect(left.pos,up.pos,right.pos-left.pos,down.pos-up.pos); | |
} | |
void draw() { | |
count--; | |
if (count>0) return; | |
count = 1; | |
background(255); | |
w = random(10,16); | |
N = max(160/w,4); | |
ArrayList lines = new ArrayList(); | |
lines.add(new Line(1,0)); | |
lines.add(new Line(1,W)); | |
lines.add(new Line(2,0)); | |
lines.add(new Line(2,H)); | |
for (int i=0; i<N; i++) { | |
int t,p; | |
if (i<2 || random(0,2)>1) { | |
t = 1; | |
} else { | |
t = 2; | |
} | |
if (t==1) p = int(random(0,W)); | |
if (t==2) p = int(random(0,H)); | |
Line line = new Line(t,p); | |
if (line.check(lines)) { | |
lines.add(line); | |
} | |
} | |
noStroke(); | |
int c = lines.size(); | |
int M = int(random(c/4,c/3)); | |
for (int i=0; i<M; i++) { | |
myfill(lines,w,random(W),random(H)); | |
} | |
strokeWeight(w); | |
stroke(0); | |
for (int i=4; i<lines.size(); i++) { | |
Line line = lines.get(i); | |
line.draw(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment