Grasshopper

algorithmic modeling for Rhino

Hello,

I'm still new to vb.net and can t manage to sort a list of points according to the distance to them. The following code gives me the first closest point (but later on i would like to be able to find more then one closest point). The array part was just an attempt in getting the list sorted... but without a result:

Private Sub RunScript(ByVal inPts As List(Of On3dPoint), ByVal sPts2 As On3dPoint, ByRef A As Object)

    Dim d1 As Double = Double.MaxValue
    Dim min1 As Int32 = -1
    Dim d As Double

    Dim arrS As New ArrayList()
    Dim arrS0 As New List(Of List(Of on3dPoint))
    Dim arrS1 As New List(Of Double)
    Dim sList As New List(Of On3dPoint)
    //the last 4 lines were to check different options

    For i As Int32 = 1 To inPts.Count() - 1

      d = sPts2.DistanceTo(inPts(i))

      If (d < d1) Then
        d1 = d
        min1 = i
      End If

    Next

    arrS0.Add(inPts)
    arrS1.Add(d)
    arrS.Add(arrS0)
    arrS.Add(arrS1)
    sList = arrS.Sort(inPts)
    //the last 5 lines were to check different options


    A = inPts(min1)

I would appreciate any kind of hint/help on this one,

Thanks in advance,
Tudor

Views: 1596

Replies to This Discussion

Hi Tudor,

from the top of my head:

Private Sub RunScript(ByVal inPts As List(Of On3dPoint), ByVal sPts2 As On3dPoint, ByRef A As Object)
If (inPts.Count = 0) Then Return

Dim points As On3dPoint() = inPts.ToArray()
Dim distances As Double()
ReDim distances(inPts.Count-1)

For i As Integer = 0 To points.Length - 1
distances(i) = inPts(i).DistanceTo(sPts2)
Next

Array.Sort(distances, points)

A = points 'Returns an array of points, sorted by distance.
End Sub

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Thanks a lot David!

Works like a charm.

Thanks again for the quick reply.
Tudor

Hi,

Do I have to alter anything to work this code in Version 0.8.0062? Because A returns null when using the above code.

Thanks in advance.

mt

Hi mt,

if you're using the new VB and C# components you'll need to ditch types like On3dPoint because they no longer exist in RhinoCommon.


--

David Rutten

david@mcneel.com

Poprad, Slovakia

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