Keep in mind that, if you are safely converting each index with %, you can access more than the next point – you can access three points, or every tenth point, and at any offset.
Here is an example that draws a series of triangles based on a ring of points, constantly changing its starting points and offsets. As frameCount and step both increase, code is eventually using i values like:
1270 1292 1314
but instead of getting index errors, with modulo they access safe indices in the ArrayList of size 24 – points:
22, 20, 18
/**
* Accessing points like a circular array
* 2020-04-14 Processing 3.4
*/
ArrayList<PVector> points;
int num = 24;
int step = 1;
void setup() {
size(600,600);
points = new ArrayList<PVector>();
// circle of points
for (int i=0; i<num; i++) {
float x = width * 4/9.0 * sin(TWO_PI*i/(float)num);
float y = height * 4/9.0 * cos(TWO_PI*i/(float)num);
points.add(new PVector(x, y));
}
}
void draw(){
background(128);
translate(width/2,height/2);
PVector p1, p2, p3;
// count from anywhere
for (int i=frameCount; i<frameCount+points.size(); i++) {
// access points
p1 = points.get(i%points.size());
p2 = points.get((i+step)%points.size());
p3 = points.get((i+2*step)%points.size());
triangle(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
}
println(frameCount, frameCount+step, frameCount+2*step);
if(frameCount%60==0) step++;
}
