Hi everyone, I am new to the forum and a 2-week new Rhino learner without much programming experience, but trying to create a recursive loop with python(!). Brain racked for days so am seeking help here…
I attempt to write a script that draws a circle within a circle and continues for a few generations. As I am confused about the syntax, I am mostly only able to write down the steps I think the script should do.
import rhinoscriptsyntax as rs
def concentric_circles():
circle = rs.GetObject("select starting circle",4)
if not circle:
print ("Without giving me a circle what do you expect?")
return
if not rs.IsCircle(circle):
print ("Did you try to fool me?")
return
radius = rs.CircleRadius(circle)
if radius<2.0:
print ("Your circle is to small for this task.")
return
while radius>2.0:
radius-=1
center = rs.CircleCenterPoint(circle)
rs.AddCircle(center, radius)
concentric_circles()
import rhinoscriptsyntax as rs
import random
import math
def touching_circles():
circle = rs.GetObject("select starting circle",4)
if not circle:
print ("without giving me a circle what do you expect?")
return
if not rs.IsCircle(circle):
print ("did you try to fool me?")
return
radius = rs.CircleRadius(circle)
if radius<2.0:
print ("Your circle is to small for this task.")
return
while radius>2.0:
radius-=1
center = rs.CircleCenterPoint(circle)
circle = rs.AddCircle(center, radius)
angle = random.random()*2*math.pi
x = math.sin(angle)
y = math.cos(angle)
trans=(x,y,0)
rs.MoveObject(circle, trans)
touching_circles()
and one for spiraling circles:
import rhinoscriptsyntax as rs
import random
import math
def spiraling_circles():
circle = rs.GetObject("select starting circle",4)
if not circle:
print ("without giving me a circle what do you expect?")
return
if not rs.IsCircle(circle):
print ("did you try to fool me?")
return
radius = rs.CircleRadius(circle)
if radius<2.0:
print ("Your circle is to small for this task.")
return
angle = 0
while radius>2.0:
radius-=1
center = rs.CircleCenterPoint(circle)
circle = rs.AddCircle(center, radius)
angle+= math.pi/7
x = math.sin(angle)
y = math.cos(angle)
trans=(x,y,0)
rs.MoveObject(circle, trans)
spiraling_circles()
Thank you so much for the quick response! I am working based on your solution although I haven’t grasped the angle/sin/cos bits. Let me finish it off and come back later. Cheers!
Just look for polar coordinates in your favorite search engine. The relation between a point on a circle with radius 1 and the derived x and y coordinates is that for any angle a, x=cos(a) and y=sin(a)
Although I swapped x and y in my example but it’s in fact the same logic.
So I take a random angle and then move the circle in that direction 1 unit.