Grasshopper

algorithmic modeling for Rhino

I'm trying to randomly swap items in a tree. The idea is to join points with lines but randomly switch some points across paths so the lines cross over. I couldn't figure out how to do this with just the GH components so dabbled in VB... Can someone give me some help here please?

>>

  Private Sub RunScript(ByVal PointsToSwap As DataTree(Of Object), ByVal PointsInEachPath As Object, ByVal Paths As Object, ByVal ProbSwap As Object, ByRef SwappedPoints As Object)
    Dim i As Integer
    Dim j As Integer
    Dim RandomClass As New Random()
    Dim RandomNumber As Double
    Dim PointToSwap As Object
    Dim ThePointsToSwap As New DataTree(Of Object)

    ThePointsToSwap = PointsToSwap

    For i = 0 To Paths
      'set path
      Dim apath As New GH_Path(i)

      For j = 0 To PointsInEachPath - 1
        'swap items
        'RandomNumber = RandomClass.NextDouble()
        randomnumber = 0.5
        If RandomNumber > ProbSwap Then
          PointToSwap = ThePointsToSwap(apath, j)
          ThePointsToSwap(apath, j) = ThePointsToSwap(apath, j + 1)
          ThePointsToSwap(apath, j + 1) = PointToSwap
        End If

      Next

    Next

    SwappedPoints = ThePointsToSwap

wrist_strap.gh

Views: 880

Replies to This Discussion

Hi Martyn,

Only the way that you access the path seems to go wrong. I usually use a different way to point to the path in a datatree, maybe you want to try it that way: YourDataTree.branch(i)(j)

This way you can directly make use of your loop variable. I also got rid of some inputs for the tree sizes as you can just get that information from the datatree you bring into the component. Lastly I thought that it would be nice to add an input for the random seed (as it is always nice to have more control over the random appearance). In bold the changes (the result looks pretty nice by the way, make sure to post the physical result when it's ready!):

 

    Dim i As Integer

    Dim j As Integer

    Dim RandomClass As New Random(rndSeed)

    Dim RandomNumber As Double

    Dim ThePointsToSwap As New DataTree(Of Point3d)

    ThePointsToSwap = PointsToSwap

 

    For i = 0 To PointsToSwap.BranchCount - 1

       For j = 0 To PointsToSwap.Branch(i).Count - 2

        'swap items

        RandomNumber = RandomClass.NextDouble()

         If RandomNumber > ProbSwap Then

          ThePointsToSwap.branch(i)(j) = PointsToSwap.branch(i)(j + 1)

          ThePointsToSwap.branch(i)(j + 1) = PointsToSwap.branch(i)(j)

        End If

      Next

    Next

 

    SwappedPoints = ThePointsToSwap

Attachments:

Thanks Marc, that's great, I knew there would be a better way to reference items in a tree!

I will post pictures of anything I manage to make!

A = B

B = A

will not swap two values, it will set both to B. To swap two values, you'll need a third to cache the result:

cache = A

A = B

B = cache

--

David Rutten

david@mcneel.com

Thanks David,

What I thought the code was doing was making a copy of the input tree and then setting

CopyA = OriginalB

CopyB = OriginalA

I ended up doing the cache thing anyway!

It turned out that that VB script was doing something strange... when it swapped the first point, that point seemed to disappear from the original list. I had to swap a point into another variable before the first swap and then use the variable to swap back into the list.

I got what I wanted in the end though... Just need to work out how to get a set of radii for the variable pipe component next!

(and the world class solution would actually weave the curves, but I think that's beyond me!

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