Grasshopper

algorithmic modeling for Rhino

Hi all,
I'm a beginner in rhino script and grasshopper. 
so this question maybe very stupid either.

The purpose of doing this is to create a parametric stadium seating sightline.
I attached simple diagram of creating sightline.  
In the diagram formula is N=[((R+C)x(D+T))/D] - R where R is the vertical distance of eye above point of focus and D is the horizontal distance from eye to point of focus.

So I have very simple test script.

Call main()
Sub main()
  Dim D,R,N
  Dim T,C
  T=1
  C=1
  For D=0 To 5 Step 0
  D=D+T
  For R=0 To 5 Step 0
  N=(((R+C)*(D+T))/D)-R
  R=R+N
  Call Rhino.addpoint(Array(D,R,N))
  Next
  Next
    
   End Sub

Basically I want to make all variable "D","R","T" and "C" as parametric number slider in GH
and repeat "D=D+T", "R=R+N" and "N=(((R+C)*(D+T))/D)-R" until certain times.

The question is how to make a incremental loop in GH.
If anyone think that there is a better solution to do this please teach me. 

Thanks for your time!!

Views: 806

Attachments:

Replies to This Discussion

Incremental loop... ouch.

If you decide to use a VB component for this, then do the following:

1) Create all the input parameters, and set all type filters to type Double
2) inside the component, use the following code:

Sub RunScript(ByVal D As Double, ByVal R As Double, ByVal C as Double, ByVal T As Double)
'pts will hold all the points we compute.
Dim pts As New List(Of On3dPoint)

'I'm assuming D is no longer hardcoded since it will be an input?
For D = 0.0 To 5.0 Step 0 '(!!! Step 0???, are you crazy!?!? that will crash!)
D += T
For R = 0.0 To 5.0 Step 0 '(again... step 0?)
N = (((R + C) * (D + T)) / D) - R
R += N

pts.Add(New On3dPoint(D, R, N))
Next
Next

'Assign the result to the A output
A = pts
End Sub


I've just written that without testing, there may be a bug I missed.


--
David Rutten
david@mcneel.com
Poprad, Slovakia
Hi david,


Thanks for your help and forgive my brave ignorance~ haha
I start to see points and lines on screen finally.
I have more questions now though.


First of all, did I apply it correctly?

second, why the x value are repeated?

I really appreciate your help.
Thanks.
Attachments:
Pretty much. I don't know if the algorithm is correct, but there doesn't appear to be an obvious error.

You have two loops inside each other, that's why you're getting columns of points. If you only want points at the top, you should put the pts.Add() after the first Next.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Hi David,

I'm fixing the algorithm but there's a question.
Can you explain me why the "N" value starts with 0.0?
I can find any answer of that.

I put step "0" because when it was "1" it adds 1 to its value.
For example I want "D" it increased by "T" but "D" goes "T+1" with step 1.
Tell me if I'm misunderstanding something.

I really appreciate your advices.
Thanks.

Best
Jooyoung chung
Attachments:
A For..Next loop works by incrementing the iteration variable until it exceeds the limit. Thus:

For i As Int32 = 0 To 9

Will run 10 times. The first time, i will be zero. When the first iteration completes, i will be incremented. The default increment amount is 1.

If you change the syntax to be:

For i As Int32 = 0 To 9 Step 2

then i will be incremented by 2 each time. So now the loop will run 5 times:

first iteration i = 0
second iteration i = 2
third iteration i = 4
fourth iteration i = 6
fifth iteration i = 8
sixth iteration i = 10 which is more than 9 so the 6th iteration never happens.

If you use Step 0, i isn't incremented at all, and the loop should run forever, unless you have some other abort statement or if you adjust the iteration variables inside the loop. You are doing the latter. Your iteration variable is D. The loop itself does not increment it, but you manually increment it when you call D += T.

Although this is logically solid, it is very bad practice since it makes the code very hard to read.

Since D, R and N are not external variables, I would not expose them as Input parameters.

Also, you inner loop is very weird:

For R = R1 To R Step 0

Your iteration variable here is R, but your termination limit is also R. Not to mention the step is again zero.


Trying to debug this is very difficult because it's been written in a very unorthodox fashion. I have a distinct feeling this algorithm can be written down with far fewer variables and constructs.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Hi David,
now I have a successful result.
Basically I did not need two loops.
This simple example teach me many things.

Thanks for your help.
Hi, JC,
Developing the system is for generating section of stadium bowl and has been stopped for a while but you may take a look.
Let me know if you have any question.
Attachments:

Thanks a lot David!

When I placed 0.5 as Step my GH crashed...

Is it not possible to have decimal increments?

Greetings,

Arthur

Not when your iteration variable is an integer. 

 

It is generally a bad idea to use decimal increments in loops, as it might lead to significant binary noise in the logic.

 

Let's for the sake of argument assume that the number 0.5 cannot be exactly represented using a 64-bit floating point number. So what's actually stored in the memory is something like 0.4999999997 or 0.500000000002

 

Now, when you run a loop and you add this increment to a starting value, say 100 times, then you amplify that discrepancy with about a hundred. Eventually, when you were supposed to hit the 50.0 mark, you're only at 49.99999997, which is less than 50.0, which means you'll get an extra iteration.

 

When you use floating point iteration counters, you'll also need to be smart and adjust your loop limit accordingly. I find it much easier to use integers in loops (integer math is always exact) and then compute a floating point value inside the loop from the integer. This avoids both cumulative floating point rounding and the aforementioned extra iteration problem.

 

And I don't think Grasshopper crashed. It just got stuck in an infinite loop, as 0.5 has its decimal portion stripped, which means you were adding 0 to i every iteration which meant you weren't getting anywhere.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service