# Storing previous points when using a for-loop?

**URL:** <https://discourse.processing.org/t/storing-previous-points-when-using-a-for-loop/8829>\
**Category:** Coding Questions\
**Created:** [March 1, 2019, 3:03am UTC](https://discourse.processing.org/t/storing-previous-points-when-using-a-for-loop/8829 "2019-03-01T03:03:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JsRambles](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@JsRambles](https://discourse.processing.org/u/JsRambles)\
**Post date:** [March 1, 2019, 3:03am UTC](https://discourse.processing.org/t/storing-previous-points-when-using-a-for-loop/8829/1 "2019-03-01T03:03:57Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 1, 2019, 5:02am UTC](https://discourse.processing.org/t/storing-previous-points-when-using-a-for-loop/8829/2 "2019-03-01T05:02:26Z")

</div>

move up the line

> line(old,new);

by 2 lines and it works.

---

<div class="post-metadata">

**Author:** ![JsRambles](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@JsRambles](https://discourse.processing.org/u/JsRambles)\
**Post date:** [March 1, 2019, 2:11pm UTC](https://discourse.processing.org/t/storing-previous-points-when-using-a-for-loop/8829/3 "2019-03-01T14:11:41Z")

</div>

Thank you so much! 😃
