# Distance map from boundary

**URL:** <https://community.freefem.org/t/distance-map-from-boundary/2342>\
**Category:** General Discussion\
**Created:** [March 17, 2023, 3:59pm UTC](https://community.freefem.org/t/distance-map-from-boundary/2342 "2023-03-17T15:59:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![akiss](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@akiss](https://community.freefem.org/u/akiss)\
**Post date:** [March 17, 2023, 3:59pm UTC](https://community.freefem.org/t/distance-map-from-boundary/2342/1 "2023-03-17T15:59:07Z")

</div>

Hello!  
Given a mesh defined in FreeFem (in 2D for the moment), I would be interested by a distance map to a part of its boundary as a finite element field on it.

For instance, if I have a square mesh with label 1 for the bottom, label 2 for the right side, label 3 for the top and label 4 for the left side, I would like to have a distance map to the boundary part defined by labels 1 and 2.

What would be the way to have that?

Thanks a lot in advance!

---

<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:** [March 18, 2023, 4:15pm UTC](https://community.freefem.org/t/distance-map-from-boundary/2342/2 "2023-03-18T16:15:33Z")

</div>

You can use the plugin distante,

see :

> <https://github.com/FreeFem/FreeFem-sources/blob/master/examples/plugin/distance.edp>

> <https://github.com/FreeFem/FreeFem-sources/blob/master/examples/plugin/distance2.edp>

to build the iso value of the border

```auto
load "distance"
int[int] lb=labels(Th);:
varf vb(u,v) = on(lb,u=1):
Vh ub= vb(0,Vh,tgv=1);// 1 on all arder and zero inside
ub[] = ub[] ? 0 : 1; // 0 on border and 1 inside
Vh dist;
distance(Th,ub,dist);
plot(ub,wait=1); 

```

---

<div class="post-metadata">

**Author:** ![Jacoban90](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@Jacoban90](https://community.freefem.org/u/Jacoban90)\
**Post date:** [January 25, 2024, 9:37am UTC](https://community.freefem.org/t/distance-map-from-boundary/2342/3 "2024-01-25T09:37:55Z")

</div>

Hello Fréderic,

Regarding the last example that you have mentioned:

```auto
load "distance"
int[int] lb=labels(Th);:
varf vb(u,v) = on(lb,u=1):
Vh ub= vb(0,Vh,tgv=1);// 1 on all arder and zero inside
ub[] = ub[] ? 0 : 1; // 0 on border and 1 inside
Vh dist;
distance(Th,ub,dist);
plot(ub,wait=1); 

```

By applying it initially to a square domain in the following form:

load “iovtk”

load “distance”

//mesh Th=readmesh(“Square2d.mesh”);

mesh Th=square(10,10);

int[int] lb=labels(Th);

fespace Vh(Th,P1);

Vh u,v;

varf vb(u,v) = on(lb,u=0);

//+on(2,3,4, u=-1);

Vh ub = vb(0,Vh, tgv=1);

plot(ub, wait=1, value=1);

ub = ub ? 0:1;

plot(ub,wait=1,value=1);

Vh dist;

distance(Th,ub,dist,distmax=100);

plot(dist,wait=1,value=1,nbiso=100);

savevtk(“distance.vtk”,Th,dist,dataname=“distance”);

When i plot the distance filed i get:

[image]

The representation I get is above. Could you give me a hint of what’s faling or missing in the code in order to obtain the desired distribution? The idea is to reproduce this method in a ficticious domain which embeds a smaller square domain and compute a lavel set phi function:

[image]

Thank you in avance.

Best Regards

Jacob

---

<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:** [February 6, 2024, 4:27pm UTC](https://community.freefem.org/t/distance-map-from-boundary/2342/4 "2024-02-06T16:27:39Z")

</div>

My correction

```auto
load "distance"
mesh Th=square(10,10);
fespace Vh(Th,P1);
int[int] lb=labels(Th);
varf vb(u,v) = on(lb,u=1);
Vh ub= vb(0,Vh,tgv=1);// 1 on all arder and zero inside
ub[] = ub[] ? 0 : 1; // 0 on border and 1 inside
Vh dist;
distance(Th,ub,dist[]);
plot(ub,wait=1);
plot(dist,wait=1);

```

the correction :  
[untitled.edp](https://community.freefem.org/uploads/short-url/3mmz40egT8oMnMvh93RrI2mZQId.edp) (435 Bytes)
