RosettaCodeData/Task/Hilbert-curve/Processing/hilbert-curve.processing

84 lines
1.5 KiB
Plaintext

int iterations = 7;
float strokeLen = 600;
int angleDeg = 90;
String axiom = "L";
StringDict rules = new StringDict();
String sentence = axiom;
int xo, yo;
void setup() {
size(700, 700);
xo= 50;
yo = height - 50;
strokeWeight(1);
noFill();
rules.set("L", "+RF-LFL-FR+");
rules.set("R", "-LF+RFR+FL-");
generate(iterations);
}
void draw() {
background(0);
translate(xo, yo);
plot(radians(angleDeg));
}
void generate(int n) {
for (int i=0; i < n; i++) {
strokeLen *= 0.5;
String nextSentence = "";
for (int j=0; j < sentence.length(); j++) {
char c = sentence.charAt(j);
String ruleResult = rules.get(str(c), str(c));
nextSentence += ruleResult;
}
sentence = nextSentence;
}
}
void plot(float angle) {
for (int i=0; i < sentence.length(); i++) {
char c = sentence.charAt(i);
if (c == 'F') {
stroke(255);
line(0, 0, 0, -strokeLen);
translate(0, -strokeLen);
} else if (c == '+') {
rotate(angle);
} else if (c == '-') {
rotate(-angle);
}
}
}
void keyPressed() {
if (key == '-') {
angleDeg -= 1;
println("Angle: " + angleDeg);
}
if (key == '=' || key == '+') {
angleDeg += 1;
println("Angle: " + angleDeg);
}
if (key == 'a') {
strokeLen *= 2;
}
if (key == 'z') {
strokeLen /= 2;
}
if (keyCode == LEFT) {
xo -= 25;
}
if (keyCode == RIGHT) {
xo += 25;
}
if (keyCode == UP) {
yo -= 25;
}
if (keyCode == DOWN) {
yo += 25;
}
}