Hi,
I am currently struggling with a seldom behaviour. I want to plot the mouse position starting from the left side of the window. Ones it arrived on the right side, it should stay there:
ArrayList<Integer> y = new ArrayList<Integer>();
void setup() {
size(400, 300);
}
void draw() {
background(255);
if (y.size()-1<width)
{
y.add(mouseY);
}
else
{
y.set(y.size()-1,mouseY);
}
for (int i=y.size()-1;i>0; i--) {
line(i, y.get(i), i-1, y.get(i-1));
}
for (int i=0; i<y.size()-1; i++) {
y.set(i,y.get(i+1));
}
}
Somehow, while being in the if statement the plot seems somehow rugged, whereas it is smooth when being in the else statement:
You only had the “rugged graph” in the first sweep (to width) of your code.
I rearranged your code to remove these and so you can better understand where this was happening:
ArrayList<Integer> y = new ArrayList<Integer>();
void setup()
{
size(400, 300);
stroke(255, 255, 0);
strokeWeight(2);
}
void draw()
{
background(0);
if (y.size()-1<width)
{
y.add(mouseY);
for (int i=y.size()-1;i>0; i--)
line(i, y.get(i), i-1, y.get(i-1));
}
else
{
y.set(y.size()-1,mouseY);
for (int i=y.size()-1;i>0; i--)
line(i, y.get(i), i-1, y.get(i-1));
for (int i=0; i<y.size()-1; i++)
y.set(i,y.get(i+1));
}
// Moved this code up:
//for (int i=y.size()-1;i>0; i--)
// {
// line(i, y.get(i), i-1, y.get(i-1));
// }
//for (int i=0; i<y.size()-1; i++)
// {
// y.set(i,y.get(i+1));
// }
}
The strokeWeight(2) and contrast in colors played a big part in smoothing the appearance of output in this example.
As a personal exercise I wrote similar code from scratch using arrays (familiar) and then migrating to ArrayLists (new for me) and only used lines to join points.