# Extend lines by increment (iterative) stop when collision True [Python]

Hi,

I am trying to achieve a Gilbert Tessellation as an exercise. The idea is simple, lines from random points extend both sides in random directions, until they intersect.

So, the intention is to create a loop that extends each line by a factor, and constantly checks for intersections. The catch is that there is a loop for each line… when the intersection is true for one line, that line stops extending while the others continue.

I am stuck… how can this simple algorithm be achieved?

This is my code so far.

``````import rhinoscriptsyntax as rs
import ghpythonlib.components as gh

while bool == False:
bool = gh.CollisionManyXMany(curves)[0]
extended_curves = rs.ExtendCurveLength(curves, 0, 2, ext)
if bool == True:
break

a = extended_curves
``````

For some reason the error reads:

Runtime error (UnboundNameException): name ‘extended_curves’ is not defined
Traceback:
line 11, in script

Which is utterly useless, it tells me the variable “extended_curves” is not defined. It surely has to do with something regarding global and local variables, and the fact that the variable is being defined inside a loop… but how the f*** can I define extended_curves without extending them??

Oh I know: extended_curves = extended_curves. Pff easy.

I am about to destroy my pc. This cant be so hard. I already had a bad time dealing with GUIDs, GEOMETRY, and all that rhino crap.

GILBERT TRY.gh (10.1 KB)

Hi,

The catch here is that `bool != False`
‘bool’ is a builtin python type and as such is testable for False and it’s not falsy so the loop never runs leaving you with an undefined variable ‘extended_curves’

So to fix the code you could:

``````
import rhinoscriptsyntax as rs
import ghpythonlib.components as gh

collided = False
while collided == False:
collided = gh.CollisionManyXMany(curves)[0]
extended_curves = rs.ExtendCurveLength(curves, 0, 2, ext)
if collided == True:
break

a = extended_curves

``````

Does this make sense?
-Willem

Hi Willem, thank you.

I am afraid I tested your code and it crashed Rhino twice now, seems the loop never ends.

Sorry, forgot to change the remaining `bool` to `collided`
I edited the code above to the correct one

BTW:
Out of curiosity I’m trying to get the rest of the code to work, as it is throwing other errors now.

-Willem

Hi,

I think I got a working version for you.
First of all you need to set the input to list access:

next the code I made is this:

``````import rhinoscriptsyntax as rs
import ghpythonlib.components as gh
import Rhino

Nmax = 1000 #prevent endless loop by setting max loops
N = 0
collide = False
while collide == False:
N+=1
if N >= Nmax: break #max loops reached

#printing for debug purposes to find the type returned
#print type(gh.CollisionManyXMany(curves))
#print gh.CollisionManyXMany(curves)
#print gh.CollisionManyXMany(curves).collision

#gh.CollisionManyXMany returns a named tuple we need the property collision or index [0] like previously
#that is a list of booleans for witch we wat all to be True
collide = all( gh.CollisionManyXMany(curves).collision )
if collide == True:
break
extended_curves = []
for curve in curves :
extended_curves.append(curve.Extend(Rhino.Geometry.CurveEnd.Both,ext,Rhino.Geometry.CurveExtensionStyle.Line))
#print curve.GetLength()
curves = extended_curves

print N
a = curves

``````

Cleaning the code more I got this:

``````import rhinoscriptsyntax as rs
import ghpythonlib.components as gh
import Rhino

Nmax = 1000 #prevent endless loop by setting max loops
N = 0
while N < Nmax:
N+=1
if all( gh.CollisionManyXMany(curves).collision ) : break
curves = [curve.Extend(Rhino.Geometry.CurveEnd.Both,ext,Rhino.Geometry.CurveExtensionStyle.Line) for curve in curves]

print N
a = curves
``````

Using gh.ExtendCurve:

``````import rhinoscriptsyntax as rs
import ghpythonlib.components as gh

Nmax = 1000 #prevent endless loop by setting max loops
N = 0
while N < Nmax:
N+=1
if all( gh.CollisionManyXMany(curves).collision ) : break
curves = [gh.ExtendCurve(curve,0,ext,ext) for curve in curves]

print N
a = curves
``````

HTH
-Willem

2 Likes

Hi Willem, thank you for your time and the detailed post!

I will need to implement some tweaking to make it an actual Gilbert Tessellation, for example, if one end of the line stops extending due to an intersection, the other end can still keep on extending, hopefully I can take it from here.

Edit: I think you made it so that it only stops extending when all booleans == True.
That should no be the case, each curve should be independent.

Anyways I just realized there is a collision situation, where a curve intersects another one, instead of both stopping in the intersection point, one must stop and the other continue. Which one? I do not know, but this seems to be a lot more complex, oh well.