Python Tutorials | (back to the list of tutorials) |
add_library('igeo') def setup() : size(480, 360, IG.GL) agent = MyAgent() class MyAgent(IAgent) : # ...
add_library('igeo') def setup() : size(480, 360, IG.GL) agent = MyAgent() class MyAgent(IAgent) : def update(self) : # definition of update behavior
Now here is an example to define the behavior of the agent with update() method. Inside the setup() method, IG.duration(int) method defines the number of updating cycle inside iGeo. If you put -1, it makes the cycle endless but please use it carefully because the code could run until it eats up all available memory inside Processing. Not setting the duration with IG.duration(int) also makes the cycle endless.
add_library('igeo') def setup() : size(480, 360, IG.GL) IG.duration(10) agent = MyAgent(IVec(0,0,0)) class MyAgent(IAgent) : def __init__(self, p) : self.pos = p def update(self) : IBox(self.pos, 10, 10, 10) MyAgent(self.pos.dup().add(10,10,10)) self.del()
In the code above, MyAgent class has a field of "IVec pos;" to store a location of the agent. Then the constructor is defined to initialize the filed pos.
Inside the update() method, the code creates an instance of IBox at the location of pos and another instance of MyAgent at shifted location, and then it deletes itself. In the updating cycle of iGeo, in every cycle, a new MyAgent instance is created at the shifted location and the instance is deleted in the next cycle after creating a new one. This cycle is repeated until the whole updating cycle ends with the duration specified by IG.duration(int) method.
add_library('igeo') def setup() : size(480, 360, IG.GL) IG.duration(400) num = 10 for i in range(num) : MyAgent(IRand.pt(30,30,0), 5).clr(0.5) IG.transparent() #transparent graphic mode class MyAgent(IAgent) : def __init__(self, pt, sz) : self.pos = pt self.size = sz def update(self) : IBox(self.pos, self.size, self.size, self.size).clr(self.clr()) childNum = IRand.getInt(0, 2) for i in range(childNum) : nextSize = IRand.get(1, 4) nextPos = self.pos.dup() #random direction dir=IRand.getInt(0, 5) if dir==0 : nextPos.add(self.size, 0, 0) #right elif dir==1 : nextPos.add(-nextSize, 0, 0) #left elif dir==2 : nextPos.add(0, self.size, 0) #up elif dir==3 : nextPos.add(0, -nextSize, 0) #down elif dir==4 : nextPos.add(0, 0, self.size) #above # slightly chaning the color r = self.clr().getRed() + IRand.getInt(-10, 10) g = self.clr().getGreen() + IRand.getInt(-10, 10) b = self.clr().getBlue() + IRand.getInt(-10, 10) MyAgent(nextPos, nextSize).clr(r, g, b) self.del()
One thing to be noted is that in the setup() method, there is a method of "IG.transparent()", which set the iGeo's shading mode to be "transparent shade without edges" mode. This is because putting wireframe is relatively heavy operation and skipped to show the behavior of the agents faster. Other methods to set shading modes are, IG.wireframe(), IG.fill(), IG.wireframeFill() (default), and IG.wireframeTransparent().
add_library('igeo') def setup() : size(480, 360, IG.GL) IG.duration(300) num = 10 for i in range(num) : MyLineAgent(IRand.pt(30,30,0), IVec(10,0,0)).clr(0) class MyLineAgent(IAgent) : def __init__(self, pt, dir) : self.pos = pt self.dir = dir def update(self) : pos2 = self.pos.dup().add(self.dir) ICurve(self.pos, pos2).clr(self.clr()) childNum = IRand.getInt(0, 2) for i in range(childNum) : dir2 = self.dir.dup() angle = IRand.get(-PI/4, PI/4) dir2.rot(IVec(0,0,1), angle) r = self.clr().getRed() + IRand.getInt(-10, 10) g = self.clr().getGreen() + IRand.getInt(-10, 10) b = self.clr().getBlue() + IRand.getInt(-10, 10) MyLineAgent(pos2, dir2).clr(r,g,b) self.del()
Instead, if you type a keyboard shortcut "control + s" after the updating cycle started, a dialog window shows up and you can save geometries agents generate.