以前dixon在GH的课上做了一棵树,我现在用python写出来(有点小瑕疵),代码如下。我现在想把这些直线的树枝做成连续的曲线,这样看上去可能会有优美些。可是问题是怎么做到每个树枝都很连贯,过渡的也很平滑?不知道你们有什么意见。
import rhinoscriptsyntax as rs
import random as r
def Main():
startline=rs.GetObject ("select a line to start a tree")
angle=rs.GetReal ("type angle",15)
gens=rs.GetInteger ("type number of generations",4)
CurGen=[]
CurGen.append(startline)
for i in range(gens):
NewGen=[]
for line in CurGen:
Newlines= branch(line,angle)
NewGen.extend(Newlines)
CurGen=NewGen
def branch(line,angle):
startpt=rs.CurveStartPoint(line)
endpt=rs.CurveEndPoint(line)
vec=rs.VectorCreate(endpt,startpt)
vec1=rs.VectorRotate(vec,r.random()*60,[0,1,1])
vec2=rs.VectorRotate(vec,-r.random()*60,[-1,-1,1])
vec3=rs.VectorRotate(vec,r.random()*60,[1,-1,1])
endpt1=rs.PointAdd(endpt,vec1)
endpt2=rs.PointAdd(endpt,vec2)
endpt3=rs.PointAdd(endpt,vec3)
line1=rs.AddLine(endpt,endpt1)
midpt1 = rs.CurveMidPoint(line1)
newmidpt1=rs.MoveObject(rs.AddPoint(midpt1),(0,0,4))
points1 = (endpt),(newmidpt1),(endpt1)
curve1= rs.AddCurve(points1)
#line2=rs.AddLine(endpt,endpt2)
line2=rs.AddLine(endpt,endpt2)
midpt2 = rs.CurveMidPoint(line2)
newmidpt2=rs.MoveObject(rs.AddPoint(midpt2),(0,0,4))
points2 = (endpt),(newmidpt2),(endpt2)
curve2= rs.AddCurve(points2)
line3=rs.AddLine(endpt,endpt3)
midpt3 = rs.CurveMidPoint(line3)
newmidpt3=rs.MoveObject(rs.AddPoint(midpt3),(0,0,4))
points3 = (endpt),(newmidpt3),(endpt3)
curve3= rs.AddCurve(points3)
#line3=rs.AddLine(endpt,endpt3)
#return[line1,line2,line3]
return[curve1,curve2,curve3]
delete(line1,line2,line3)
Main()
|