王振风与王鹿鸣

Page 1

_algorithm


3d-attractor Option Explicit ‘Script written by FUN ‘3d attractor ‘Script version 2008,1,27, 11:49:53 ‘ x’ = sin(Ay) − z cos(Bx) ‘ y’ = z sin(Cx) − cos(Dy) ‘ z’ = E sin(x) Call Main() Sub Main() Dim A,B,C,D,E Dim x,y,z,n, i,j dim x1,y1,z1 dim sphere,mate A=2 B=0.5 C=-1 D=-1 E=2 x=0 y=0 z=0 ‘call rhino.EnableRedraw(false) for i= 0 to 10000 sphere=rhino.AddSphere(array(x,y,z),1/100) x1=x y1=y z1=z x=sin(A*y)-Cos(B*x)*z y=sin(C*x)*z-Cos(D*y) z=E*sin(x) ‘x=sin(A*y)-cos(A*x)*C ‘y=sin(B*x)-cos(B*y)*D ‘z=sin(B*x)+cos(B*y)*D Dim r: r=int(255*(i/10000)) Dim g: g=int(250) Dim b1: b1=int(250) mate=rhino.AddMaterialToObject(sphere) Call rhino.ObjectColor(sphere,RGB(r,g,b1)) Call rhino.MaterialColor(mate,RGB(r,g,b1)) Next ‘call rhino.EnableRedraw(True) End Sub



8 queens puzzle The eight queens puzzle is the problem of putting eight chess queens on an 8×8 chessboard such that none of them is able to capture any other using the standard chess queen’s moves. This means it is as if all the queens are different colors. The queens must be placed in such a way that no two queens would be able to attack each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general n queens puzzle of placing n queens on an n×n chessboard (n ≥ 4). -- Wikipedia The eight queens puzzle has 92 distinct solutions. If solutions that differ only by symmetry operation (rotations and reflections) of the board are counted as one, the puzzle has 12 unique solutions. The script uses backtracing to find all the distinct solutions for “n queens puzzle” problem.


N=7


Option Explicit ‘Script written by FUN ‘N queens puzzle ‘Script version Friday, December 21, 2007 4:24:30 PM Call Main() Sub Main() Dim n, i, j Dim kk, arrResult(),count count=0 n=rhino.GetInteger(“number of queens please”,8) kk=0 ReDim arrResult(kk) For i= 0 To n-1 arrResult(0)=array(0,i,0) Call queenspuzzle(arrResult,1,n, count) Next End Sub Function queenspuzzle(pttest,column,n,count) Dim k, arrResult(),blnTest,i,j,ptqueen If column>n Then Exit Function If column=n Then count=count+1 For i=0 To Ubound(pttest) pttest(i)(2)=pttest(i)(2)+count Call addtube(pttest(i)) Next End If ReDim arrResult(Ubound(pttest)) For i = 0 To Ubound(pttest) arrResult(i)=pttest(i) Next For i=0 To n-1 ptqueen=array(column,i,0) blnTest=True For j= 0 To Ubound(pttest) If blnTest Then If pttest(j)(1)=ptqueen(1) Or abs (pttest(j)(1)+pttest(j)(0))= abs(ptqueen(1)+ptqueen(0)) Or pttest(j)(1)pttest(j)(0)= ptqueen(1)-ptqueen(0) Then blnTest=False End If End If Next If blnTest Then ReDim Preserve arrResult(Ubound(pttest)+1)



k=Ubound(arrResult) arrResult(k)=ptqueen Call queenspuzzle(arrResult,column+1,n,count) End If Next End Function Function addtube(arrpt) Dim i,edge,arrPtpoly(4),polyline,arrsur,strcur edge=1 arrPtpoly(0)=arrpt arrPtpoly(4)=arrpt arrPtpoly(1)=array(arrpt(0)+edge,arrpt(1),arrpt(2)) arrPtpoly(2)=array(arrpt(0)+edge,arrpt(1)+edge,arrpt(2)) arrPtpoly(3)=array(arrpt(0),arrpt(1)+edge,arrpt(2)) polyline=rhino.AddPolyline(arrPtpoly) arrsur=rhino.AddPlanarSrf(array(polyline)) strcur=rhino.AddLine(array(0,0,0),array(0,0,1)) call rhino.ExtrudeSurface(arrsur(0),strcur) End Function



convex-hull In mathematics, the convex hull or convex envelope for a set of points X in a real vector space V is the minimal convex set containing X. ---Wikipedia The gift wrapping algorithm is an algorithm for computing the convex hull of a set of points in two or more dimensions Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGs ‘Script version 2008 1 2 , 21:39:12 Call Main() Sub Main() Dim arrPtDone(),t,arrPtInput(),arrTemp(),k t=0 ReDim arrPtInput(t) arrPtInput(0)=randomPts(200) Do While Ubound(arrPtInput(t))>=2 ReDim Preserve arrPtDone(t) ReDim Preserve arrPtInput(t) arrPtInput(t)=rhino.SortPoints(arrPtInput(t)) arrPtDone(t)=convex(arrPtInput(t)) ReDim Preserve arrPtInput(t+1) arrPtInput(t+1)=arrayDeduct(arrPtInput(t),arrPtDone(t)) t=t+1 Loop End Sub Function Convex(arrPtInput) Dim p,t,i,j,test,chaji,te,k Dim arrPtConvex() t=0 ReDim arrPtConvex(t) arrPtConvex(0)=arrPtInput(1) Do While arrPtConvex(t)(0)<>arrPtInput(0)(0) Or arrPtConvex(t) (1)<>arrPtInput(0)(1) arrPtConvex(0)=arrPtInput(0) i=t For j=0 To Ubound(arrPtInput) test=True If arrPtInput(j)(0)<>arrPtConvex(i)(0) And test Then For k=0 To Ubound(arrPtInput) 10


200 POINTS

11


If k<>j And (arrPtInput( k)(0)<>arrPtConvex(i)(0) Or arrPtInput(k)(1)<>arrPtConvex(i)(1))Then chaji=(arrPtInput(j)(0)-arrPtConvex(i)(0))*(arrPtInput(k)(1)arrPtConvex(i)(1))-(arrPtInput(k)(0)-arrPtConvex(i)(0))*(arrPtInput(j)(1) -arrPtConvex(i)(1)) If chaji<=0 Then test=False End If End If Next If test Then t=t+1 ReDim Preserve arrPtConvex(t) arrPtConvex(t)=arrPtInput(j) Call rhino.AddCone(arrPtConvex(t),arrPtConvex(t-1),0.5) End If End If Next Loop Call rhino.AddPolyline(arrPtConvex) convex=arrPtConvex End Function Function arrayDeduct(arrInput,arrDone) Dim i, j, arrNew(),p,test p=0 test=True ReDim arrNew(p) For i=0 To Ubound(arrInput) test=True For j=0 To Ubound(arrDone) If rhino.PointCompare(arrInput(i),arrDone(j)) Then test=False End If Next If test Then ReDim Preserve arrNew(p) arrNew(p)=arrInput(i) p=p+1 End If Next arrayDeduct=arrNew End Function Function randomPts(number) Dim x, y, i, a, b, arrPts() ReDim arrPts(number-1) a=100 b=100 12


Call rhino.CurrentLayer(“Layer 01”) For i= 0 To number-1 x=rnd()*100 y=rnd()*100 arrPts(i)=array(x,y,0) Call rhino.AddPoint(arrPts(i)) Call rhino.AddSphere(arrPts(i),0.5) Next randomPts=arrPts Call rhino.CurrentLayer(“Layer 02”) End Function

600 POINTS

13


divide&conquer Divide and conquer was a successful military strategy long before it became an algorithm design paradigm. Generals observed that it was easier to defeat one army of 50,000 men, followed by another army of 50,000 men than it was to beat a single 100,000 man army. Thus the wise general would attack so as to divide the enemy army into two forces and then mop up one after the other. To use divide and conquer as an algorithm design technique, we must divide the problem into two smaller subproblems, solve each of them recursively, and then meld the two partial solutions into one solution to the full problem. Whenever the merging takes less time than solving the two subproblems, we get an efficient algorithm ----Algorithms Mon Jun 2 23:33:50 EDT 1997 You can hang the whole structure on the red point, it will keep balance The script calculate the network connecting 2^8=256 random size cone together and keep them balance on one (the red) point (the weight of the connection tubes are calculated) Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGs ‘Script version 2008,2,17 19:46:05 ‘Divide & Conquer Call Main() Sub Main() Dim arrPosition(), arrWeight() ,conDensity Dim intBall, i ,j, times,moment(),tt,radias ReDim moment(0) ‘----------------------- times=8 conDensity=1 radias=10 ‘---------------------- intBall=2^times-1 ReDim arrPosition(intBall) ReDim arrWeight(intBall) For i=0 To intBall arrPosition(i)= positionWeight(1500,2000) Call drawcone(arrPosition(i),radias) Next Call DivideConquer(arrPosition,moment,conDensity) tt=Ubound(moment) Call rhino.AddSphere(moment(tt-1),9) 14


15


End Sub Function positionWeight(bound,weight) ‘lamp position and weight Dim x,y,z,w x=rnd()*bound y=rnd()*bound z=0 w=rnd()*weight positionWeight=array(x,y,z,w) End Function Function DivideConquer(arrPo,moment,conDensity) Dim i,j, mid, arrTempA(),arrTempB() Dim lamp1, lamp2,t If Ubound(arrPo)=0 Then ‘Conquer DivideConquer=arrPo(0) lamp1=arrPo(0) lamp2=arrPo(0) Else mid=(Ubound(arrPo)-1)/2 ‘divide the array to two equal parts ReDim arrTempA(mid) For i=0 To mid arrTempA(i)=arrPo(i) Next ReDim arrTempB(mid) For i=mid+1 To Ubound(arrPo) arrTempB(i-(mid+1))=arrPo(i) Next lamp1=DivideConquer(arrTempA,moment,conDensity) lamp2=DivideConquer(arrTempB,moment,conDensity) End If DivideConquer=merge(lamp1,lamp2,moment,conDensity) ‘Merge two parts to one t=Ubound(moment) ReDim Preserve moment(t+1) End Function Function merge(arrpt1W,arrpt2W,moment,conDensity) Dim arrp1(2),arrp2(2),k,arrMoments(3),t ,arrMomPt,conMom(3), midpt(2),line Dim distance k=arrpt2W(3)/(arrpt2W(3)+arrpt1W(3)) arrMoments(0)=arrpt1W(0)+(arrpt2W(0)-arrpt1W(0))*k arrMoments(1)=arrpt1W(1)+(arrpt2W(1)-arrpt1W(1))*k arrMoments(2)=arrpt1W(2)+(arrpt2W(2)-arrpt1W(2))*k arrMoments(3)=arrpt1W(3)+ arrpt2W(3) arrMomPt=arrMoments arrp1(0)=arrpt1W(0) 16


17


arrp1(1)=arrpt1W(1) arrp1(2)=arrpt1W(2) arrp2(0)=arrpt2W(0) arrp2(1)=arrpt2W(1) arrp2(2)=arrpt2W(2) line=rhino.AddLine(arrp1,arrp2) Call rhino.AddCylinder(arrp1,arrp2,conDensity) distance=rhino.Distance(arrp1,arrp2) midpt(0)=(arrp1(0)+ arrp2(0))/2 midpt(1)=(arrp1(1)+ arrp2(1))/2 midpt(2)=(arrp1(2)+ arrp2(2))/2 conMom(0)=midpt(0) conMom(1)=midpt(1) conMom(2)=midpt(2) conMom(3)=distance*conDensity merge=mergeagain(arrMoments,conMom,moment) End Function Function mergeagain(arrpt1W,arrpt2W,moment) Dim k,arrMoments(3),t Dim distance k=arrpt2W(3)/(arrpt2W(3)+arrpt1W(3)) arrMoments(0)=arrpt1W(0)+(arrpt2W(0)-arrpt1W(0))*k arrMoments(1)=arrpt1W(1)+(arrpt2W(1)-arrpt1W(1))*k arrMoments(2)=arrpt1W(2)+(arrpt2W(2)-arrpt1W(2))*k arrMoments(3)=arrpt1W(3)+ arrpt2W(3) mergeagain=arrMoments t=Ubound(moment) moment(t)=array(arrMoments(0),arrMoments(1),arrMoments(2)) End Function Function drawcone(arrPosition,radias) Dim height, oriPt(2), BoPt(2) height=arrPosition(3)/radias oriPt(0)=arrPosition(0) oriPt(1)=arrPosition(1) oriPt(2)=arrPosition(2) BoPt(0)=arrPosition(0) BoPt(1)=arrPosition(1) BoPt(2)=arrPosition(2)-height Call rhino.AddCone(BoPt,oriPt,radias) End Function 18


19


DLA Diffusion-limited aggregation (DLA) is the process whereby particles undergoing a random walk due to Brownian motion cluster together to form aggregates of such particles. This theory, proposed by Witten and Sander in 1981 ,is applicable to aggregation in any system where diffusion is the primary means of transport in the system. DLA can be observed in many systems such as electrodeposition, Hele-Shaw flow, mineral deposits, and dielectric breakdown. ---Wikipedia Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGs ‘Script version 2008,1,12 12:50:08 ‘1:seed,stable, 2:empty, 3:mobile, 4:outside Call Main() Sub Main() Dim x,y,z, i, j,k, intSeed, intMobile ,arrTemp Dim arrState() Dim seedx,seedy,seedz,mobilex,mobiley,mobilez x=10 y=10 z=30 intSeed=3 intMobile=600 ReDim arrState(x,y,z) ‘initial the matrix as empty For i=0 To x For j=0 To y For k=0 To z arrState(i,j,k)= 2 ‘call rhino.AddPoint(array(i,j,k)) Next Next Next ‘put seed randomly For i=1 To intSeed seedx=int(1+rnd()*(x-2)) seedy=int(1+rnd()*(y-2)) seedz=int(1+rnd()*(z-2)) arrState(seedx,seedy,seedz)=1 Call drawStar(array(seedx,seedy,seedz),1) Next ‘put mobile randomly but not on the position of the seed For i=1 To intMobile mobilex=int(1+rnd()*(x-2)) mobiley=int(1+rnd()*(y-2)) mobilez=int(1+rnd()*(z-2)) 20


21


If Not arrState(mobilex,mobiley,mobilez)=1 Then arrState(mobilex,mobiley,mobilez)=3 End If Next Do While intMobile>0 For i=1 To x-1 For j=1 To y-1 For k=1 To z-1 If arrState(i,j,k)= 3 Then Call rhino.Print(“pp”) Call checkMove(array(i,j,k),arrState,intMobile) Call rhino.Print(intMobile) End If Next Next Next Loop End Sub Function checkMove(arrPo, arrState,intMobile) Dim i,j,k,test test=False For i=arrPo(0)-1 To arrPo(0)+1 For j=arrPo(1)-1 To arrPo(1)+1 For k=arrPo(2)-1 To arrPo(2)+1 If Not (i=arrPo(0)And j=arrPo(1) And k=arrPo(2)) Then If arrState(i,j,k)=1 Then test=True Call rhino.Print(rhino.Pt2Str(arrPo)) End If End If Next Next Next If test Then arrState(arrPo(0),arrPo(1),arrPo(2))=1 Call drawStar(arrPo,1) intMobile=intMobile-1 Else arrState(arrPo(0),arrPo(1),arrPo(2))=2 i=arrPo(0)+(-1+int(rnd()*2)) j=arrPo(1)+(-1+int(rnd()*2)) k=arrPo(2)+(-1+int(rnd()*2)) arrState(i,j,k)=3 End If End Function Function drawStar(PtCor,length) Dim ptx, pty, ptz,ptCen Dim pt0,pt1,pt2,pt3,pt4,pt5,pt6,pt7 22


23


Dim Radias,pt(7),i Radias=0.1 ptx=PtCor(0)+rnd(length) pty=PtCor(1)+rnd(length) ptz=PtCor(2)+rnd(length) ptCen=array(ptx,pty,ptz) pt(0)=PtCor pt(1)=array(PtCor(0),PtCor(1)+length,PtCor(2)) pt(2)=array(PtCor(0),PtCor(1),PtCor(2)+length) pt(3)=array(PtCor(0),PtCor(1)+length,PtCor(2)+length) pt(4)=array(PtCor(0)+length,PtCor(1),PtCor(2)) pt(5)=array(PtCor(0)+length,PtCor(1)+length,PtCor(2)) pt(6)=array(PtCor(0)+length,PtCor(1),PtCor(2)+length) pt(7)=array(PtCor(0)+length,PtCor(1)+length,PtCor(2)+length) call rhino.AddSphere(ptCen,Radias) for i=0 to 7 Call coolSurface(ptCen,pt(i),Radias) call rhino.AddSphere(pt(i),Radias) Next End Function Function coolSurface(arrPt1,arrPt2,Radias) Dim arrCurSec,dblRmid Dim strCir1, strCir2,strCirmid, strCirOnSur1,strCirOnSur2 Dim dblR1,dblR2,dist,vec,arrPtmid, arrloftsur vec=rhino.VectorCreate(arrPt1,arrPt2) dist=rhino.Distance(arrPt1,arrPt2) dblR1=Radias dblR2=Radias dblRmid=dist/50 arrPtmid=array((arrPt1(0)+arrPt2(0))/2,(arrPt1(1)+arrPt2(1))/ 2,(arrPt1(2)+arrPt2(2))/2) strcir1=DrawCircle(vec,arrpt1,dblR1) strcir2=DrawCircle(vec,arrpt2,dblR2) strCirmid=DrawCircle(vec,arrPtmid,dblRmid) arrCurSec=array(strCir1,strCirmid,strCir2) arrloftsur=rhino.AddLoftSrf(arrCurSec) coolSurface=arrloftsur(0) End Function Function DrawCircle(vec,Ptstart,Radias) Dim plane plane=rhino.PlaneFromNormal(Ptstart,vec) DrawCircle=rhino.AddCircle(plane,Radias) End Function

24


25


genetic-algorithm A genetic algorithm (GA) is a search technique used in computing to find exact or approximate solutions to optimization and search problems. Genetic algorithms are categorized as global search heuristics. Genetic algorithms are a particular class of evolutionary algorithms (also known as evolutionary computation) that use techniques inspired by evolutionary biology such as inheritance, mutation, selection, and crossover (also called recombination).--------Wikipedia Option Explicit ‘Script written by FUN ‘calculate the point has the shortest sum distance to a group of points ‘using genetic algorithm ‘Script version 11,4,2007 14:15:01 Call Main() Sub Main() Dim dnaPt, popu(),i,Max,j,record,matee Dim PtTargets,mutateR,mpool,pttemp,mm Dim best, bestPt best=10000000 mutateR=0.01 PtTargets=rhino.GetPointCoordinates(“select points”) For i=0 To Ubound(PtTargets) Call rhino.AddSphere(PtTargets(i),1) Next ‘-----fill the first population Max=100 ReDim popu(Max) For i=0 To Max popu(i)=array(rnd()*200,rnd()*200,rnd()*200) Next ‘---------------------------------- bestPt=drawstar(popu(0),PtTargets) Do record=worldrecord(popu,PtTargets) ‘-----get the best point in the current population-- mpool=naturalselection(popu,PtTargets,record) For i=0 To Ubound(popu) popu(i)=generate(mpool) Next Call rhino.Print(record) mm=Ptrecord(popu,PtTargets) pttemp=drawstar(popu(mm),PtTargets) Call rhino.DeleteObjects(pttemp) ‘-----keep the best record-------- If best>record Then best=record Call rhino.DeleteObjects(bestPt) bestPt=drawstar(popu(mm),PtTargets) End If 26


27


Loop End Sub

Call rhino.Print(Ubound(mpool))

Function worldrecord(popu,PtTargets) ‘calculate the best point in the population Dim dis, i, j, short short=100000000000000 For i=0 To Ubound(popu) For j=0 To Ubound(PtTargets) dis=dis+rhino.Distance(popu(i),PtTargets(j)) Next If short>dis Then short=dis End If Next worldrecord=short End Function Function mate(DNA, partner) ‘crossover the mom and dad to create a child Dim i,j,crossover,child(2) crossover=int(rnd()*2.99) For i=0 To 2 If i>crossover Then child(i)=partner(i) Else child(i)=DNA(i) End If Next mate=child End Function Function mutate(DNA,mr) ‘mutate the child according to certain mutation rate Dim i For i=0 To 2 If rnd()<mr Then DNA(i)=rnd()*200 End If Next mutate=DNA End Function Function distance(arrPt,PtTargets) Dim i distance=0 For i=0 To Ubound(PtTargets) distance=distance+rhino.Distance(arrPt,PtTargets(i)) Next End Function Function naturalselection(popu,PtTargets,record) ‘create a mating pool put children in according to the fitness of them ‘the better fit, the more being put in 28


Dim i,j,fitdis,n,matepool(),kk ReDim matepool(0) kk=0 For i=0 To Ubound(popu) fitdis=distance(popu(i),PtTargets) n=int(record/fitdis*50) For j=0 To n ReDim Preserve matepool(kk) matepool(kk)=popu(i) kk=kk+1 Next Next naturalselection=matepool End Function Function generate(matpool) ‘generate a new population Dim i,j,mo,da,mom,dad,children mo=int(rnd()*Ubound(matpool)) da=int(rnd()*Ubound(matpool)) mom=matpool(mo) dad=matpool(da) children=mate(mom,dad) children=mutate(children,0.01) generate=children End Function Function Ptrecord(popu,PtTargets) ‘get the positon of the bestrecord point Dim dis, i, j, short,mmm short=100000000000000 mmm=0 For i=0 To Ubound(popu) For j=0 To Ubound(PtTargets) dis=dis+rhino.Distance(popu(i),PtTargets(j)) Next If short>dis Then short=dis mmm=i End If Next Ptrecord=mmm End Function Function drawstar(arrPt,PtTargets) Dim lines(),i,j ReDim lines(Ubound(PtTargets)) For i= 0 To Ubound(PtTargets) ‘lines(i)=rhino.AddLine(arrPt,PtTargets(i)) lines(i)=rhino.AddCone(arrPt,PtTargets(i),2) Next drawstar=lines End Function 29


loose-fractal A genetic algorithm (GA) is a search technique used in computing to find exact or approximate solutions to optimization and search problems. Genetic algorithms are categorized as global search heuristics. Genetic algorithms are a particular class of evolutionary algorithms (also known as evolutionary computation) that use techniques inspired by evolutionary biology such as inheritance, mutation, selection, and crossover (also called recombination).--------Wikipedia Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGS ‘Script version 2007.10.13. 21:47:45 Call Main() Sub Main() Dim arrPt1, arrPt2 arrPt1=rhino.GetPoint(“pick the first point”) arrPt2=rhino.GetPoint(“pick the second point”) Call drawstar(arrPt1,arrPt2) End Sub Function drawstar(arrPt1,arrPt2) Dim strLine, curDom, arrPtonCur,t Dim cenPt, vecNormal,plane, dblR Dim strCircle, arrPtStar,i Dim arrShortpt, DisTest strLine=rhino.AddLine(arrPt1, arrPt2) arrPtonCur= randomPTonCur(strLine) vecNormal=rhino.VectorCreate(arrPt1, arrPt2) plane=rhino.PlaneFromNormal(arrPtonCur, vecNormal) dblR=rhino.Distance(arrPt1, arrPt2) strCircle=rhino.AddCircle(plane,dblR) cenPt=randomPTonCur(strCircle) arrPtStar=SurroundPt(arrPt1, arrPt2,cenPt,dblR) Call addlinestoStar(arrPtStar,cenPt) arrShortpt=shortarm(arrPtStar,cenPt) DisTest=rhino.Distance(cenPt,arrShortpt) If DisTest<1 Then Exit Function Call drawstar(cenPt,arrShortpt) End Function Function randomPTonCur(strCur) Dim curDom,t curDom=rhino.CurveDomain(strCur) t=curDom(0)+(curDom(1)-curDom(0))*rnd() randomPTonCur=rhino.EvaluateCurve(strCur, t) Call rhino.DeleteObject(strCur) 30


31


Call rhino.DeleteObject(strCur) End Function Function SurroundPt(arrPt1, arrPt2,cenPt,dblR) Dim n, arrPts,num,i, j num=int(rnd()*3+1) ReDim arrPts(num+1) arrPts(0)=arrPt1 arrPts(1)=arrPt2 For i=2 To Ubound(arrPts) arrPts(i)=rndPtinR(cenPt,dblR) Next SurroundPt=arrPts End Function Function rndPtinR(cenPt,dblR) Dim x, y, z x=(cenPt(0)-dblR)+rnd()*2*dblR y=(cenPt(1)-dblR)+rnd()*2*dblR z=(cenPt(2)-dblR)+rnd()*2*dblR rndPtinR=array(x,y,z) Call rhino.AddPoint(rndPtinR) End Function Function addlinestoStar(arrPtStar,cenPt) Dim i,dblDis,dblR For i=0 To Ubound(arrPtStar) dblDis=rhino.Distance(arrPtStar(i),cenPt) call rhino.AddCone(cenPt,arrPtStar(i),dblDis/40) Next End Function Function shortarm(arrPtStar,cenPt) Dim dblDist,dblshort,i, index dblshort=rhino.Distance(cenPt,arrPtStar(0)) For i=0 To Ubound(arrPtStar) dblDist=rhino.Distance(cenPt,arrPtStar(i)) If dblDist<=dblshort Then dblshort=dblDist index=i End If Next shortarm=arrPtStar(index) End Function

32


33


L-system-arm An L-system or Lindenmayer system is a formal grammar (a set of rules and symbols) most famously used to model the growth processes of plant development, but also able to model the morphology of a variety of organisms. L-systems can also be used to generate self-similar fractals such as iterated functionsystems. ---------Wikipedia These images are generated by the same script with different seeds and rules. Option Explicit ‘Script written by FUN ‘Script version 2007 11 15 22:55:10 ‘A L-system ‘”F”-draw; “+”-turn right; “-”-turn left; “[“-roll left; “]”-roll right; “{“-push matrix; “}”-pop matrix Call Main() Sub Main() Dim arrPtSta, vecDir,generation Dim strSeed, arrSeed,i,j,k,arrSeedTp(),n Dim arrRule,strRule,strSur arrPtSta=array(0,0,0) vecDir=rhino.VectorCreate(array(0,0,1),array(0,0,0)) ‘”1” ‘strSeed=”F F + F } [ - F [“ ‘”1” ‘strRule=”F } + F F } + [ [ } F” ‘”2” ‘strSeed=”F F + F } [ - F [ F [ } F F } F” ‘”2” ‘strRule=”F } + [ F” ‘”3” ‘strSeed=”F F + F } [ - F [“ ‘”3” ‘strRule=”F + F } + [ [ } F F” ‘”4” strSeed=”F } F + [ F - F” ‘”4” strRule=”F + [ F } - ] F” ‘strSeed=”F” ‘strRule=”F + F } - F + F } - F “ strSur=rhino.GetObject(“select surface”) arrSeed=rhino.Strtok(strSeed) generation=6 ‘----Put the initial str to strSeed-----------34


35


For k=1 To generation ReDim arrSeedTp(0) n=0 For i=0 To Ubound(arrSeed) ReDim Preserve arrSeedTp(n) If arrSeed(i)=”+” Then arrSeedTp(n)=”+” n=n+1 End If If arrSeed(i)=”-” Then arrSeedTp(n)=”-” n=n+1 End If If arrSeed(i)=”[“ Then arrSeedTp(n)=”[“ n=n+1 End If If arrSeed(i)=”]” Then arrSeedTp(n)=”]” n=n+1 End If If arrSeed(i)=”{“ Then arrSeedTp(n)=”{“ n=n+1 End If If arrSeed(i)=”}” Then arrSeedTp(n)=”}” n=n+1 End If If arrSeed(i)=”F” Then arrRule=rhino.Strtok(strRule) For j=0 To Ubound(arrRule) ReDim Preserve arrSeedTp(n) arrSeedTp(n)=arrRule(j) n=n+1 Next End If Next ‘-----generate new seed for next generation according to the rules---- ReDim arrSeed(Ubound(arrSeedTp)) arrSeed=arrSeedTp Call Turtle(arrSeed,k,strSur) Next End Sub Function Turtle(arrSeed,k,strSur) 36


37


Dim i,j Dim arrPtSta, vecDir, dblAngt, dblAngr Dim vec01, vec02,vecAxist,vecAxisr,arrPtonSur,arrUV vec01=rhino.VectorCreate(array(100,0,0),array(0,0,0)) vec01=rhino.VectorScale(vec01,1/(k+3)) vec02=rhino.VectorCreate(array(0,0,100),array(0,0,0)) vec02=rhino.VectorScale(vec02,1/(k+3)) vecAxist=rhino.VectorCrossProduct(vec01,vec02) vecAxisr=vec02 vecDir=vec02 dblAngt=40.0 dblAngr=30.0 arrPtSta=array(0,0,0) For i=0 To Ubound(arrSeed) If arrSeed(i)=”+” Then vecDir=rhino.VectorRotate(vecDir,dblAngt,vecAxist) vec01=vec02 vec02=vecDir vecAxist=rhino.VectorCrossProduct(vec01,vec02) vecAxisr=vec01 End If If arrSeed(i)=”-” Then vecDir=rhino.VectorRotate(vecDir,-dblAngt,vecAxist) vec01=vec02 vec02=vecDir vecAxist=rhino.VectorCrossProduct(vec01,vec02) vecAxisr=vec01 End If If arrSeed(i)=”[“ Then vecDir=rhino.VectorRotate(vecDir,dblAngr,vecAxisr vec01=vec02 vec02=vecDir vecAxist=rhino.VectorCrossProduct(vec01,vec02) vecAxisr=vec01 End If If arrSeed(i)=”]” Then vecDir=rhino.VectorRotate(vecDir,-dblAngr,vecAxisr) vec01=vec02 vec02=vecDir vecAxist=rhino.VectorCrossProduct(vec01,vec02) vecAxisr=vec01 End If If arrSeed(i)=”{“ Then arrPtSta=rhino.PointAdd(arrPtSta,vecDir) End If 38


39


If arrSeed(i)=”}” Then arrPtSta=rhino.PointSubtract(arrPtSta,vecDir) End If If arrSeed(i)=”F” Then arrUV=rhino.SurfaceClosestPoint(strSur,arrPtSta) arrPtSta=rhino.EvaluateSurface(strSur,arrUV) Call drawarm(arrPtSta, vecDir, k, strSur) arrPtSta=rhino.PointAdd(arrPtSta,vecDir) End If Next End Function Function drawarm(arrPtSta, vecDir, k, strSur) Dim arrPtEnd,mater,cone,arrUV arrPtEnd=rhino.PointAdd(arrPtSta,vecDir) arrUV=rhino.SurfaceClosestPoint(strSur,arrPtEnd) arrPtEnd=rhino.EvaluateSurface(strSur,arrUV) cone=rhino.AddCylinder(arrPtSta,arrPtEnd,3/(k+2)) End Function

40


41


tower-of-hanoi The Tower of Hanoi or Towers of Hanoi is a mathematical game or puzzle. It consists of three pegs, and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another peg, obeying the following rules: Only one disk may be moved at a time. Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg. No disk may be placed on top of a smaller disk. ---------Wikipedia Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGS ‘Script version 2007 10 15 21:40:52 Call Main() Sub Main() Dim arrPt1, arrPt2, arrPt3 Dim intn,num Dim strCone, dblMate arrPt1=rhino.GetPoint(“first point”) arrPt2=rhino.GetPoint(“first point”) arrPt3=rhino.GetPoint(“first point”) intn=rhino.GetInteger(“number of floors”) num=30 Call hanoiTower(intn,arrPt1, arrPt2,arrPt3,num) End Sub Function hanoiTower(intn,arrPt1, arrPt2,arrPt3,num) Dim k, strCone,dblMate,dblTran k=10 If intn=1 Then strCone=rhino.AddCone(arrPt1,arrPt2,2) dblMate=rhino.AddMaterialToObject(strCone) Call rhino.MaterialColor(dblMate,RGB(255,255,num)) dblTran=rhino.ObjectMaterialIndex(strCone) Call rhino.MaterialTransparency(dblTran,num/255) 42


43


Else

num=num+30

Call hanoiTower((intn-1),arrPt1, arrPt3,arrPt2,num) arrPt1=PointReplace(arrPt1,0) arrPt2=PointReplace(arrPt2,0) arrPt3=PointReplace(arrPt3,k) ‘----------------------------------------------------

strCone=rhino.AddCone(arrPt1,arrPt2,2) dblMate=rhino.AddMaterialToObject(strCone) Call rhino.MaterialColor(dblMate,RGB(num,num,num)) dblTran=rhino.ObjectMaterialIndex(strCone) Call rhino.MaterialTransparency(dblTran,(1-num/255)) num=num+30 arrPt1=PointReplace(arrPt1,0) arrPt2=PointReplace(arrPt2,0) arrPt3=PointReplace(arrPt3,k) ‘--------------------------------------------------- Call hanoiTower((intn-1),arrPt3, arrPt2,arrPt1,num) arrPt1=PointReplace(arrPt1,0) arrPt2=PointReplace(arrPt2,0) arrPt3=PointReplace(arrPt3,k) End If End Function Function PointReplace(arrPt1,kk) Dim x1,y1,z1 x1=arrPt1(0) y1=arrPt1(1) z1=arrPt1(2)+kk PointReplace=array(x1,y1,z1) End Function

44


45


46


_geometry

47


constant-volumn-measuring cup Measuring cups have kept their shapes as cylinder or cone for ages. But the “2015 measuring cup” manages to change this tradition. By applying “Euclidean Geometry”, we keep the horizontal section area on all the height of the cup the same which means the volumn of the cup is equal to a cylinder cup with the same height and horizontal section area according to “calculous”. Option Explicit ‘Script written by <insert name> ‘Script copyrighted by <insert company name> ‘Script version 2007 11 23 23:42:04 Call Main() Sub Main()

48

Dim arrPtdiv, strLine, i,j, k,stepL, ptOri,m Dim crvDom, arrSec(),n, arrSecOut() k=30 ReDim arrSec(0) ReDim arrSecOut(0) n=1 strLine=rhino.GetObject(“select curve”) crvDom=rhino.CurveDomain(strLine) stepL=(crvDom(1)-crvDom(0))/k ptOri=rhino.EvaluateCurve(strLine,crvDom(0)) arrSec(0)=drawrec(ptOri,0,k) arrSecOut(0)=drawrecOut(ptOri,0,k) For i=crvDom(0)+stepL To crvDom(1)-stepL Step stepL ReDim Preserve arrSec(n) ReDim Preserve arrSecOut(n) ptOri=rhino.EvaluateCurve(strLine,i) arrSec(n)=drawrec(ptOri,n,k) arrSecOut(n)=drawrecOut(ptOri,n,k) Call rhino.AddLoftSrf(array(arrSec(n),arrSec(n-1))) Call rhino.AddLoftSrf(array(arrSecOut(n),arrSecOut(n-1))) n=n+1 Next ReDim Preserve arrSec(n) ReDim Preserve arrSecOut(n) ptOri=rhino.EvaluateCurve(strLine,crvDom(1)) arrSec(n)=drawrec(ptOri,n,k) arrSecOut(n)=drawrecOut(ptOri,n,k) Call rhino.AddLoftSrf(array(arrSec(n),arrSec(n-1))) Call rhino.AddLoftSrf(array(arrSecOut(n),arrSecOut(n-1)))


49


Call rhino.AddLoftSrf(array(arrSec(n),arrSecOut(n))) Call rhino.AddLoftSrf(array(arrSec(0),arrSecOut(0))) End Sub Function drawrec(ptOri,n,k) Dim a,b,pt1(2),area, pt2(2),pt3(2) Dim xlen,ylen,xt,yt,m m=0.5 xlen=200 area=50000 xt=80+(xlen-20)*(n/k) a=10 b=10 pt1(0)=ptOri(0)+m*xt pt1(1)=ptOri(1)-50 pt1(2)=ptOri(2) pt2(0)=ptOri(0)+xt pt2(1)=ptOri(1)+b pt2(2)=ptOri(2) pt3(0)=pt1(0)+a pt3(1)=pt1(1)+(area/xt) pt3(2)=ptOri(2) drawrec=rhino.AddPolyline(array(ptOri,pt1,pt2,pt3,ptOri)) End Function Function drawrecOut(ptOri,n,k) Dim a,b,pt1(2),area, pt2(2),pt3(2),pt0(2) Dim xlen,ylen,xt,yt,m m=0.5 xlen=200 area=60000 xt=80+(xlen-20)*(n/k) a=10 b=10 pt1(0)=ptOri(0)+m*xt pt1(1)=ptOri(1)-80 pt1(2)=ptOri(2) pt2(0)=ptOri(0)+xt+50 pt2(1)=ptOri(1)+b pt2(2)=ptOri(2) pt3(0)=pt1(0)+a pt3(1)=pt1(1)+(area/xt) pt3(2)=ptOri(2) pt0(0)=ptOri(0)-20 pt0(1)=ptOri(1) pt0(2)=ptOri(2) drawrecOut=rhino.AddPolyline(array(pt0,pt1,pt2,pt3,pt0)) End Function

50


51


Cyclic Quadrilateral Recursion Cyclic Quadrilateral is a problem from an outstanding collection by T. Andreescu and R. Gelca (Cut a cyclic quadrilateral into n > 4 cyclic quadrilaterals.) -- Interactive Mathematics Activities Using recursion to develop the cyclic quadrilateral into a fractal.

Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGS ‘Script version 2007 10 3 22:31:44 Call Main() Sub Main() Dim arrpt1, arrpt2, arrpt3, arrpt4 Dim circle,k Dim line1,line2,lineextend Dim arrptmid arrpt1=rhino.GetPoint(“select 1st point”) arrpt2=rhino.GetPoint(“select 2nd point”) arrpt3=rhino.GetPoint(“select 3rd point”) circle=rhino.addcircle3pt(arrpt1,arrpt2,arrpt3) k=0.5 line1=rhino.addline(arrpt1,arrpt3) arrptmid=PtonCrv(arrpt1,arrpt3,k) line2=rhino.AddLine(arrpt2,arrptmid) lineextend=rhino.ExtendCurve(line2,0,1,array(circle)) arrpt4=rhino.CurveEndPoint(lineextend) Call rhino.AddPoint(arrpt4) Call rhino.DeleteObject(line1) Call rhino.DeleteObject(line2) Call rhino.DeleteObject(lineextend) Call RecurseAddcyclicqudrilatral(arrpt1,arrpt2,arrpt3,arrpt4) End Sub Function RecurseAddcyclicqudrilatral(arrpt1,arrpt2,arrpt3,arrpt4) Dim circle1,circle2,circle3,circle0 Dim arrpt,arrptmid,arrpt24 Dim polyline Dim k1,k2 Dim arrptinter1,arrptinter2,arrptinter3,arrptinter4,arrpt21 Dim length0,length1,length2,length3 Dim test test=True 52


D

Q

N

A

P C

R M

B

For any P, points M, N, Q, R always exist on the side lines of ABCD such the quadrilaterals AQPR, DNPQ, CMPN, and BRPM are cyclic.

53


k1=0.5 k2=0.5 arrpt=array(arrpt1,arrpt2,arrpt3,arrpt4) polyline=rhino.AddPolyline(array(arrpt1,arrpt2,arrpt3,arrpt4,arrpt1)) arrpt24=PtonCrv(arrpt1,arrpt4,k2) arrptmid=PtonCrv(arrpt1,arrpt3,k1) arrptinter1=circlecurveintersection(arrpt1,arrpt24,arrptmid,polyline) circle0=rhino.AddCircle3Pt(arrpt1,arrpt24,arrptmid) length0=rhino.CurveLength(circle0) If length0<9 Then Exit Function Call RecurseAddcyclicqudrilatral(arrpt1,arrpt24,arrptmid,arrptinter1) arrptinter2=circlecurveintersection(arrpt2,arrptinter1,arrptmid,polyline) circle1=rhino.AddCircle3Pt(arrpt2,arrptinter1,arrptmid) length1=rhino.CurveLength(circle1) If length1<9 Then Exit Function Call RecurseAddcyclicqudrilatral(arrptinter1,arrpt2,arrptinter2,arrptmid) arrptinter3=circlecurveintersection(arrpt3,arrptinter2,arrptmid,polyline) circle2=rhino.AddCircle3Pt(arrpt3,arrptinter2,arrptmid) length2=rhino.CurveLength(circle2) If length1<9 Then Exit Function Call RecurseAddcyclicqudrilatral(arrptmid,arrptinter2,arrpt3,arrptinter3) arrptinter4=circlecurveintersection(arrpt4,arrptinter3,arrptmid,polyline) circle3=rhino.AddCircle3Pt(arrpt4,arrptinter3,arrptmid) length3=rhino.CurveLength(circle3) If length3<9 Then Exit Function Call RecurseAddcyclicqudrilatral(arrptinter4,arrptmid,arrptinter3,arrpt4) End Function Function PtonCrv(arrpt1,arrpt2,k) Dim Crvdom, strLine,ratio strLine=rhino.addline(arrpt1,arrpt2) Crvdom=rhino.CurveDomain(strLine) ratio=Crvdom(0)+k*(Crvdom(1)-Crvdom(0)) PtonCrv=rhino.EvaluateCurve(strLine,ratio) Call rhino.DeleteObject(strLine) End Function Function circlecurveintersection(arrpt1,arrpt2,arrpt3,line) Dim circle,circlesur,pt,MaCirSur circle=rhino.AddCircle3Pt(arrpt1,arrpt2,arrpt3) pt=rhino.CurveCurveIntersection(circle,line) circlecurveintersection=pt(2,1) End Function

54


Each different color represents a different generation in the recursion process.

55


mesh-cloth-circle Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGs ‘Script version 2007 11 26 23:03:24 Call Main() Sub Main() Const rhObjectMesh = 32 Dim strObject, arrFaces, arrFace(3), i,area,strPl,arrUV,j Dim vecNor,center,ptend,strpp,bisec1,bisec2,plane Dim dis1, dis2, ptshort, ptfar,centerpt,ptTan,edge,ptTan1,radia s,circloth Dim mate, R,G,B,curCir strObject = Rhino.GetObject(“Select mesh”, rhObjectMesh) arrFaces = Rhino.MeshFaces(strObject, False) If IsArray(arrFaces) Then ‘Rhino.EnableRedraw False i = 0 While i <= UBound(arrFaces) arrFace(0) = arrFaces(i) arrFace(1) = arrFaces(i+1) arrFace(2) = arrFaces(i+2) arrFace(3) = arrFaces(i) strpp=rhino.AddPolyline(arrFace) strPl=Rhino.AddPlanarSrf(array(strpp)) call rhino.AddCone(arrFace(0),arrFace(1),1) call rhino.AddCone(arrFace(1),arrFace(2),1) call rhino.AddCone(arrFace(2),arrFace(0),1) center=rhino.SurfaceAreaCentroid(strPl(0)) arrUV=rhino.SurfaceClosestPoint(strPl(0),cen ter(0)) area=rhino.SurfaceArea(strPl(0)) vecNor=rhino.SurfaceNormal(strPl(0),arrUV) vecNor=rhino.VectorScale(vecNor,2000/ area(0)) ptend=rhino.PointAdd(center(0),vecNor) dis1=rhino.Distance(arrFace(0),arrFace(1)) dis2=rhino.Distance(arrFace(0),arrFace(2)) If dis1>dis2 Then ptshort=arrFace(2) ptfar=arrFace(1) Else ptshort=arrFace(1) ptfar=arrFace(2) End If bisec1=bisector(arrFace(0), ptshort, ptfar,vecNor) dis1=rhino.Distance(arrFace(1),arrFace(0)) dis2=rhino.Distance(arrFace(1),arrFace(2)) If dis1>dis2 Then ptshort=arrFace(2) ptfar=arrFace(0) 56


A

P

B

C

57


ptshort=arrFace(0) ptfar=arrFace(2) End If bisec2=bisector(arrFace(1), ptshort, ptfar,vecNor) centerpt=rhino.CurveCurveIntersection(bisec1 ,bisec2) edge=rhino.AddLine(arrFace(1),arrFace(0)) ptTan=rhino.CurveClosestPoint(edge,centerpt( 0,1)) ptTan1=rhino.EvaluateCurve(edge,ptTan) radias=rhino.Distance(centerpt(0,1),ptTan1) plane=rhino.PlaneFromNormal(centerpt(0,1),ve cNor) circloth=rhino.AddCircle(plane,radias) curCir=rhino.AddPlanarSrf(array(circloth)) R=int(rnd()*254+1) G=int(rnd()*254+1) B=int(rnd()*254+1) Call rhino.DeleteObjects(array(bisec1,bisec2 ,strPl(0))) i = i + 3 Wend ‘Rhino.EnableRedraw True End If End Sub Function bisector(ptori, ptshort, ptfar,vecNor) Dim disShort,circle,plane,ptinter,line,lineper,ptmid line=rhino.AddLine(ptori,ptfar) plane=rhino.PlaneFromNormal(ptori,vecNor) disShort=rhino.distance(ptori,ptshort) circle=rhino.AddCircle(plane,disShort) ptinter=rhino.CurveCurveIntersection(circle,line) lineper=rhino.AddLine(ptinter(0,1),ptshort) ptmid=rhino.CurveMidPoint(lineper) bisector=rhino.addline(ptori,ptmid) rhino.DeleteObjects(array(circle,line,lineper)) End Function

58


59


shortpath-bottle Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGs ‘Script version 2008,2,17 19:46:05 ‘Divide & Conquer Call Main() Sub Main() Dim sur, i , j sur=rhino.GetSurfaceObject(“select surface”) Dim surdomU,surdomV,point(),u, v,test(),vec(),hcone(),mate surdomU=rhino.SurfaceDomain(sur(0),0) surdomV=rhino.SurfaceDomain(sur(0),1) Call rhino.EnableRedraw(False) For j=0 To 300 ReDim Preserve point(j) ReDim Preserve test(j) ReDim Preserve vec(j) ReDim Preserve hcone(j) u=surdomU(0)+rnd()*(surdomU(1)-surdomU(0)) v=surdomV(0)+rnd()*(surdomV(1)-surdomV(0)) If u-surdomU(0)<4 Then u=surdomU(0) End If If surdomU(1)-u<4 Then u=surdomU(1) End If test(j)=rhino.EvaluateSurface(sur(0),array(u,v)) If j>0 Then hcone(j)=rhino.ShortPath(sur(0),test(j),test (j-1)) Dim r: r=int(255*(u-surdomU(0))/(surdomU(1)surdomU(0))) Dim g: g=int(100*(v-surdomV(0))/(surdomV(1)surdomV(0))) Dim b: b=int(5*(u-surdomU(0))/(surdomU(1)surdomU(0))) Call rhino.ObjectColor(hcone(j),RGB(r,g,b)) End If Next Call rhino.EnableRedraw(True) End Sub

60


61


62


_application

63


component-on-surface Option Explicit ‘Script written by FUN ‘3d attractor ‘Script version 2007 10 3 22:31:44 Call Main() Sub Main() Dim strSur Dim dblDomu, dblDomv, i,j Dim dblstU, dblstV Dim intDivU, intDivV Dim arrpt1, arrpt2, arrpt3, arrpt4 strSur=rhino.GetObject(“select surface to assemble”) If IsNull(strSur) Then Exit Sub intDivU=rhino.GetInteger(“divition in U”) intDivV=rhino.GetInteger(“divition in V”) If IsNull(intDivU) Then Exit Sub If IsNull(intDivV) Then Exit Sub dblDomu=rhino.SurfaceDomain(strSur,0) dblDomv=rhino.SurfaceDomain(strSur,1) dblstU=(dblDomu(1)-dblDomu(0))/intDivU dblstV=(dblDomv(1)-dblDomv(0))/intDivV For i = dblstU To (dblDomu(1)) Step dblstU For j = dblstV To (dblDomv(1)) Step dblstV arrpt1=rhino.EvaluateSurface(strSur, array(i,j)) arrpt2=rhino.EvaluateSurface(strSur, array(i-dblstU,j)) arrpt3=rhino.EvaluateSurface(strSur, array(i-dblstU,j-dblstV)) arrpt4=rhino.EvaluateSurface(strSur, array(i,j-dblstV)) Call rhino.AddLine(arrpt1,arrpt3) Call rhino.AddLine(arrpt2,arrpt4) Next Next End Sub Function addnurbs(pt1,pt2,pt3,pt4) addnurbs=rhino.AddCurve(array(pt1,pt2,pt3,pt4,pt1)) addnurbs= rhino.Addpolyline(array(pt1,pt2,pt3,pt4,pt1)) End Function Function addtrangle(pt1,pt2,pt3,strSur,U,V) Dim arrHeight, strTr, vecHeight,arrPth, strPoly 64


the length of the component depends on the local curvature of the surface 65


Dim intIndex, nMaterial,sLayer,newMaterial, dblcolor Dim mate, R,G,B arrHeight=rhino.SurfaceCurvature(strSur,array(U,V)) If IsNull(arrHeight) Then Exit Function vecHeight=rhino.VectorScale(arrHeight(1),60*arrHeight(7)) arrPth=rhino.PointAdd(pt1,vecHeight) strPoly=rhino.AddPolyline(array(pt2,pt3,arrPth,pt2)) strTr=rhino.AddPlanarSrf(array(strPoly))

R=int(rnd()*254+1) G=int(rnd()*254+1) B=int(rnd()*254+1) mate=rhino.AddMaterialToObject(strTr(0)) Call rhino.ObjectColor(strTr, RGB(R,25,60)) Call rhino.MaterialColor(mate, RGB(R,25,60)) End Function

66


component assembled on closed double curved surface

67


cone-clolor Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGS ‘Script version 2007 4 30 14:39:18 Call Main() Sub Main() Dim sur, i , j Dim surdomU,surdomV,point(),u, v,test(),vec(),hcone(),mate surdomU=rhino.SurfaceDomain(sur(0),0) surdomV=rhino.SurfaceDomain(sur(0),1) Call rhino.EnableRedraw(False) For j=0 To 500 ReDim Preserve point(j) ReDim Preserve test(j) ReDim Preserve vec(j) ReDim Preserve hcone(j) u=surdomU(0)+rnd()*(surdomU(1)-surdomU(0)) v=surdomV(0)+rnd()*(surdomV(1)-surdomV(0)) test(j)=rhino.EvaluateSurface(sur(0),array(u,v)) point(j)=rhino.SurfaceCurvature(sur(0),array(u,v)) point(j)(1)=rhino.VectorScale(point(j)(1),rnd()) vec(j)=rhino.PointAdd(test(j),point(j)(1)) hcone(j)=rhino.Addcylinder(test(j),vec(j),1) Dim r: r=int(255*(u-surdomU(0))/(surdomU(1)-surdomU(0))) Dim g: g=int(100*(v-surdomV(0))/(surdomV(1)-surdomV(0))) Dim b: b=int(5*(u-surdomU(0))/(surdomU(1)-surdomU(0))) mate=rhino.AddMaterialToObject(hcone(j)) Call rhino.ObjectColor(hcone(j),RGB(r,g,b)) Call rhino.MaterialColor(mate,RGB(r,g,b)) Next Call rhino.EnableRedraw(True) End Sub

68


69


first-class-eye In object-oriented programming, a class is a programming language construct used to group related fields and methods. A class is a cohesive package that consists of a particular kind of metadata. It describes the rules by which objects behave; these objects are referred to as instances of that class. A class has both an interface and a structure. ---Wikipedia Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGS ‘Script version 2007,10,27 19:50:14 Class eye Private Pt1,Pt2,Pt3,Pt4 Private Ptmid14, Ptmid23 Private arrPtlineUp(4), arrPtlineDn(4),arrCrvLine(1),midPtTemp, arrCrvLineDn(1) Private heightfull, height, surUp, surDn Private x,y,z Private Sub class_Initialize() Pt1=array(0,0,0) Pt2=array(0,10,0) Pt3=array(10,10,0) Pt4=array(10,0,0) End Sub Public Function DriPts(arrPt1,arrPt2,arrPt3,arrPt4) Pt1=arrPt1 Pt2=arrPt2 Pt3=arrPt3 Pt4=arrPt4 End Function Public Function line Call rhino.AddLine(Pt1,Pt2) Call rhino.addline(Pt3,Pt4) End Function Public Function OpenRatio(OpenRa) heightfull=rhino.Distance(Pt1,Pt3) height=heightfull*OpenRa End Function Public Function drawSurUp Ptmid14=PtonCur(Pt1,Pt4,0.5) Ptmid23=PtonCur(Pt2,Pt3,0.5) arrPtlineUp(0)= Ptmid14 arrPtlineUp(4)= Ptmid23 70


71


arrPtlineUp(1)= PtonCur(Ptmid14,Ptmid23,0.2) midPtTemp=PtonCur(Ptmid14,Ptmid23,0.5) arrPtlineUp(3)= PtonCur(Ptmid14,Ptmid23,0.8) arrPtlineUp(2)=array(midPtTemp(0),midPtTemp(1),midPtTe mp(2)+height) arrCrvLine(0)=rhino.AddLine(Pt1,Pt2) arrCrvLine(1)=rhino.AddCurve(arrPtlineUp) surUp=rhino.AddLoftSrf(arrCrvLine) End Function Public Function drawSurDn Ptmid14=PtonCur(Pt1,Pt4,0.5) Ptmid23=PtonCur(Pt2,Pt3,0.5) arrPtlineDn(0)= Ptmid14 arrPtlineDn(4)= Ptmid23 arrPtlineDn(1)= PtonCur(Ptmid14,Ptmid23,0.2) midPtTemp=PtonCur(Ptmid14,Ptmid23,0.5) arrPtlineDn(3)= PtonCur(Ptmid14,Ptmid23,0.8) arrPtlineDn(2)=array(midPtTemp(0),midPtTemp(1),midPtTe mp(2)-height) arrCrvLineDn(0)=rhino.AddLine(Pt4,Pt3) arrCrvLineDn(1)=rhino.AddCurve(arrPtlineDn) surDn=rhino.AddLoftSrf(arrCrvLineDn) End Function Public Function PtonCur(Ptstart,Ptend,k) x=Ptstart(0)+(Ptend(0)-Ptstart(0))*k y=Ptstart(1)+(Ptend(1)-Ptstart(1))*k z=Ptstart(2)+(Ptend(2)-Ptstart(2))*k PtonCur=array(x,y,z) End Function End Class Option Explicit Call Main() Sub Main() Dim strSur Dim dblDomu, dblDomv, i,j Dim dblstU, dblstV Dim intDivU, intDivV Dim arrpt1, arrpt2, arrpt3, arrpt4,n,kk(),OpenRa,arrHeight strSur=rhino.GetObject(“select surface to assemble”) If IsNull(strSur) Then Exit Sub intDivU=rhino.GetInteger(“divition in U”) intDivV=rhino.GetInteger(“divition in V”) If IsNull(intDivU) Then Exit Sub If IsNull(intDivV) Then Exit Sub dblDomu=rhino.SurfaceDomain(strSur,0) dblDomv=rhino.SurfaceDomain(strSur,1) 72


dblstU=(dblDomu(1)-dblDomu(0))/intDivU dblstV=(dblDomv(1)-dblDomv(0))/intDivV n=0 ReDim Preserve kk(n) For i = dblstU To (dblDomu(1)) Step dblstU For j = dblstV To (dblDomv(1)) Step dblstV ReDim Preserve kk(n) arrpt1=rhino.EvaluateSurface(strSur, array(i,j)) arrpt2=rhino.EvaluateSurface(strSur, array(i-dblstU,j)) arrpt3=rhino.EvaluateSurface(strSur, array(i-dblstU,j-dblstV)) arrpt4=rhino.EvaluateSurface(strSur, array(i,j-dblstV)) Set kk(n)=New eye kk(n).DriPts arrPt1,arrPt2,arrPt3,arrPt4 OpenRa=j/dblDomv(1) kk(n).OpenRatio(OpenRa) kk(n).drawSurUp kk(n).drawSurDn n=n+1 Next Next End Sub Function addnurbs(pt1,pt2,pt3,pt4) addnurbs=rhino.AddCurve(array(pt1,pt2,pt3,pt4,pt1)) addnurbs= rhino.Addpolyline(array(pt1,pt2,pt3,pt4,pt1)) End Function Function addtrangle(pt1,pt2,pt3,strSur,U,V) Dim arrHeight, strTr, vecHeight,arrPth, strPoly Dim intIndex, nMaterial,sLayer,newMaterial, dblcolor Dim mate, R,G,B arrHeight=rhino.SurfaceCurvature(strSur,array(U,V)) If IsNull(arrHeight) Then Exit Function vecHeight=rhino.VectorScale(arrHeight(1),60*arrHeight(7)) arrPth=rhino.PointAdd(pt1,vecHeight) strPoly=rhino.AddPolyline(array(pt2,pt3,arrPth,pt2)) strTr=rhino.AddPlanarSrf(array(strPoly)) R=int(rnd()*254+1) G=int(rnd()*254+1) B=int(rnd()*254+1) mate=rhino.AddMaterialToObject(strTr(0)) Call rhino.ObjectColor(strTr, RGB(R,25,60)) Call rhino.MaterialColor(mate, RGB(R,25,60)) End Function 73


go-through The script achieves putting certain number of deformed tubes within one volumn but avoid intersections among them. intersection check is a basic technique used to solve many geomotrical problems. By checking and regenerating, the script achieves putting certain number of deformed tubes within one volumn but avoid intersections among them. Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGs ‘Script version Thursday, October 18, 2007 5:28:41 PM Call Main() Sub Main() Dim strSur Dim arrPt1, arrPt2, arrPtmid strSur=rhino.GetObject(“select surface to play”) If IsNull(strSur) Then Exit Sub Dim n,i,j,blnvb Dim dblRmid, dblLineDis Dim arrAxis(),strLoftSur() Dim PtinterCheck,CurInterCheck n=0 Do arrPt1=RndPtonSur(strSur) arrPt2=RndPtonSur(strSur) ReDim Preserve arrAxis(n),strLoftSur(n) arrAxis(n)=rhino.AddLine(arrPt1,arrPt2) dblLineDis=rhino.Distance(arrPt1,arrPt2) dblRmid=dblLineDis/100 If n=0 Then strLoftSur(n)=coolSurface(strSur,arrPt1,arrP t2,dblRmid) End If If n>0 Then strLoftSur(n)=coolSurface(strSur,arrPt1,arrPt2,dblRmid) Do i=0 Do While i<n CurInterCheck=rhino.SurfaceSurfaceIntersection(strLoftSur(n),st rLoftSur(i),,True) If IsNull(CurInterCheck) Then blnvb=2 Call rhino.Print(“true”) Else blnvb=1 Call rhino.DeleteObject(strLoftSur(n)) Call rhino.HideObject(arrAxis(n)) Call rhino.Print(“false”) 74


75


Exit Do End If i=i+1 Loop If blnvb=1 Then arrPt1=RndPtonSur(strSur) arrPt2=RndPtonSur(strSur) dblLineDis=rhino.Distance(arrPt1,arrPt2) dblRmid=dblLineDis/100 strLoftSur(n)=coolSurface(strSur,arrPt1,arrPt2,dblRmid) End If Loop While blnvb=1 End If n=n+1 If n>200 Then Exit Do End If Loop End Sub Function RndPtonSur(strSur) Dim dblDomu, dblDomv Dim RndU, RndV dblDomu=rhino.SurfaceDomain(strSur,0) dblDomv=rhino.SurfaceDomain(strSur,1) RndU=dblDomu(0)+rnd()*(dblDomu(1)-dblDomu(0)) RndV=dblDomv(0)+rnd()*(dblDomv(1)-dblDomv(0)) RndPtonSur=rhino.EvaluateSurface(strSur,array(RndU,RndV)) Call rhino.AddPoint(RndPtonSur) End Function Function coolSurface(strSur,arrPt1,arrPt2,dblRmid) Dim arrCurSec Dim strCir1, strCir2,strCirmid, strCirOnSur1,strCirOnSur2 Dim dblR1,dblR2,dist,vec,arrPtmid, arrloftsur,dblLineDis vec=rhino.VectorCreate(arrPt1,arrPt2) dist=rhino.Distance(arrPt1,arrPt2) dblR1=dist/30+rnd()*dist/30 dblR2=dist/30+rnd()*dist/30 arrPtmid=array((arrPt1(0)+arrPt2(0))/2,(arrPt1(1)+arrPt2(1))/ 2,(arrPt1(2)+arrPt2(2))/2) strcir1=DrawCircle(vec,arrpt1,dblR1) strcir2=DrawCircle(vec,arrpt2,dblR2) strCirmid=DrawCircle(vec,arrPtmid,dblRmid) strCirOnSur1=rhino.PullCurve(strSur,strcir1) strCirOnSur2=rhino.PullCurve(strSur,strcir2) arrCurSec=array(strCirOnSur1(0),strCirmid,strCirOnSur2(0)) If rhino.IsCurveClosed(strCirOnSur1(0)) And rhino.IsCurveClosed (strCirOnSur2(0)) Then arrloftsur=rhino.AddLoftSrf(arrCurSec,,,,1,20) coolSurface=arrloftsur(0) Else arrPt1=RndPtonSur(strSur) arrPt2=RndPtonSur(strSur) dblLineDis=rhino.Distance(arrPt1,arrPt2) 76


dblRmid=dblLineDis/50 coolSurface=coolSurface(strSur,arrPt1,arrPt2,dblRmid) End If Call rhino.HideObjects(array(strcir1,strcir2,strCirmid,strCirOn Sur1(0),strCirOnSur2(0))) End Function Function DrawCircle(vec,Ptstart,Radias) Dim plane plane=rhino.PlaneFromNormal(Ptstart,vec) DrawCircle=rhino.AddCircle(plane,Radias) End Function

77


grid The quadrilatural grid----all patterns which are defined by 4 insertion points could be inserted in this grid such as triangle,quadrilatural, haxegon etc. The density of the grid depends on the side curves in row and column. The distance between the straight line and curve above is the main factor. The example is a haxegonal grid with different number of control lines and different density of elements. Option Explicit ‘Script written by FUN ‘Script version 2007 10 15 22:08:53 ‘control curve requirements: 1. the straight line must be shorter than the cuvers in both direction in order to get valid intersection points ‘2. the straight line in row must be drawed from left to right and in column must be form down to up ‘sequence requirement: control curver selection sequence should follow from up to down in row and frow left to right in column ‘dividenumber constrains: the divide number from row to row has to be the same due to the nurbs propertie. and so to the one for columns. Call Main() Sub Main() Dim arrPtsRow(),arrPtsColumn() Dim intCount Dim CurR,lineR,divideNumberR Dim CurC,lineC,divideNumberC Dim nr,nc Dim AngPolarR1,AngPolarC1 Dim AngPolarR2,AngPolarC2 Dim GridLineR, GridLineC Dim arrResultPts nr=0 nc=0 AngPolarR1=90 AngPolarC1=180 AngPolarR2=0 AngPolarC2=90 Do intCount = rhino.GetInteger(“select set of curves in row type 1,select set of curves in column type 2,finish select type 3”) Select Case intCount Case 1 ReDim Preserve arrPtsRow(nr) curR=rhino.GetObject(“curve which define the density”) lineR=rhino.GetObject(“base line”) 78


3by3 control lines 24by24 grid

3by3 control lines 24by24 grid

79


divideNumberR=rhino. GetInteger(“how many segements u wanna have”,24) arrPtsRow(nr)= CurveToDensity(CurR ,lineR,divideNumberR,AngPolarR1,AngPolarR2) nr=nr+1 Case 2 ReDim Preserve arrPtsColumn(nc) curC=rhino.GetObject(“curve which define the density”) lineC=rhino.GetObject(“base line”) divideNumberC=rhino. GetInteger(“how many segements u wanna have”,12) arrPtsColumn(nc)= CurveToDensity(C urC,lineC,divideNumberC,AngPolarC1,AngPolarC2) nc=nc+1 Case 3 Exit Do End Select Loop If intCount = 0 Then Exit Sub GridLineR=AddGridLine(arrPtsRow,divideNumberR) GridLineC=AddGridLine(arrPtsColumn,divideNumberC) arrResultPts=GridIntersectionPts(GridLineR,GridLineC) Dim arrpt1,arrpt2,arrpt3,arrpt4 Dim u, v Dim ulast : ulast = divideNumberR+1 Dim vlast : vlast = divideNumberC+1 Dim i : i = 0 For u = 0 To ulast - 2 For v = 0 To vlast - 2 i =v+u*vlast arrpt1=arrResultPts(i+1) arrpt2=arrResultPts((u+1)*vlast+v+1) arrpt3=arrResultPts((u+1)*vlast+v) arrpt4=arrResultPts(i) Call addpolygon(arrpt1,arrpt2,arrpt3,arrpt4) Next Next End Sub Function GridIntersectionPts(GridLineR,GridLineC) Dim m,n m=Ubound(GridLineR)+1 n=Ubound(GridLineC)+1 Dim p p=m*n-1 Dim arrResultPts(),arrInterPts() ReDim arrResultPts(p),arrInterPts(p) Dim i,j,k k=0 For i=0 To m-1 For j=0 To n-1 arrInterPts(k)=rhino.CurveCurveIntersection( GridLineR(i),GridLineC(j)) 80


4by3 control lines 24by24 grid

4by3 control lines 24by12 grid

81


arrResultPts(k)=arrInterPts(k)(0,1) k=k+1 Next Next GridIntersectionPts=arrResultPts End Function Function AddGridLine(arrPts,divideNumber) Dim i,j Dim m,n m=Ubound(arrPts) Dim arrptsTem() ReDim arrptsTem(m) Dim polyline() ReDim polyline(dividenumber) For j=0 To dividenumber For i=0 To m arrptsTem(i)=arrPts(i)(j) Next polyline(j)=rhino.AddInterpCurve(arrptsTem) Next AddGridLine=polyline End Function Function CurveToDensity(Cur,line,divideNumber,angPolar1,angPolar2) Dim LineLen Dim arrpts Dim i,j,n,k,r Dim arrPolarLine(),arrIntersectionPts() Dim arrDis(),arrPolarPts() Dim Distance Dim EndPt,arrEndPts Dim arrResultPts() LineLen=rhino.CurveLength(line) arrpts=rhino.DivideCurve(line,divideNumber) EndPt=rhino.CurveEndPoint(line) n=0 For i=0 To Ubound(arrpts)-1 ReDim Preserve arrPolarLine(n),arrIntersectionPts(n),a rrDis(n),arrPolarPts(n) arrPolarPts(n)=rhino.Polar(arrpts(i),angPolar1,300) arrPolarLine(n)=rhino.AddLine(arrpts(i),arrPolarPts(i)) arrIntersectionPts(n)=rhino.CurveCurveIntersection(arrPolarLine (n),Cur) arrDis(n)=rhino.Distance(arrpts(i),arrIntersectionPts(n)(0,1)) ReDim Preserve arrPolarLine(n),arrIntersectionPts(n),arrDis(n), arrPolarPts(n) n=n+1 Next Dim kk kk=0 For r=0 To Ubound(arrDis) kk=kk+arrDis(r) Next distance=kk 82


n=n-1 ReDim Preserve arrResultPts(0) arrResultPts(0)=rhino.CurveStartPoint(line) For j=1 To n ReDim Preserve arrResultPts(j) k=arrDis(j)/distance arrEndPts=rhino.Polar(arrResultPts(j-1),angpolar2,LineLen) arrResultPts(j)= PtonCrv(arrResultPts(j-1),arrEndPts,k) Next ReDim Preserve arrResultPts(n+1) arrResultPts(n+1)=rhino.curveendpoint(line) Call rhino.DeleteObjects(arrPolarLine) Call rhino.AddPoints(arrResultPts) CurveToDensity=arrResultPts End Function Function PtonCrv(arrpt1,arrpt2,k) Dim Crvdom, strLine,ratio strLine=rhino.addline(arrpt1,arrpt2) Crvdom=rhino.CurveDomain(strLine) ratio=Crvdom(0)+k*(Crvdom(1)-Crvdom(0)) PtonCrv=rhino.EvaluateCurve(strLine,ratio) Call rhino.DeleteObject(strLine) End Function Function addpolygon(arrpt1,arrpt2,arrpt3,arrpt4) Dim polypt121,polypt122 Dim polypt431,polypt432 Dim polypt411,polypt412 Dim polypt321,polypt322 Dim k1,k2,k3,k4,k5,k6 k1=0.5 k2=0.5 k3=0.05 k4=0.05 k5=0.3 k6=0.3 polypt411=PtonCrv(arrpt1,arrpt4,k1) polypt321=PtonCrv(arrpt2,arrpt3,k2) polypt431=PtonCrv(arrpt4,arrpt3,k5) polypt432=PtonCrv(arrpt3,arrpt4,k6) polypt121=PtonCrv(arrpt1,arrpt2,k5) polypt122=PtonCrv(arrpt2,arrpt1,k6) polypt412=PtonCrv(polypt411,polypt321,k3) polypt322=PtonCrv(polypt321,polypt411,k4) Call rhino.AddLine(polypt411,polypt412) Call rhino.AddLine(polypt322,polypt321) Call rhino.AddLine(polypt431,polypt432) Call rhino.AddLine(polypt121,polypt122) Call rhino.AddLine(polypt412,polypt431) Call rhino.AddLine(polypt412,polypt121) Call rhino.AddLine(polypt432,polypt322) Call rhino.AddLine(polypt322,polypt122) End Function 83


identical-windows Putting identical elements on a curved surface is a popular problem in architecture world. By drawing circles one after another on the contours of the surface, the script puts identical windows on a curved wall

Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGs ‘Script version Tuesday, October 23, 2007 7:44:12 PM Call Main() Sub Main() Dim strCur,dblWid,PtStart,PtEnd Dim arrPts(), i, strPlane,n,m Dim arrCir() Dim Curdom, PtInter,CurPtdom Dim DisTest,height strCur=rhino.GetObject(“please select the curve to build windows”) dblWid=rhino.GetReal(“width of the window”) height=rhino.GetReal(“height of the window”) PtStart=rhino.CurveStartPoint(strCur) Curdom=rhino.CurveDomain(strCur) strPlane=rhino.CurveFrame(strCur,Curdom(0)) ReDim arrCir(0) ReDim arrPts(0) arrPts(0)=PtStart arrCir(0)=rhino.AddCircle(strPlane,dblWid) ‘-------------first circle drew--------------- ReDim Preserve arrPts(1),arrCir(1) PtInter=rhino.CurveCurveIntersection(strCur,arrCir(0)) arrPts(1)=PtInter(0,1) CurPtdom=rhino.CurveClosestPoint(strCur,arrPts(1)) strPlane=rhino.CurveFrame(strCur,CurPtdom) arrCir(1)=rhino.AddCircle(strPlane,dblWid) ‘------------second circle drew-------------- n=2 Do ReDim Preserve arrPts(n) ReDim Preserve arrCir(n) PtInter=rhino.CurveCurveIntersection(strCur,arrCir(n1)) arrPts(n)=PtInter(1,1) CurPtdom=rhino.CurveClosestPoint(strCur,arrPts(n)) strPlane=rhino.CurveFrame(strCur,CurPtdom) arrCir(n)=rhino.AddCircle(strPlane,dblWid) 84


85


arrCir(n)=rhino.AddCircle(strPlane,dblWid) PtEnd=rhino.CurveEndPoint(strCur) DisTest=rhino.Distance(arrPts(n),PtEnd) If DisTest<dblWid Then Exit Do Else n=n+1 End If Loop For i=0 To Ubound(arrPts)-1 Call drawWindows(arrPts(i),arrPts(i+1),height) Next End Sub Function drawWindows(Pt1,Pt2,height) Dim CurLine,strWin, PtTop CurLine=rhino.AddLine(Pt1,Pt2) PtTop=array(Pt1(0),Pt1(1),Pt1(2)+height) strWin=rhino.ExtrudeCurveStraight(CurLine,Pt1,PtTop) dblMate=rhino.AddMaterialToObject(strWin) Call rhino.MaterialColor(dblMate,RGB(2,34,65)) End Function

86


87


random The script deduct one volumn from another first(in this case, a donut from a cube) and putting certain number of circles in the remained space . The orientation of the circles are adjusted toward a prechosen point within the volumn.

Option Explicit ‘Script written by FUN ‘Script version Friday, October 12, 2007 5:18:33 PM Call Main() Sub Main() Dim ranx, rany, ranz Dim arrPtTest,arrPtRan, strLine ,strSur Dim arrInPt, blnin, blnon Dim i,j Dim arrBbox, vec, plane, circle, surcircle strSur=rhino.GetObject(“select surface”) arrPtTest=rhino.GetPoint(“select the test point”) arrBbox=rhino.BoundingBox(strSur) For i=1 To 2000 ranx=randomUpDown(arrBbox(0)(0),arrBbox(1)(0)) rany=randomUpDown(arrBbox(0)(1),arrBbox(3)(1)) ranz=randomUpDown(arrBbox(0)(2),arrBbox(4)(2)) arrPtRan=array(ranx,rany,ranz) strLine=rhino.AddLine(arrPtRan,arrPtTest) arrInPt=rhino.CurveSurfaceIntersection(strLine,strSur) vec=rhino.VectorCreate(arrPtRan, arrPtTest) plane=rhino.PlaneFromNormal(arrPtRan,vec) circle=rhino.AddCircle(plane,0.2) Call rhino.AddPlanarSrf(array(circle)) If Not IsNull(arrInPt) Then plane=rhino.PlaneFromNormal(arrInPt(0,1),vec) circle=rhino.AddCircle(plane,0.2) Call rhino.AddPlanarSrf(array(circle)) If Ubound(arrInPt)>=1 Then plane=rhino.PlaneFromNormal(arrInPt(1,1),vec) circle=rhino.AddCircle(plane,0.2) call rhino.AddPlanarSrf(array(circle)) End If End If Call rhino.DeleteObject(strLine) Next End Sub Function randomUpDown(num1,num2) randomUpDown=num1+rnd()*(num2-num1) End Function 88


89


stairs To populate stairs on a inclined surface is a common task for arhictects.This application is trying to populate stairs and at the same time keep the part of the stairs the same continuation with the surface. Option Explicit ‘Script written by FUN ‘Script copyrighted by BUGS ‘Script version 2007 12 1 15:29:21 Call Main() Sub Main() Dim strsur,arrstartpt,arrendpt,dblinterval Dim disstep,dis,n Dim arrcontour,arrcontour2 Dim arrbox strsur=rhino.GetObject(“Select a surface”) arrbox=rhino.BoundingBox(strsur) arrstartpt=arrbox(2) arrendpt=arrbox(6) disstep=rhino.GetReal(“type distance between 2 steps”,0.15) dis=rhino.Distance(arrstartpt,arrendpt) n=int(dis/disstep) dblinterval=dis/n arrcontour=rhino.AddSrfContourCrvs(strsur,arrstartpt,arrendpt,d blinterval) arrcontour2=rhino.CopyObjects(arrcontour,array(0,0,0),array(0,0 ,dblinterval*-1)) Dim Crvtop,CrvtopCopy,CrvEdge Dim i Dim arrMidPt() Dim max max=0 CrvEdge=rhino.DuplicateEdgeCurves(strsur) For i=0 To ubound(CrvEdge) ReDim Preserve arrMidPt(i) arrMidPt(i)=rhino.CurveMidPoint(CrvEdge(i)) If arrMidpt(i)(2)>max Then max=arrMidpt(i)(2) crvtop=CrvEdge(i) End If Next CrvtopCopy=rhino.CopyObjects(array(Crvtop),array(0,0,0),array(0 ,0,dblinterval*-1)) Dim t Dim crv Dim m:m=ubound(arrcontour) For t=0 To m-1 crv=blendCrv(arrcontour(t),arrcontour2(t+1)) Call rhino.AddLoftSrf(array(crv,arrcontour(t+1))) Call rhino.addloftsrf(array(crv,arrcontour(t))) 90


91


Next Dim Crvlast Dim blnCompare Crvlast=blendCrv(arrcontour(m),Crvtopcopy(0)) blnCompare=rhino.CurveDirectionsMatch(Crvlast,Crvtop) If Not blnCompare=True Then Call rhino.ReverseCurve(Crvtop) End If Call rhino.AddLoftSrf(array(Crvlast,Crvtop)) Call rhino.AddLoftSrf(array(Crvlast,arrcontour(m))) Call rhino.HideObject(strsur) End Sub Function blendCrv(contourCrv,contourCrv2) Dim k1,k2,k3,k4,k5,k6,k7,k8 k1=0.05 k2=0.1 k3=0.2 k4=0.4 k5=1-k4 k6=1-k3 k7=1-k2 k8=1-k1 Dim pt1,pt2,pt3,pt33,pt333,pt4,pt44,pt444,pt5,pt55,pt555,pt6,pt 66,pt666,pt7,pt8 Dim startpt,endpt startpt=rhino.CurveStartPoint(contourCrv) endpt=rhino.CurveEndPoint(contourCrv) pt1=ptoncrv(contourCrv,k1) pt2=ptoncrv(contourCrv,k2) pt3=ptoncrv(contourCrv,k3) pt33=rhino.CurveClosestPoint(contourCrv2,pt3) pt333=rhino.EvaluateCurve(contourCrv2,pt33) pt4=ptoncrv(contourCrv,k4) pt44=rhino.CurveClosestPoint(contourCrv2,pt4) pt444=rhino.EvaluateCurve(contourCrv2,pt44) pt5=ptoncrv(contourCrv,k5) pt55=rhino.CurveClosestPoint(contourCrv2,pt5) pt555=rhino.EvaluateCurve(contourCrv2,pt55) pt6=ptoncrv(contourCrv,k6) pt66=rhino.CurveClosestPoint(contourCrv2,pt6) pt666=rhino.EvaluateCurve(contourCrv2,pt66) pt7=ptoncrv(contourCrv,k7) pt8=ptoncrv(contourCrv,k8) blendCrv=rhino.AddCurve(array(startpt,pt1,pt2,pt333,pt444,pt555 ,pt666,pt7,pt8,endpt)) End Function Function PtonCrv(strCrv,k) Dim Crvdom,ratio Crvdom=rhino.CurveDomain(strCrv) ratio=Crvdom(0)+k*(Crvdom(1)-Crvdom(0)) PtonCrv=rhino.EvaluateCurve(strCrv,ratio) End Function 92


93


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.