Distance - store three nearest Vertices to given Vertex .... Getting Crazy :D

Hoi Folks,
Im building a conform script and i struggle a lot how to get the three nearest vertices to a given vertice in one iteration. I coded a little thing to get it for two but Im hardly confused how to do it. Any help would be really nice . Greetings EliderDeli

local oldDistance = 1e9
local oldDistance2 = 1e9
local oldDistance3 = 1e9

indexNearestVertice = undefined

first = 0
second = 0
third = 0
myList = refObj.selectedVerts

for i=1 to myList.count by 1 do
(

distanceO = calculatdistance myList[i].pos SelectedVerticeID.pos

if (distanceO < oldDistance) then
(
second = first
first = myList[i]
oldDistance = distanceO
)
if (distanceO < oldDistance2 && distanceO != oldDistance) then second = myList[i]
)

Comment viewing options

.

collect pairs of distance and index and then sort by distance

```(
delete objects

obj = plane widthsegments:10 lengthsegments:10 isSelected:true
addModifier obj (Noisemodifier strength:[10,10,0] scale:11 fractal:on )
convertToMesh obj

pt = point pos:(obj.center + [0,0,3]) centermarker:on cross:off wirecolor:yellow

setVertSelection obj #{32..80}

subObjectLevel = 1

fn sortByDist a b = (

if a[1] < b[1] then -1 else if a[1] > b[1] then 1 else 0

)

pairs = for v in getVertSelection obj collect #( distance pt.pos (GetVert obj v), v )

qsort pairs sortByDist

closestIndexes = #{ pairs[1][2], pairs[2][2], pairs[3][2] }

for v in closestIndexes do point pos:(GetVert obj v + [0,0,1]) centermarker:on cross:off wirecolor:green

)```

Wow thank you very much this

Wow thank you very much this was extremly helpfull for me.