honeycomb pattern
in
Programming Questions
•
1 year ago
Hey I´ve written some code that should create a honeycomb pattern. It works, except the upper right corner where the pattern gets shifte a little bit. Can anybody tell me why it doesn´t work properly?
This is the code:
int sidelength = 40;
void draw_comb (int x,int y,int l)
{
beginShape();
vertex(x-0.5*l,y+cos(radians(30))*l);
vertex(x+0.5*l,y+cos(radians(30))*l);
vertex(x+l,y);
vertex(x+0.5*l,y-cos(radians(30))*l);
vertex(x-0.5*l,y-cos(radians(30))*l);
vertex(x-l,y);
vertex(x-0.5*l,y+cos(radians(30))*l);
endShape();
}
void honeycomb_background()
{
for(int y_start=height ; y_start >= -height; y_start -= 2*sidelength*sin(radians(60)))
{
for(int x=0,y=y_start;x<=width+sidelength; x+=3/2*sidelength+sidelength*sin(radians(30)), y+= sidelength/tan(radians(30))/2)
{
draw_comb(x,y,sidelength);
}
}
}
void setup()
{
size(800,800);
}
void draw()
{
noFill();
stroke(100);
honeycomb_background();
//draw_comb(200,200,20);
//ellipse(200,200,10,10);
}
void draw_comb (int x,int y,int l)
{
beginShape();
vertex(x-0.5*l,y+cos(radians(30))*l);
vertex(x+0.5*l,y+cos(radians(30))*l);
vertex(x+l,y);
vertex(x+0.5*l,y-cos(radians(30))*l);
vertex(x-0.5*l,y-cos(radians(30))*l);
vertex(x-l,y);
vertex(x-0.5*l,y+cos(radians(30))*l);
endShape();
}
void honeycomb_background()
{
for(int y_start=height ; y_start >= -height; y_start -= 2*sidelength*sin(radians(60)))
{
for(int x=0,y=y_start;x<=width+sidelength; x+=3/2*sidelength+sidelength*sin(radians(30)), y+= sidelength/tan(radians(30))/2)
{
draw_comb(x,y,sidelength);
}
}
}
void setup()
{
size(800,800);
}
void draw()
{
noFill();
stroke(100);
honeycomb_background();
//draw_comb(200,200,20);
//ellipse(200,200,10,10);
}
1