I have code that draws a multi-colored spiral pattern with different line lengths and colors depending on which number keys are pressed. I would like to change this to reading the numbers from an array, but I am having some trouble. I am new to processing so my code contains lines that I do not understand (added due to your helpful suggestions, Processing collective – many thanks). The keypressed code is as follows:
int lineSeg=10; //length of line segment
int xPos=0;
int yPos=0;
String exec=""; // A String exec gets recorded (in keyPressed()) and then evaluated (executed) in evaluateInputs().
void setup() {
size(800,800);
background(#FFFFFF);
strokeWeight (3);
}
//translate
//rotate(-HALF_PI);
void draw () {
translate(width/2, height/2);
rotate (-HALF_PI);
evaluateInputs();
}
void keyPressed() {
if (key == '1' ||
key == '2' ||
key == '3' ||
key == '4' ||
key == '5' ||
key == '6' ||
key == '7' ||
key == '8' ||
key == '9' ||
key == '0' ) {
exec += key; //record keys
}
}
void evaluateInputs() {
//evaluate recorded string
for (int i=0; i < exec.length(); i++) {
float amtValue = map(i, 0, exec.length(), 0, 1);
switch (exec.charAt(i)) {
case '1':
stroke(#F02947);//red
rotate(-HALF_PI);
tLine(lineSeg);
break;
case '2':
stroke(#F029D6);//fucia
rotate(-HALF_PI);
tLine(2*lineSeg);
break;
case '3':
stroke(#9229F0);//purple
rotate(-HALF_PI);
tLine(3*lineSeg);
break;
case '4':
stroke(#293EF0);//blue
rotate(-HALF_PI);
tLine(4*lineSeg);
break;
case '5':
stroke(#29E1F0);//turquoise
rotate(-HALF_PI);
tLine(5*lineSeg);
break;
case '6':
stroke(#29F077);//lime green
rotate(-HALF_PI);
tLine(6*lineSeg);
break;
case '7':
stroke(#549B05);//moss
rotate(-HALF_PI);
tLine(7*lineSeg);
break;
case '8':
stroke(#EFF029);//yellow
rotate(-HALF_PI);
tLine(8*lineSeg);
break;
case '9':
stroke(#F09029);//pumpkin
rotate(-HALF_PI);
tLine(9*lineSeg);
break;
case '0':
stroke(0);//black
fill(0);
ellipse(0, 0, lineSeg, lineSeg);
break;
} //switch
} //for
} //funct
void tLine(int x) {
tLineDraw(x);
}
void tLineDraw(int x){
line(0, 0, x, 0);
translate(x, 0);
}
My attempt to switch this code over to reading from an array is here:
int[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int lineSeg=10; //length of line segment
int xPos=0;
int yPos=0;
void setup() {
size(800,800);
background(#FFFFFF);
strokeWeight (3);
}
//translate
//rotate(-HALF_PI);
void draw () {
translate(width/2, height/2);
rotate (-HALF_PI);
}
for (int i=0; i < data.length; i++) {
// float amtValue = map(i, 0, exec.length(), 0, 1); // I have no idea what this does; someone told me to do it
switch (data[i]) { //instead of if/then format
case '1':
stroke(#F02947);//red
rotate(-HALF_PI);
tLine(lineSeg);
break;
case '2':
stroke(#F029D6);//fucia
rotate(-HALF_PI);
tLine(2*lineSeg);
break;
case '3':
stroke(#9229F0);//purple
rotate(-HALF_PI);
tLine(3*lineSeg);
break;
case '4':
stroke(#293EF0);//blue
rotate(-HALF_PI);
tLine(4*lineSeg);
break;
case '5':
stroke(#29E1F0);//turquoise
rotate(-HALF_PI);
tLine(5*lineSeg);
break;
case '6':
stroke(#29F077);//lime green
rotate(-HALF_PI);
tLine(6*lineSeg);
break;
case '7':
stroke(#549B05);//moss
rotate(-HALF_PI);
tLine(7*lineSeg);
break;
case '8':
stroke(#EFF029);//yellow
rotate(-HALF_PI);
tLine(8*lineSeg);
break;
case '9':
stroke(#F09029);//pumpkin
rotate(-HALF_PI);
tLine(9*lineSeg);
break;
case '0':
stroke(0);//black
fill(0);
ellipse(0, 0, lineSeg, lineSeg);
break;
} //switch
} //for
void tLine(int x) {
tLineDraw(x);
}
void tLineDraw(int x){
line(0, 0, x, 0);
translate(x, 0);
}
Right now my error is “expecting EOF, found ‘for’,” but I suspect there is more wrong with the code than just that.
Any help would be appreciated!