#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Is there a way to draw a polygon of n points with a forloop by drawing lines and not using vertex()

edited March 2018

I need to create a polygon on 9 points, the only way ive figured out how to do this is by using the beginShape , vertex(x,y) and endShape. I havent been taught these functions in class so i dont want to use them to create this polygon. is there a way to do this with just a forloop and lines?

code i have so far

int points = 9;

float centerX,centerY;

void setup(){

size(500,500);

centerX = width/2; centerY = height/2;

} void draw(){

background(200);

float angle = TWO_PI / points;

for( int i = 0 ; i < points ; i ++ ){

float posX = centerX + cos(i*angle) * radius;

float posY = centerY + sin(i*angle) * radius;

}

}

Tagged:

``````int points = 9;
float centerX, centerY;

void setup() {

size(500, 500);

centerX = width/2;
centerY = height/2;
}

void draw() {

background(200);

float angle = TWO_PI / points;

for ( int i = 0; i < points; i ++ ) {

float posX = centerX + cos(i*angle) * radius;
float posY = centerY + sin(i*angle) * radius;

float posX2 = centerX + cos((i+1)*angle) * radius;
float posY2 = centerY + sin((i+1)*angle) * radius;

stroke(255);
line(posX, posY,
posX2, posY2);

fill(255, 2, 2);
noStroke();
ellipse(posX, posY, 2, 2);
}
}
``````