How to get next vertex from array

I have an array of points, and in a loop through them I want to get a reference to the next point that is a vertex. It seems like I’m close here but what am I doing wrong?

nVertices is intended to represent the vertices of a polygon. nDivisions represents the number of points which come between vertex points.

So, for example, nVertices=6, nDivisions=1 would represent a hexagon with an extra point between each pair of vertices. nVertices=8, nDivisions=2 would represent an octagon with two points between each pair of vertices, etc.

This is the output from the code below.

vertex-> this point: 0 , next vertex: 0
         this point: 1 , next vertex: 0
vertex-> this point: 2 , next vertex: 2
         this point: 3 , next vertex: 2
vertex-> this point: 4 , next vertex: 4
         this point: 5 , next vertex: 4
vertex-> this point: 6 , next vertex: 6
         this point: 7 , next vertex: 6
vertex-> this point: 8 , next vertex: 8
         this point: 9 , next vertex: 8
vertex-> this point: 10 , next vertex: 10
         this point: 11 , next vertex: 10

And this is the expected output:

vertex-> this point: 0 , next vertex: 2
         this point: 1 , next vertex: 2
vertex-> this point: 2 , next vertex: 4
         this point: 3 , next vertex: 4
vertex-> this point: 4 , next vertex: 6
         this point: 5 , next vertex: 6
vertex-> this point: 6 , next vertex: 8
         this point: 7 , next vertex: 8
vertex-> this point: 8 , next vertex: 10
         this point: 9 , next vertex: 10
vertex-> this point: 10 , next vertex: 0
         this point: 11 , next vertex: 0
PVector[] points;
int nVertices = 6, nDivisions = 1, nPoints, pointsPerVertex;

void setup() {
   nPoints = nVertices + (nVertices * nDivisions);
   pointsPerVertex = nPoints / nVertices;
   points = new PVector[nPoints];
}

void draw() {
   background(127);
   noLoop();

   for (int pt = 0; pt < points.length; pt++) {
      
      boolean isVertex = pt % pointsPerVertex == 0;
      String vertexString = (isVertex) ? "vertex->" : "        ";
      
      int nextVertex = pt - (pt + pointsPerVertex) % pointsPerVertex;
      
      println(vertexString, "this point:", pt, ", next vertex:", nextVertex);
   }
}
2 Likes

Seems like this works. Guess I asked a bit too soon.


PVector[] points;
int nVertices = 6, nDivisions = 1, nPoints, pointsPerVertex;

void setup() {
   nPoints = nVertices + (nVertices * nDivisions);
   pointsPerVertex = nPoints / nVertices;
   points = new PVector[nPoints];
}

void draw() {
   background(127);
   noLoop();
   
   for (int pt = 0; pt < points.length; pt++) {
      
      boolean isVertex = pt % pointsPerVertex == 0;
      String vertexString = (isVertex) ? "vertex->" : "        ";
      
      // int nextVertex = pt;
      // nextVertex += pointsPerVertex;
      // nextVertex -= pt % pointsPerVertex;
      // nextVertex %= nPoints;

      int nextVertex = (pt + pointsPerVertex - (pt % pointsPerVertex)) % numPoints;
      
      println(vertexString, "this point:", pt, ", next vertex:", nextVertex);
   }
}
3 Likes