Last active
January 20, 2017 05:00
-
-
Save jmorrow1/02127ae524b2699913794dedde90bada to your computer and use it in GitHub Desktop.
This file contains 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
class Fractal { | |
StringBuilder s; | |
LSystem lsystem; | |
DistFunc d; | |
float turnAngle; | |
Map<Character, Character> charMap; | |
int n = 1; | |
float tx, ty; | |
Fractal(String axiom, LSystem lsystem, DistFunc d, float turnAngle) { | |
this(axiom, lsystem, d, turnAngle, new HashMap<Character, Character>(), 0, 0); | |
} | |
Fractal(String axiom, LSystem lsystem, DistFunc d, float turnAngle, Map<Character, Character> charMap, float tx, float ty) { | |
this.s = new StringBuilder(axiom); | |
this.lsystem = lsystem; | |
this.d = d; | |
this.turnAngle = turnAngle; | |
this.charMap = charMap; | |
this.tx = tx; | |
this.ty = ty; | |
lsystem.step(s); | |
} | |
void restart(String axiom) { | |
n = 1; | |
this.s = new StringBuilder(axiom); | |
lsystem.step(s); | |
} | |
void step() { | |
lsystem.step(s); | |
n++; | |
} | |
void draw() { | |
//curve | |
float d = this.d.dist(n); | |
float x = d/2 + tx; | |
float y = d/2 + ty; | |
float angle = 0; | |
strokeWeight(1); | |
beginShape(); | |
vertex(x, y); | |
for (int i=0; i<s.length(); i++) { | |
char c = charMap.containsKey(s.charAt(i)) ? charMap.get(s.charAt(i)) : s.charAt(i); | |
switch (c) { | |
case 'F' : x += d*cos(angle); | |
y += d*sin(angle); | |
vertex(x, y); | |
break; | |
case '+' : angle += turnAngle; break; | |
case '-' : angle -= turnAngle; break; | |
} | |
} | |
endShape(); | |
//dots | |
x = d/2 + tx; | |
y = d/2 + ty; | |
strokeWeight(5); | |
point(x, y); | |
for (int i=0; i<s.length(); i++) { | |
char c = charMap.containsKey(s.charAt(i)) ? charMap.get(s.charAt(i)) : s.charAt(i); | |
switch (c) { | |
case 'F' : x += d*cos(angle); | |
y += d*sin(angle); | |
point(x, y); | |
break; | |
case '+' : angle += turnAngle; break; | |
case '-' : angle -= turnAngle; break; | |
} | |
} | |
} | |
ArrayList<Point> enumerateString() { | |
ArrayList<Point> pts = new ArrayList<Point>(); | |
float d = this.d.dist(n); | |
float x = d/2 + tx; | |
float y = d/2 + ty; | |
float angle = 0; | |
pts.add(new Point(x, y)); | |
for (int i=0; i<s.length(); i++) { | |
char c = charMap.containsKey(s.charAt(i)) ? charMap.get(s.charAt(i)) : s.charAt(i); | |
switch (c) { | |
case 'F' : x += d*cos(angle); | |
y += d*sin(angle); | |
pts.add(new Point(x, y)); | |
break; | |
case '+' : angle += turnAngle; break; | |
case '-' : angle -= turnAngle; break; | |
} | |
} | |
return pts; | |
} | |
} | |
interface DistFunc { | |
float dist(int n); | |
} |
This file contains 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
Fractal gosperCurve() { | |
String axiom = "A"; | |
DistFunc d = new DistFunc() { | |
public float dist(int n) { | |
return 6; | |
} | |
}; | |
float turnAngle = radians(60); | |
LSystem grammar = new LSystem(); | |
grammar.addRule('A', "A-B--B+A++AA+B-"); | |
grammar.addRule('B', "+A-BB--B-A++A+B"); | |
HashMap<Character, Character> charMap = new HashMap<Character, Character>(); | |
charMap.put('A', 'F'); | |
charMap.put('B', 'F'); | |
return new Fractal(axiom, grammar, d, turnAngle, charMap, width-20, height); | |
} |
This file contains 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
Fractal hilbertCurve() { | |
String axiom = "L"; | |
DistFunc d = new DistFunc() { | |
public float dist(int n) { | |
return width / pow(2, n); | |
} | |
}; | |
float turnAngle = radians(90); | |
LSystem grammar = new LSystem(); | |
grammar.addRule('L', "+RF-LFL-FR+"); | |
grammar.addRule('R', "-LF+RFR+FL-"); | |
return new Fractal(axiom, grammar, d, turnAngle); | |
} |
This file contains 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
//James Morrow | |
//github.com/jmorrow1 | |
//jamesmorrowdesign.com | |
import java.util.Map; | |
Fractal fractal; | |
void setup() { | |
size(512, 512); | |
fractal = hilbertCurve(); | |
noLoop(); | |
} | |
void draw() { | |
background(255); | |
fractal.draw(); | |
} | |
void mousePressed() { | |
fractal.step(); | |
redraw(); | |
} | |
class Point { | |
float x, y; | |
Point(float x, float y) { | |
this.x = x; | |
this.y = y; | |
} | |
} |
This file contains 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
class LSystem { | |
Map<Character, String> rules; | |
LSystem() { | |
this(new HashMap<Character, String>()); | |
} | |
LSystem(Map<Character, String> rules) { | |
this.rules = rules; | |
} | |
void step(StringBuilder s) { | |
int i=0; | |
while (i < s.length()) { | |
char c = s.charAt(i); | |
String deriv = rules.get(c); | |
if (deriv != null) { | |
s.replace(i, i+1, deriv); | |
i += deriv.length(); | |
} | |
else { | |
i++; | |
} | |
} | |
} | |
void addRule(Character c, String s) { | |
rules.put(c, s); | |
} | |
void removeRule(Character c) { | |
rules.remove(c); | |
} | |
} |
This file contains 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
Fractal peanoCurve() { | |
String axiom = "X"; | |
DistFunc d = new DistFunc() { | |
public float dist(int n) { | |
return width / pow(3, n); | |
} | |
}; | |
float turnAngle = radians(90); | |
LSystem grammar = new LSystem(); | |
grammar.addRule('X', "XFYFX+F+YFXFY-F-XFYFX"); | |
grammar.addRule('Y', "YFXFY-F-XFYFX+F+YFXFY"); | |
return new Fractal(axiom, grammar, d, turnAngle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment