# How to get next vertex from array

**URL:** <https://discourse.processing.org/t/how-to-get-next-vertex-from-array/18284>\
**Category:** Coding Questions\
**Created:** [March 1, 2020, 10:27pm UTC](https://discourse.processing.org/t/how-to-get-next-vertex-from-array/18284 "2020-03-01T22:27:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [March 1, 2020, 10:27pm UTC](https://discourse.processing.org/t/how-to-get-next-vertex-from-array/18284/1 "2020-03-01T22:27:19Z")

</div>

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.

```auto
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:

```auto
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

```

```auto
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);
   }
}

```

---

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [March 2, 2020, 2:09pm UTC](https://discourse.processing.org/t/how-to-get-next-vertex-from-array/18284/2 "2020-03-02T14:09:48Z")

</div>

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

```auto

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);
   }
}

```
