Hi my friends,
I am trying to follow Daniel’s Chaos Game and adding a P5 control to control values on runtime. But it seems I have to move some setups to the draw part, because codes in setup part only update once before draw. So I move codes to draw part but points will not keep accumulating due to some reasons. I wonder if the Processing have something like the unity ‘fixedupdate’ which will check about update in certain time.
Here is the code I try to achieve this effect; but when I update the slide the points will not accumulate. if I comment the background on draw part, the point just keeps duplicating and overlaying on the new shape, so how to do a reset on this only when it is a new shape.
import controlP5.*;
ControlP5 cp1;
ArrayList<PVector> points;
PVector current;
PVector previous;
final float precent = 0.5;
int n;
void setup()
{
size(800, 800);
//background(0);
n = 6;
cp1 = new ControlP5(this);
cp1.addSlider("n")
.setPosition(0, 0)
.setRange(0, 10);
points = new ArrayList<PVector>();
for(int i = 0; i < n; i++)
{
float angle = i * (TWO_PI / n);
//PVector v = new PVector(random(width), random(height));
PVector v = PVector.fromAngle(angle);
v.mult(width/2);
v.add(width/2, height/2);
points.add(v);
}
Setpts();
}
void Setpts()
{
//printArray(points);
current = new PVector(random(width), random(height));
background(0);
stroke(250);
strokeWeight(8);
for(PVector p : points)
{
point(p.x, p.y);
}
}
void draw()
{
//if(frameCount % 100 == 0)
//{
//reset();
//}
if(n != 6)
{
points.removeAll(points);
for(int i = 0; i < n; i++)
{
float angle = i * (TWO_PI / n);
//PVector v = new PVector(random(width), random(height));
PVector v = PVector.fromAngle(angle);
v.mult(width/2);
v.add(width/2, height/2);
points.add(v);
}
Setpts();
}
for(int i = 0; i < 5000; i++)
{
strokeWeight(1);
stroke(250, 160, 0, 200);
//stroke(250, 160, 0);
int index = floor(random(points.size()));
PVector next = points.get(index);
if(next != previous)
{
current.x = lerp(current.x, next.x, precent);
current.y = lerp(current.y, next.y, precent);
point(current.x, current.y);
}
previous = next;
}