Grasshopper

algorithmic modeling for Rhino

closest point sorting, seeking points after the first index

I know how to get the closest point in an array of points using the convenience method but if I wanted the second or third closest is there a way using this underlying function? or do I have to get into a whole new sorting routine?

Views: 1595

Replies to This Discussion

Try this:
Sub RunScript(ByVal ptList As List(Of On3dPoint))

Dim tree As New DataTree(Of On3dPoint)
Dim pathNum As Int32 = 0
Dim j As Int32 = ptList.Count() - 1

nearTwo(ptList, tree, pathNum, j)

pOut = tree

End Sub

#Region "Additional methods and Type declarations"
Sub nearTwo (ByVal ptsIn As List(Of On3dPoint), ByRef tree As DataTree(Of On3dPoint), ByRef pathNum As Int32, ByRef j As Int32)
While (j > 3)
Dim path As New EH_Path(pathNum)

Dim d1 As Double = Double.MaxValue
Dim d2 As Double = Double.MaxValue
Dim min1 As Int32 = -1
Dim min2 As Int32 = -1

For i As Int32 = 1 To j - 1
Dim d As Double = ptsIn(0).DistanceTo(ptsIn(i))

If (d < d1) Then
d1 = d
min1 = i
Else If (d < d2) Then
d2 = d
min2 = i
End If
Next

tree.Add(ptsIn(0), path)
tree.Add(ptsIn(min1), path)
tree.Add(ptsIn(min2), path)
ptsIn(0) = Nothing
ptsIn(min1) = Nothing
ptsIn(min2) = Nothing

Dim tList As New List(Of On3dPoint)
For i As Int32 = 0 To j - 1
If (ptsIn(i) <> Nothing) Then
tList.Add(ptsIn(i))
End If
Next

j = tList.Count()
pathNum += 1

'calls recursive routine
nearTwo(tList, tree, pathNum, j)

End While
End Sub
or this:

I tried this definition, but it gives me the one list of 2nd or 3rd closest if i replace 1 with 2 or 3, if i want the 3 most closest point, should i be using the range component??

Just came across this closest point sorting solution. Brilliant Vicente! Thanks.

thanks for that!
Hi Dirk
can U explain your script because the result I'm having is really strange.and I dont really understand what is a problem//

Thnx
Alex

Here's a python version that sorts the input points based on their distance from a single point and the returns the first 'n' of these. The entire script is

def sortfunc(a,b):
....return cmp( a.DistanceTo(the_point), b.DistanceTo(the_point) )
a = sorted(points, sortfunc)
a = a[0:int(closest_count)]

Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service