Creating a hexagon mesh

Hello community freefem, i need help creating a hexagon mesh with a certain number of nodes.
I have tried by setting the coordinates of my 6 points, but then i only have 6 nodes (one at each point) but i would like 50 nodes per segment of my hexagon.
Could someone help me solve this problem?

Hello, Can you provide us your code so that we can help you?

Oh yes of course that would be helpful. This was my first attempt but could not find a solution:

int nni= 64;        //number of nodes per side and along the inclusion boundary
real a=2;
border b1(t=(pi/6),(2*pi+pi/6)) {x=a*cos(t); y=a*sin(t); label=1;};
mesh Th=buildmesh(b1(6));
plot(Th,wait=true);

This is the code with the coordinates :

real a = 1; // cell side size
int b = 6;
func real circlex(real t){
return a*cos(t);
}
func real circley(real t){
return a*sin(t);
}
real[int] xx(6);
real[int] yy(6);
real k=0;
for (int i=0;i<b;i++){
xx(i)=circlex(k);
yy(i)=circley(k);
k=k+pi/3;
}
mesh Thxy=triangulate(xx,yy);
plot(Thxy);

Thanks,

First of all when you are running your first code does this plot something ? Because when i m running your first code nothing happen.

And for the second, with the exception of the first value of xx and yy, the others are “nan” objects.

That’s weird, because i do see the mesh (I am coding on VSC with v4.13 of FreeFem).
And for the second code, when i use :
cout << xx << endl;
cout << yy << endl;,
i do not get any NANs…

int nni= 64;        //number of nodes per side and along the inclusion boundary
real a=2;
border b1(t=(pi/6),(2*pi+pi/6)) {x=a*cos(t); y=a*sin(t); label=1;};
mesh Th=buildmesh(b1(6));
plot(Th,wait=true);

I just realised that for the first code, i had copied pasted directly into the conversion so the multiplication in front of the pi did not paste correctly, maybe this was the issue for you

IDEM for the second code:

real a = 1;   // cell side size
int b = 6;
func real circlex(real t){
	return a*cos(t);
}
func real circley(real t){
	return a*sin(t);
}
real[int] xx(6);
real[int] yy(6);
real k=0;
for (int i=0;i<b;i++){
    xx(i)=circlex(k);
    yy(i)=circley(k);
    k=k+pi/3;
}
cout << xx << endl;
cout << yy << endl;
mesh Thxy=triangulate(xx,yy);
plot(Thxy);

I did a mistake when i copy paste sorry for that.

Hello,

If you want 50 nodes per segment, build your segment one by one with border and buildmesh.

Does this example help you move forward?

int n=50;  

real yy1 = 0.866025; // yy(1) = circley(pi/3)


border c01(t=0,1) {x = -0.5 + t     ; y = -yy1                   ; label=1;};
border c02(t=0,1) {x =  0.5 + t/2   ; y = -yy1 + t * yy1        ; label=2;};
border c03(t=0,1) {x =  1 - t/2     ; y = -yy1 + yy1+t*yy1     ; label=3;};
border c04(t=0,1) {x =  0.5 - t     ; y = -yy1 +2*yy1           ; label=4;};
border c05(t=0,1) {x =  -0.5 - t/2  ; y = -yy1 +2*yy1 - t*yy1  ; label=5;};
border c06(t=0,1) {x = -1 + t/2     ; y = -yy1 + yy1 - t* yy1  ; label=6;};

mesh Th = buildmesh(c01(n)+c02(n)+c03(n)+c04(n)+c05(n)+c06(n)); 


plot(Th,wait = 1); 

Yes thank you very much! I did not realise that I could construct the mesh like this…
This does help me move forward and I hope it will help others as well!

1 Like

For me the sample way to solve your problem

  1. build a hexagon form a circle
  2. split you hexagon mesh with trunk
border C(t=0,2*pi){ x = cos(t); y= sin(t); lable=1;}
mesh Thhex= C(6); 
mesh Th= trunc(Thhex,1,split=64);