Storing previous points when using a for-loop?

Hello,

I am trying to make a 9 sided polygon using points and than connecting the points with lines.

Reading other forums I know there is a way to do this using the vertex() commands and using arrays however this is for an assignment and unfortunately we are supposed to use for-loops.

I’ve figured out how to make the points using a for-loop however I am having trouble figuring out how to connect them with lines. Particularly how to store a previous point within its own separate variable.

This is what I have so far:

int points = 9;
int radius = 100;
int cntX, cntY;
float pointX, pointY, pPointX, pPointY;

void setup() {
  size(500,500);
  cntX = width/2;
  cntY = height/2;
  pointX = cntX+radius;
  pointY = cntY;
  background(0);
}

void draw() {
  background(0);
  stroke(255);
  for(int i=0; i<=points; i++) {
  float angle = TWO_PI*i/points;
  pointX = cntX+radius*cos(angle);
  pointY = cntY+radius*sin(angle);
  point(pointX, pointY);
  pPointX = pointX;
  pPointY = pointY;
  line(pPointX, pPointY, pointX, pointY);
  }
}

I’m thinking I might need to make another for-loop nested within the first for-loop somehow?

1 Like

move up the line

line(old,new);

by 2 lines and it works.

2 Likes

Thank you so much! :smiley: