Created
July 15, 2013 03:32
-
-
Save jordanorelli/5997313 to your computer and use it in GitHub Desktop.
quadratic koch island in Processing, as in The Algorithmic Beauty of Plants.
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
void setup() { | |
size(500, 500); | |
smooth(); | |
background(255); | |
stroke(0); | |
strokeWeight(1); | |
HashMap productions = new HashMap(); | |
productions.put('F', "F-F+F+FF-F-F+F"); | |
String axiom = "F-F-F-F"; | |
for (int i = 0; i < 3; i++) { | |
axiom = gen(axiom, productions); | |
} | |
HashMap commands = new HashMap(); | |
commands.put('F', F); | |
commands.put('f', f); | |
commands.put('-', minus); | |
commands.put('+', plus); | |
Turtle t = new Turtle(width * 0.25, height * 0.75, PI * 0.5); | |
t.run(commands, axiom); | |
} | |
String gen(String input, HashMap productions) { | |
StringBuffer buf = new StringBuffer(); | |
for (int i = 0; i < input.length(); i++) { | |
char c = input.charAt(i); | |
if (!productions.containsKey(c)) { | |
buf.append(c); | |
continue; | |
} | |
buf.append((String)productions.get(c)); | |
} | |
return buf.toString(); | |
} |
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
float D = 4.0; | |
class Turtle { | |
PVector position; | |
float heading; | |
HashMap commands; | |
Turtle(float x, float y, float heading) { | |
this.position = new PVector(x, y); | |
this.heading = heading; | |
} | |
void run(HashMap commands, String program) { | |
for (int i = 0; i < program.length(); i++) { | |
char c = program.charAt(i); | |
if (!commands.containsKey(c)) continue; | |
TurtleCommand cmd = (TurtleCommand)commands.get(c); | |
cmd.run(this); | |
} | |
} | |
} | |
abstract class TurtleCommand { | |
public abstract void run(Turtle t); | |
} | |
TurtleCommand F = new TurtleCommand() { | |
public void run(Turtle t) { | |
float x = t.position.x + D * cos(t.heading); | |
float y = t.position.y - D * sin(t.heading); | |
line(t.position.x, t.position.y, x, y); | |
t.position.x = x; | |
t.position.y = y; | |
println("move forward and draw"); | |
} | |
}; | |
TurtleCommand f = new TurtleCommand() { | |
public void run(Turtle t) { | |
t.position.x += D * cos(t.heading); | |
t.position.y -= D * sin(t.heading); | |
println("move forward"); | |
} | |
}; | |
TurtleCommand plus = new TurtleCommand() { | |
public void run(Turtle t) { | |
t.heading += PI * 0.5; | |
println("turn left"); | |
} | |
}; | |
TurtleCommand minus = new TurtleCommand() { | |
public void run(Turtle t) { | |
t.heading -= PI * 0.5; | |
println("turn right"); | |
} | |
}; |
Author
jordanorelli
commented
Jul 15, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment