# Trying to create a polygon point connector

**URL:** <https://discourse.processing.org/t/trying-to-create-a-polygon-point-connector/15362>\
**Category:** Coding Questions\
**Created:** [November 9, 2019, 10:32pm UTC](https://discourse.processing.org/t/trying-to-create-a-polygon-point-connector/15362 "2019-11-09T22:32:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![musiciantriescoding](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/musiciantriescoding/32/7662_2.png) [@musiciantriescoding](https://discourse.processing.org/u/musiciantriescoding)\
**Post date:** [November 9, 2019, 10:32pm UTC](https://discourse.processing.org/t/trying-to-create-a-polygon-point-connector/15362/1 "2019-11-09T22:32:37Z")

</div>

Hello! I am trying to create a **polygon point connector** that works likes this:  
1. draw _n_ amount of points on a circle equidistant from each other  
2. connect the points to the _nth_ point next to it to create polygons and stars

To me, this sounded simple enough to use as an exercise. I did get the code to somewhat work, however, a few strange glitches are occurring.

Firstly, when the number of points is set to a number that is not divisible by 360, the loop shoots over the counter.

Secondly, although I set the first line to start on the first point drawn, there seems to always be a line being drawn before the polygon/stars are drawn.

If anyone can point out my blunders, I would be very grateful! Thanks! Here is the code:

```auto

int numsides=14; //these are the number of points to be drawn
float rotation=360/numsides;
int x,y; //center of polygon
int r=430; //radius of polygon
float[] edgesx; //storing points in these arrays
float[] edgesy;
float pointx,pointy;
int displacement=5; //this is the nth point next to point
int posx1,posy1,posx2,posy2; //these are the indices of the arrays
void setup(){
  size(900,900);
  x= width/2;
  y= height/2;
  edgesx = new float[numsides];
  edgesy = new float[numsides];
   posx1=0;
   posy1=0;
   posx2=posx1+displacement;
   posx2=posy1+displacement;
}
void draw(){
   int counter = 0;
//here you see that I have to put - rotation if the number of points is 
//not a divisor of 360. 
    for(float i=0;i<360-rotation;i+=rotation){ 
       pointx=x+r*cos(radians(i));
        pointy=y+r*sin(radians(i));
        point(pointx,pointy);
        edgesx[counter]=pointx;
        edgesy[counter]=pointy;
        counter++       
    }
// I expected these lines of code to first draw a line between the current point
//and the subsequent point taken from the array.
    line(edgesx[posx1],edgesy[posy1],edgesx[posx2],edgesy[posy2]); 
   posx1=(posx1+displacement)%(edgesx.length);  
    posy1=(posy1+displacement)%(edgesx.length);    
    posx2=(posx1+displacement)%(edgesx.length);  
    posy2=(posy1+displacement)%(edgesx.length); 
    }

```

---

<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:** [November 10, 2019, 2:21am UTC](https://discourse.processing.org/t/trying-to-create-a-polygon-point-connector/15362/2 "2019-11-10T02:21:43Z")

</div>

i think main problem is the rotation  
you use integer calc instead float

also some things can be made easier and ( i hope ) more clear.

```auto
int numsides=14; // _______________________ these are the number of points to be drawn
float rotation=TAU/(float)numsides; // ____ needs float math
int r=430; // _____________________________ radius of polygon
float[] edgesx = new float[numsides]; //__ storing points in these arrays
float[] edgesy = new float[numsides];
int displacement=5; // ____________________ this is the nth point next to point

void make_star() {
  for (int i=0; i < numsides; i++) {
    float ang = i * rotation;
    edgesx[i]=r*cos(ang);
    edgesy[i]=r*sin(ang);
  }  
}

void draw_star() {
  for (int i=0; i < numsides; i++) {
    int j = i + displacement;
    if ( j >= numsides ) j -= numsides;
    circle( edgesx[i], edgesy[i], 5);
    line( edgesx[i], edgesy[i], edgesx[j], edgesy[j]);
  }
}

void setup() {
  size(900, 900);
  make_star();
}

void draw() {
  translate(width/2, height/2);
  draw_star();
}

```
