Trying to create a polygon point connector

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:


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

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.

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();
}
2 Likes