# Creating a hexagon mesh

**URL:** <https://community.freefem.org/t/creating-a-hexagon-mesh/2809>\
**Category:** General Discussion\
**Created:** [November 21, 2023, 2:45pm UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809 "2023-11-21T14:45:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AugustinCarteron](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/augustincarteron/32/1985_2.png) [@AugustinCarteron](https://community.freefem.org/u/AugustinCarteron)\
**Post date:** [November 21, 2023, 2:45pm UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809/1 "2023-11-21T14:45:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jbl](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jbl](https://community.freefem.org/u/jbl)\
**Post date:** [November 21, 2023, 2:57pm UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809/2 "2023-11-21T14:57:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AugustinCarteron](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/augustincarteron/32/1985_2.png) [@AugustinCarteron](https://community.freefem.org/u/AugustinCarteron)\
**Post date:** [November 21, 2023, 3:02pm UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809/3 "2023-11-21T15:02:10Z")

</div>

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

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![AugustinCarteron](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/augustincarteron/32/1985_2.png) [@AugustinCarteron](https://community.freefem.org/u/AugustinCarteron)\
**Post date:** [November 21, 2023, 3:02pm UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809/4 "2023-11-21T15:02:52Z")

</div>

This is the code with the coordinates :

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![jbl](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jbl](https://community.freefem.org/u/jbl)\
**Post date:** [November 21, 2023, 3:24pm UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809/5 "2023-11-21T15:24:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AugustinCarteron](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/augustincarteron/32/1985_2.png) [@AugustinCarteron](https://community.freefem.org/u/AugustinCarteron)\
**Post date:** [November 21, 2023, 3:29pm UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809/6 "2023-11-21T15:29:59Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![AugustinCarteron](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/augustincarteron/32/1985_2.png) [@AugustinCarteron](https://community.freefem.org/u/AugustinCarteron)\
**Post date:** [November 21, 2023, 3:32pm UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809/7 "2023-11-21T15:32:52Z")

</div>

```auto
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:

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![jbl](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jbl](https://community.freefem.org/u/jbl)\
**Post date:** [November 21, 2023, 3:33pm UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809/8 "2023-11-21T15:33:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jbl](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jbl](https://community.freefem.org/u/jbl)\
**Post date:** [November 22, 2023, 8:51am UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809/9 "2023-11-22T08:51:44Z")

</div>

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?

```auto
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); 

```

---

<div class="post-metadata">

**Author:** ![AugustinCarteron](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/augustincarteron/32/1985_2.png) [@AugustinCarteron](https://community.freefem.org/u/AugustinCarteron)\
**Post date:** [November 22, 2023, 9:25am UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809/10 "2023-11-22T09:25:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [December 18, 2023, 3:25pm UTC](https://community.freefem.org/t/creating-a-hexagon-mesh/2809/11 "2023-12-18T15:25:40Z")

</div>

For me the sample way to solve your problem

1. build a hexagon form a circle
2. split you hexagon mesh with trunk

```auto
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); 

```
