Forgot password?

Create an account!

Forum

« back

RhinoScript – Grid of circle with random size problem

Messages

Please log in to write a message.

  • 8. Hanno (Dec 08, 2010 18.06):

    Hi!

    You can use ExtrudeCurveStraight for that. CircleRadius gives you the radius of a circle.

    Regards

    Hanno

  • 7. DrCatellino (Dec 07, 2010 23.43):

    I managed to get this working ! The script had a few mistakes.

    The packing could be really more efficient tough. The best would be to get the circle moving around, trying to find the best spot, instead of getting deleted and passing to the other one.

    'packing circle
    Call Main()
    Sub Main()
      ' 2d Variables
      Dim i, y, j
      ' Other Variables
      Dim dblAx, dblBx, dblStepx, arrPlane, arrIdCircle(2000), arrRadius(2000), arrRando(2000), arrRandoX(2000), arrRandoY(2000)
      ' Constant definitions
      arrPlane = Rhino.WorldXYPlane()
      dblAi = 0
      dblBi = 2000
      dblStepi = 1.0
       
      Call Rhino.EnableRedraw(False)
     
      ' Beggining of the first For loop to create 400 circles
      For i = dblAi To dblBi Step dblStepi
        Call Rhino.print(i)
        ' Do Loop for scale Random
        Do
          arrRando(i) = Rnd()*5
          If arrRando(i) > 0.5 Then Exit Do
        Loop
       
        ' Create, Move and Scale the Circle
       
         arrIdCircle(i) = Rhino.AddCircle (arrPlane, 25.0)
         Call Rhino.ScaleObject (arrIdCircle(i), Array(0,0,0), Array (arrRando(i),arrRando(i),1))
         arrRandoX(i) = Rnd()*5000
         arrRandoY(i) = Rnd()*5000
         Call Rhino.MoveObject (arrIdCircle(i),Array(0,0,0),Array(arrRandoX(i),arrRandoY(i),0))
         arrRadius(i) = arrRando(i)*25.0
         ' Loop #2 to check for overlap
          For j = 0 To i-1
            Dim Distance, Rad2, Xdiff, Ydiff
            Xdiff = Abs(arrRandoX(i)-arrRandoX(j))
            Ydiff = Abs(arrRandoY(i)-arrRandoY(j))
            Distance = Sqr(Xdiff^2+Ydiff^2)
          Rad2 = (arrRadius(i)+arrRadius(j))
          If Rad2>Distance Then
          Rhino.deleteobject(arrIdCircle(i))
          arrRadius(i)=-500
          End If
          Next
          Next
      Call Rhino.EnableRedraw(True)
          End Sub

    Now I have another question: I want to do a 3D extrude of these circle with the same height as their radius. I don't know what command to use here.

    Grid of circle with random size problem
  • 6. DrCatellino (Dec 07, 2010 20.06):

    I forgot the Code bracket, here it is:

    'packing circle
    Call Main()
    Sub Main()

    ' 2d Variables
    Dim i, y, j

    ' Other Variables
    Dim dblAx, dblBx, dblStepx, arrPlane, Overlap, arrIdCircle(399), arrRadius(399), arrRando(399), arrRandoX(399), arrRandoY(399)

    ' Constant definitions
    arrPlane = Rhino.WorldXYPlane()
    dblAi = 0
    dblBi = 399.0
    dblStepi = 1.0
    Overlap = False

    Call Rhino.EnableRedraw(False)

    ' Beggining of the first For loop to create 400 circles

    For i = dblAi To dblBi Step dblStepi

    ' Do Loop for scale Random
    Do
    arrRando(i) = Rnd()
    If arrRando(i) > 0.5 Then Exit Do
    Loop

    ' Create, Move and Scale the Circle

    arrIdCircle(i) = Rhino.AddCircle (arrPlane, 25.0)
    Call Rhino.ScaleObject (arrIdCircle(i), Array(0,0,0), Array (arrRando(i),arrRando(i),1))
    arrRandoX(i) = Rnd()*1000
    arrRandoY(i) = Rnd()*1000
    Call Rhino.MoveObject (arrIdCircle(i),Array(0,0,0),Array(arrRandoX(i),arrRandoY(i),0))

    ' Loop #2 to check for overlap

    For j = 0 To i-1
    Dim Distance, Rad2
    Distance = (Sqr(Abs(arrRandoX(i)-arrRandoX(j))+(Abs(arrRandoY(i)-arrRandoY(j)))))
    Rad2 = (arrRadius(i)+arrRadius(j))
    If Rad2>Distance Then
    Rhino.deleteobject(arrIdCircle(i))
    exit for
    End If
    Next
    Next
    Call Rhino.EnableRedraw(True)
    End Sub
  • 5. DrCatellino (Dec 07, 2010 20.04):

    I've been trying to solve this myself, but being quite new to Rhinoscript, it's not really working.

    This time I am using Circles placed in random locations, and trying to see if the combined radius of two circles is bigger than their distance, and if it is the case, delete the circle.

    There must be something wrong because the script never delete any circle and Overlaping still occurs.

    Any help would be appreciated, thanks !

    'packing circle
    Call Main()
    Sub Main()

    ' 2d Variables
    Dim i, y, j

    ' Other Variables
    Dim dblAx, dblBx, dblStepx, arrPlane, Overlap, arrIdCircle(399), arrRadius(399), arrRando(399), arrRandoX(399), arrRandoY(399)

    ' Constant definitions
    arrPlane = Rhino.WorldXYPlane()
    dblAi = 0
    dblBi = 399.0
    dblStepi = 1.0
    Overlap = False

    Call Rhino.EnableRedraw(False)

    ' Beggining of the first For loop to create 400 circles

    For i = dblAi To dblBi Step dblStepi

    ' Do Loop for scale Random
    Do
    arrRando(i) = Rnd()
    If arrRando(i) > 0.5 Then Exit Do
    Loop

    ' Create, Move and Scale the Circle

    arrIdCircle(i) = Rhino.AddCircle (arrPlane, 25.0)
    Call Rhino.ScaleObject (arrIdCircle(i), Array(0,0,0), Array (arrRando(i),arrRando(i),1))
    arrRandoX(i) = Rnd()*1000
    arrRandoY(i) = Rnd()*1000
    Call Rhino.MoveObject (arrIdCircle(i),Array(0,0,0),Array(arrRandoX(i),arrRandoY(i),0))

    ' Loop #2 to check for overlap

    For j = 0 To i-1
    Dim Distance, Rad2
    Distance = (Sqr(Abs(arrRandoX(i)-arrRandoX(j))+(Abs(arrRandoY(i)-arrRandoY(j)))))
    Rad2 = (arrRadius(i)+arrRadius(j))
    If Rad2>Distance Then
    Rhino.deleteobject(arrIdCircle(i))
    exit for
    End If
    Next
    Next
    Call Rhino.EnableRedraw(True)
    End Sub

  • 4. DrCatellino (Dec 06, 2010 21.48):

    The script is going well, I am able to create the grid of circles I want.

    Now, I want to be able to delete a circle (or rescale it) if it intersects with another one.

    Is there any command to check if an object touches another one ? Else, do I have to create an ID for each object in order the verify if the combined radius is shorter that the distance between two circles ? How would I do that ?

    'packing circle
    Call Main()
    Sub Main()
      Dim x, y
      Dim dblA, dblB, dblStep, arrPlane
      arrPlane = Rhino.WorldXYPlane()
      dblA = 0
      dblB = 1000.0
      dblStep = 50.0

      Call Rhino.EnableRedraw(False)
      For x = dblA To dblB Step dblStep
        For y = dblA To dblB Step dblStep
          Do
          Dim rando
          rando = Rnd()*2
          If rando > 0.5 Then Exit Do
            Loop
        idCircle = Rhino.AddCircle (arrPlane, 25.0)
        Call Rhino.MoveObject (idCircle,Array(0,0,0),Array(x,y,0))
        Call Rhino.ScaleObject (idCircle, Array(x,y,0), Array (rando,rando,1))
        Next
      Next
      Call Rhino.EnableRedraw(True)
    End Sub

    Thanks !

    Grid of circle with random size problem
  • 3. DrCatellino (Dec 04, 2010 21.33):

    Thank you very much, Hanno !

  • 2. Hanno (Dec 03, 2010 17.01):

    Hi,

    in RhinoScript, objects are identified by object IDs. The AddCircle method returns the ID of the newly created object, you just have to save it to be able to identify the circle for the following translation:

    idCircle = rhino.AddCircle(...)
    call Rhino.MoveObject(idCircle, ...)

    Your loops are looking fine, so this should aready generate as many circles as you need.

    Then, the Rand function always returns a number between 0 and 1. You have to convert it to the range you need (e.g. Rnd() * 10 gives you a number between 0 and 10).


    Hope that helps!

    Hanno

  • 1. DrCatellino (Dec 02, 2010 17.27):

    Hello, first I have to say I am a total newbie with Rhinoscript and this is my first script ;)

    So my final goal is to create a Circle Packing script, but first I just want to create a grid of circle of random size within a square grid.

    My thought process is:

    1- Use the For function to grid "points" between 0 and 1000 with a Step of 50.
    2- Create a circle at the origin, and move the circle to the "point" created with the For function.
    3- Scale the circle using the Random function (with a range between 0.8 and 1.2)

    Here is the incomplete script so far:

    Call Main()
    Sub Main()
      Dim x, y
      Dim dblA, dblB, dblStep, randScale, arrPlane,
     
      arrPlane = Rhino.WorldXYPlane()
      dblA = 0
      dblB = 1000.0
      dblStep = 50.0
      randScale = Rnd()
     
      For x = dblA To dblB Step dblStep
        For y = dblA To dblB Step dblStep
        Call Rhino.AddCircle (arrPlane, 50.0)
        Call Rhino.MoveObject (??,Array(0,0,0),Array(0,0,1))
        Next
        Next
    End Sub

    I didn't add the Scaling part so far, because I can't get to make the AddCircle/MoveObject to work correctly.

    How could I use the AddCircle command to create as many circles as there is steps in the For function ?

    What do I have to enter in the MoveObject command to make it work the way I want ?

    How do I use the Rdn function to enter a specific range, eventually using it for scaling ?

    Thanks in advance.

Recommend

Why are these buttons gray?