Hi,
I want to use this code to generate some circles along a sin curve and when I complete the code and click on ‘Test’ or ‘Ok’ in ghpython component, everything works just fine, but when I want to refresh the algorithm by pressing ‘F5’ button or change the number sliders, the first circle’s radius starts to change abnormally and I guess it’s having a conflict with globals but I don’t know how to fix it.

from math import sin , pi
import Rhino.Geometry as rg
import ghpythonlib.components as gh
a = []
for i in range(x):
j = t * sin(y*i*pi/x)
a.append(rg.Point3d( i , j , 0))
b = cylinders = []
c = rads =[]
for i in range(len(a)):
if 'radius' not in globals() or reset:
radius = 0.65
else :
radius = gh.Distance(a[i], a[i-1]) / 2
circle = rg.Circle(a[i] , radius)
cylinders.append(rg.Cylinder(circle , radius*h))
rads.append(radius)

how can I stop this from happening? I want every circle’s radius to calculate from the distance of origin point with that of the previous circle and exit the loop when it gets to the last point, but in my algorithm, the last radius calculation gets wrong and I guess it calculates the distance between the last and first point somehow!

import Rhino.Geometry as rg
from math import sin , pi
a = [rg.Point3d(i, t * sin(y * i * pi / x), 0) for i in xrange(x)]
circles = []
for i in xrange(x):
if i < x - 1:
radius = a[i].DistanceTo(a[i+1]) / 2
else:
radius = a[i].DistanceTo(a[i-1]) / 2
circle = rg.Circle(a[i] , radius)
circles.append(circle)
c = circles
b = [rg.Cylinder(cir, h) for cir in circles]

However, in a scenario like this, where you have predefined origin points, there is no guarantee that the generated circles intersect each other at a single point. Most will overlap or not intersect at all.

thanks
yes, maybe I should use itertools for this practice. because in this one it only checks the distance with the previous point and because of that it has the issue you mentioned below.

Even if you check both distances, to the previous and next neighbours and for instance chose the smallest or biggest value, you’ll get intersections or no intersections. The problem is that the points aren’t equally spaced. You’d have to slightly adjust those, like in a circle relaxation simulation.

Pressing the Test button or F5 does clear global variables, while just running the component “from outside” does indeed not. It was a clever design by @piac that enabled both persistent variables between runs, and an easy way to debug/clear “phantoms” variables: