# Understanding how KNearestSearch works

**URL:** <https://discourse.julialang.org/t/understanding-how-knearestsearch-works/111294>\
**Category:** New to Julia\
**Tags:** meshes\
**Created:** [March 7, 2024, 1:35pm UTC](https://discourse.julialang.org/t/understanding-how-knearestsearch-works/111294 "2024-03-07T13:35:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaoming](https://avatars.discourse-cdn.com/v4/letter/x/a6a055/32.png) [@xiaoming](https://discourse.julialang.org/u/xiaoming)\
**Post date:** [March 7, 2024, 1:35pm UTC](https://discourse.julialang.org/t/understanding-how-knearestsearch-works/111294/1 "2024-03-07T13:35:25Z")

</div>

I have a surface in three-dimensional space composed of triangles generated by Meshes.jl. Given a point in space, the function ‘search’ in Meshes.jl can find the closest triangle to this point. I want to know how this distance is measured, how the algorithm is implemented, and it would be even better if someone could provide some reference materials.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 7, 2024, 1:42pm UTC](https://discourse.julialang.org/t/understanding-how-knearestsearch-works/111294/2 "2024-03-07T13:42:43Z")

</div>

Everything is open-source, you can read the source code to learn more, it is simple Julia code:

> <https://github.com/JuliaGeometry/Meshes.jl/blob/master/src/neighborsearch/knearest.jl>

We use the NearestNeighbors.jl package and compute the distance to the centroid of the triangles. There are options to control the distance used, number of neighbors, etc.

---

<div class="post-metadata">

**Author:** ![xiaoming](https://avatars.discourse-cdn.com/v4/letter/x/a6a055/32.png) [@xiaoming](https://discourse.julialang.org/u/xiaoming)\
**Post date:** [March 7, 2024, 1:46pm UTC](https://discourse.julialang.org/t/understanding-how-knearestsearch-works/111294/3 "2024-03-07T13:46:12Z")

</div>

Thanks! I will check that.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 7, 2024, 2:09pm UTC](https://discourse.julialang.org/t/understanding-how-knearestsearch-works/111294/4 "2024-03-07T14:09:10Z")

</div>

Feel free to ask these user questions in our Zulip channel. We sometimes miss posts in Discourse.

---

<div class="post-metadata">

**Author:** ![xiaoming](https://avatars.discourse-cdn.com/v4/letter/x/a6a055/32.png) [@xiaoming](https://discourse.julialang.org/u/xiaoming)\
**Post date:** [March 7, 2024, 2:39pm UTC](https://discourse.julialang.org/t/understanding-how-knearestsearch-works/111294/5 "2024-03-07T14:39:22Z")

</div>

OK. I am glad to join the channel.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [March 7, 2024, 3:15pm UTC](https://discourse.julialang.org/t/understanding-how-knearestsearch-works/111294/6 "2024-03-07T15:15:55Z")

</div>

[![](https://global.discourse-cdn.com/julialang/original/3X/2/6/26af9705b4a0f764422a25e313446a76e5af19a2.jpeg "Kristoffer Carlsson - Fast Nearest-neighbor Queries in Julia Using NearestNeighbors.jl") ](https://www.youtube.com/watch?v=0v_42Gqaqlw)
