Help with classes and hexagon

Hi I have tried to make a hexagon by using vectors however when I try to put the shape into a class (so I later can make a grid where I can detect in which one my mouse is in) but the problem is I can’t display my shape. Down below is my code and if you can find the mistake… please tell me where it is. It would be of great help. THANK YOU :slight_smile:

form hexagon;
void setup(){
hexagon=new form();
size(800,800);
}
void draw(){
background(255);
line(width/2,0,width/2,height);
line(0,height/2,width,height/2);
hexagon.display();
}
class form{
PVector center;
PVector b;
float angle;
float a;
int npoints;
int r=15;
form(){
npoints=6;
angle=TWO_PI/npoints;
//PVector center=new PVector(width/2,height/2);
}
void display(){
PVector center=new PVector(width/2,height/2);
line(0,0,center.x,center.y);
beginShape();
for (a=0; a<TWO_PI; a+=angle){
b=center.add(center.x+ cos(a)*r,center.y+ sin(a)*r);
vertex(b.x,b.y);
}
endShape(CLOSE);
}
}

You add center twice in the PVector

1 Like

I think there are two mistakes in the display function
PVector center=new PVector(width/2,height/2);
You define the variable center again which is unnecessary, but does not affect the functionality of the code.

b=center.add(center.x+ cos(a)*r,center.y+ sin(a)*r);

The function alrready adds the x and y of center there is no need to do this yourself.