I have been trying to write a code that will allow me to press numbers keys and draw lines of that length, each connected to the one before. Ideally, the image would rotate 90 degrees between each line segment. In other words, when I press “4” it would draw a line of 4 pixels and turn 90 degrees, then when I press “8” it would draw a line of 8 pixels, starting from where the last line left off, and rotate 90 degrees, etc.
I am running into a couple issues:
(1) I can code the spiral and I can code line segments initiated by keystrokes, but when I try to put them together it all falls apart. The translate and rotate commands start behaving strangely inside a draw command and I haven’t managed to get pushMatrix to work. I read this:
https://processing.org/tutorials/interactivity/
(2) How can I make it so that it draws a line once when I press “2”, but not keep drawing that line for as long as I hold down the “2” key? I had success with redraw, but if I tried to add a translation, things went crazy again. Also, it only works the first time I press a given number, but not the second time. I read this:
https://forum.processing.org/one/topic/how-to-avoid-repetition-when-a-key-is-hold.html
Here is my code to test this:
int x=10; //length of line segment
void setup() {
size(500,500);
}
void draw() {
if(keyPressed) {
if (key == '1') {
line(0,0,1*x, 0);
}
if (key == '2') {
line(0,0,2*x, 0);
//x+=10;
//translate(x, 5);
}
}
}
void keyPressed(){
redraw();//run code in draw one time
}
Any help would be appreciated!