make line ends move with mouse instead of continually draw over the top of each other?
in
Programming Questions
•
1 year ago
Hi all,
I working with the following little sketch:
I want processing to alter the position of the ends of the lines when the mouse is moved. though at the moment when the mouse is moved processing just draws new lines over the tops of the old ones. I've tried a few things to fix this. For instance moving the vert and hor variables into the ends of the bracketed for statements.
I'm at a bit of a loss here. I know it's got to do with the fact that processing loops through the code until it is killed and also that the lines are being drawn within a for statement. I'm thinking that what I need to do is put this for loop in its own function and find a way to pass values from it for i and m and then draw the line within void draw(). I tried doing this but got error messages. Which made me think I may be way off the mark with that line of thought.
Can someone help me see the forest instead of the trees?
I working with the following little sketch:
int vert = 0;
int hor = 0;
void setup()
{
size(640,640);
smooth();
background(255,0,0);
}
void draw()
{
vert=mouseX/10;
hor=mouseY/10;
for(int i=10; i<=640;i+=20)
{
for(int m=10; m<=640; m+=20)
{
fill(255);
noStroke();
ellipse(i,m,5,5);
stroke(255);
line(320,320,i+hor,m+vert);
}
}
}
I want processing to alter the position of the ends of the lines when the mouse is moved. though at the moment when the mouse is moved processing just draws new lines over the tops of the old ones. I've tried a few things to fix this. For instance moving the vert and hor variables into the ends of the bracketed for statements.
I'm at a bit of a loss here. I know it's got to do with the fact that processing loops through the code until it is killed and also that the lines are being drawn within a for statement. I'm thinking that what I need to do is put this for loop in its own function and find a way to pass values from it for i and m and then draw the line within void draw(). I tried doing this but got error messages. Which made me think I may be way off the mark with that line of thought.
Can someone help me see the forest instead of the trees?
1