# Mesh connectivity details

**URL:** https://community.freefem.org/t/mesh-connectivity-details/1285
**Category:** General Discussion
**Created:** [October 26, 2021, 10:59am UTC](https://community.freefem.org/t/mesh-connectivity-details/1285 "2021-10-26T10:59:35Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![distractor](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@distractor](https://community.freefem.org/u/distractor)
#### Post date: [October 26, 2021, 10:59am UTC](https://community.freefem.org/t/mesh-connectivity-details/1285/1 "2021-10-26T10:59:35Z")

</div>

I want to be able to:

- Give point coordinates and as a result get the finite element (or at least its index).
- Give mesh vertice and as result get list of element indexes, containing this vertice
- Give the final element index as return the list of vertices.

So ultimately I am looking for something like `whoInElement` function but not only for the boundary elements as done in the [example](https://doc.freefem.org/documentation/mesh-generation.html):

```auto
120 int NbBoundaryElements = Th.nbe;
121 for (int k = 0; k < NbBoundaryElements; k++)
122 cout << k << " : " << Th.be(k)[0] << " " << Th.be(k)[1]
123 << " , label " << Th.be(k).label
124 << ", triangle " << int(Th.be(k).Element)
125 << " " << Th.be(k).whoinElement << endl;

```

# Example

Assume a simple square mesh. For each computational point, I must be able to determine the corresponding finite element and vice-versa, for each element I must be able to know which points are within or at least the mesh vertices.

---

<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: [October 26, 2021, 2:32pm UTC](https://community.freefem.org/t/mesh-connectivity-details/1285/2 "2021-10-26T14:32:41Z")

</div>

You want for each vertices of the mesh the list of triangle contening this vertex:

// you can do that in few line of code:

mesh Th=square(4,4);

int[int] headv(Th.nv), next(Th.nt\*3);

headv=-1; //  
for(int k =0;k\< Th.nt; ++k)  
for(int i =0;i\< 3; ++i)  
{ int v = Th[k][i]; // vertex number  
next[3_k+i] = headv[v];  
headv[v]= 3_k+i;   
}  
// show the list of triangle of vertex v  
for(int v=0; v\<Th.nv;++v)  
{ cout \<\< v \<\< " : ";  
for(int p=headv[v]; p \>=0; p=next[p])  
{  
int k = p/3, i = p%3;  
assert( Th[k][i]==v); // check …  
cout \<\< k \<\< " " ;   
}  
d a acout \<\< endl;  
}

you can have of explanation in file:

> **[NotesdeCoursMN406.pdf](https://www.ljll.math.upmc.fr/hecht/ftp/NM406/NotesdeCoursMN406.pdf)**
>
> 1704.88 KB

section 9.7 page 111 (sorry in french).
