- """
- 制作人:盖房子的猫熊
- 模拟运动。
- """
- import rhinoscriptsyntax as rs
- import random as r
- import math
- from scriptcontext import sticky as st
- class Mover(object):
- """定义一个Mover类,并初始化,
- 定义位置以及运动方向。
- """
- def __init__(self):
- self.pos=[0,0,0]
- self.vec=[0,0,0]
- self.acc=[0,0,0]
- self.c=[]
- #更新物体的运动状态
- def update(self):
- self.vec=rs.VectorAdd(self.vec,self.acc)
- self.pos=rs.VectorAdd(self.pos,self.vec)
- self.c.append(self.pos)
-
- #定义一个恒定力
- def force(self,force,mass):
- scale=1/mass
- self.acc=rs.VectorScale(force,scale)
- #定义一个变化力
- def wind(self):
- a=r.randint(-5,5)
- b=r.randint(-5,5)
- c=r.randint(-5,5)
- vec=rs.VectorCreate([0,0,0],[a/4,b/4,c/4])
- self.vec=rs.VectorAdd(self.vec,vec)
- def limited(self,vectorlength):
- if rs.VectorLength(self.vec)>10:
- self.vec=rs.VectorUnitize(self.vec)
- self.vec=rs.VectorScale(self.vec,10)
- self.acc=[0,0,0]
- def display(self):
- self.wind()
- self.force(force,10)
- self.limited(20)
- self.update()
- class OtMover(Mover):
- """
- 定义一个追随的类。
- """
- def __init__(self):
- self.pos=[0,0,0]
- self.vec=[0,0,0]
- self.acc=[0,0,0]
- self.c=[]
- self.sacle=0
- #吸引力
- def attraction(self,sca,otpos):
- distance=rs.Distance(self.pos,otpos)
- self.sacle+=0.01
- sacle=(math.sin(self.sacle)+1)*sca
- vector=(rs.VectorCreate(otpos,self.pos))*sca
- self.vec=rs.VectorAdd(self.vec,vector)
-
-
- class MiMover(Mover):
- """
- 定义一个运动的类,在一定范围内运动
- """
- def __init__(self):
- self.pos=[0,0,0]
- self.vec=[0,0,0]
- self.acc=[0,0,0]
- self.c=[]
- def checkEdges(self):
- if self.pos[0]<0 or self.pos[0]>100:
- self.vec[0]*=-1
- if self.pos[1]<0 or self.pos[1]>100:
- self.vec[1]*=-1
- if self.pos[2]<0 or self.pos[2]>100:
- self.vec[2]*=-1
- def display(self):
- self.wind()
- self.force(force,10)
- self.limited(20)
- self.checkEdges()
- self.update()
- c=[]
- d=[]
- if "move" not in st:
- st["move"]=MiMover()
- r.seed(seed)
- if Toogle:
- st["move"].display()
- point=st["move"].pos
- vector=st["move"].vec
- else:
- del st["move"]
- for item in range(number):
- name='otmove%d'%item
-
- if name not in st:
- st[name]=OtMover()
- r.seed(seed)
- if Toogle:
- st[name].attraction(sca,st["move"].pos)
- st[name].display()
- a=st[name].pos
- b=st[name].vec
- c.append(a)
- d.append(b)
- else:
- del st[name]
复制代码 |