Difference between revisions of "workshop01G3:Codefarm"

From CECO
Jump to: navigation, search
(recursive branching system)
Line 14: Line 14:
 
[[workshop01G3:References|'''References''']]</div>
 
[[workshop01G3:References|'''References''']]</div>
 
</div><br>
 
</div><br>
 
 
 
import rhinoscriptsyntax as rs
 
import random
 
import scriptcontext
 
# define a function called drawbranch
 
# the integer c in the function with a format drawbranch(a,b,c), actually, drawbranch(4,line,4)
 
# m is a parameter to avoid having to manually change
 
def drawbranch(x,line,m):
 
    #introduce possibility to use 'escape' key to avoid computer crash
 
    scriptcontext.escape_test()
 
    #define a condition to exit from funtion drawbranch, essentially increasing the i
 
    if x==0:
 
        return
 
    else:
 
        for i in range (0,m):
 
            # get start and end points of a starting curve, first curve that will be drawn
 
            endpt=rs.CurveEndPoint(line)
 
            srtpt=rs.CurveStartPoint(line)
 
            #create a vector between the endpoint and the startpoint of the initial curve
 
            vect=rs.VectorCreate(endpt,srtpt)
 
            #get 3 random integer which will be used as a degree of rotation for the initial curve,these vectors will draw the branch
 
            rnd=random.randint(-35,35)
 
            rnd1=random.randint(-15,15)
 
            rnd2=random.randint(-5,15)
 
            #3D-rotate the vector with values got from random
 
            vect=rs.VectorRotate(vect,rnd,[rnd1,rnd2,1])
 
            #add a point at the end of the 3D vector
 
            ptadd=rs.PointAdd (endpt,vect)
 
            #add a line between the endPoint of the vector and the newly added point
 
            line=rs.AddLine (endpt,ptadd)
 
            #INSIDE THE FOR-LOOP, WRITE A CONDITION FOR ADDING POINTS, BUT ONLY AT THE END OF THE LAST BRANCH
 
            #IF YOU PROVIDE A LOW NUMBER OF ITERATIONS, IT WILL BECOME RECOGNISEABLE THAT THE CONDITION FOR DRAWING THE LAST BRANCH OF EVERY ITERATION
 
            #IS WHEN X==1 AND I==THE END OF THE PROVIDED RANGE(m)-1
 
            if x==1 and i==m-1:
 
                #if condition is met, add points at the end of the branch
 
                    e=rs.AddPoint(ptadd)
 
                    endPoints.append(e)
 
                    #calling the function THIS IS WHEN THE ACTUAL BRANCH IS DRAWN
 
            drawbranch(x-1,line,m)
 
#make a list to store endPoints
 
endPoints=[]
 
#provide starting line for the future tree
 
line=rs.AddLine([0,0,0],[0,0,20])
 
#calling the function for the chosen values
 
drawbranch(4,line,3)
 
#icluding a random seed for variation
 
rndseed=random.seed(5)
 
#adding a layer endPoints
 
rs.AddLayer("endPoints")
 
#adding PolyLine between points in endPoints
 
for pt in endPoints:
 
    rs.ObjectLayer(pt,"endPoints")
 
rs.AddPolyline(endPoints, None)
 

Revision as of 23:32, 11 September 2013