Tutorials | (back to the list of tutorials) |
import processing.opengl.*; import igeo.*; void setup(){ size(480, 360, IG.GL); MyAgent agent = new MyAgent(); } class MyAgent extends IAgent{ }
import processing.opengl.*; import igeo.*; void setup(){ size(480, 360, IG.GL); MyAgent agent = new MyAgent(); } class MyAgent extends IAgent{ void update(){ // 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.
import processing.opengl.*; import igeo.*; void setup(){ size(480, 360, IG.GL); IG.duration(10); MyAgent agent = new MyAgent(new IVec(0,0,0)); } class MyAgent extends IAgent{ IVec pos; MyAgent(IVec p){ pos = p; } void update(){ new IBox(pos, 10, 10, 10); new MyAgent(pos.dup().add(10,10,10)); 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.
import processing.opengl.*; import igeo.*; void setup() { size(480, 360, IG.GL); IG.duration(400); int num = 10; for (int i=0; i < num; i++) { new MyAgent(IRandom.pt(30,30,0), 5).clr(0.5); } IG.transparent(); //transparent graphic mode } class MyAgent extends IAgent { IVec pos; double size; MyAgent(IVec pt, double sz) { pos = pt; size = sz; } public void update() { new IBox(pos, size, size, size).clr(this.clr()); int childNum = IRandom.getInt(0, 2); for (int i=0; i < childNum; i++) { double nextSize = IRandom.get(1, 4); IVec nextPos = pos.dup(); //random direction double dir=IRandom.getInt(0, 5); if (dir==0) nextPos.add(size, 0, 0); //right else if (dir==1) nextPos.add(-nextSize, 0, 0); //left else if (dir==2) nextPos.add(0, size, 0); //up else if (dir==3) nextPos.add(0, -nextSize, 0); //down else if (dir==4) nextPos.add(0, 0, size); //above // slightly chaning the color int r = clr().getRed() + IRandom.getInt(-10, 10); int g = clr().getGreen() + IRandom.getInt(-10, 10); int b = clr().getBlue() + IRandom.getInt(-10, 10); new MyAgent(nextPos, nextSize).clr(r, g, b); } 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().
import processing.opengl.*; import igeo.*; void setup() { size(480, 360, IG.GL); IG.duration(300); int num = 10; for (int i=0; i < num; i++) { new MyLineAgent(IRandom.pt(30,30,0), new IVec(10,0,0)).clr(0); } } static class MyLineAgent extends IAgent { IVec pos; IVec dir; MyLineAgent(IVec pt, IVec dir) { pos = pt; this.dir = dir; } public void update() { IVec pos2 = pos.dup().add(dir); new ICurve(pos, pos2).clr(clr()); int childNum = IRandom.getInt(0, 2); for (int i=0; i < childNum; i++) { IVec dir2 = dir.dup(); double angle = IRandom.get(-PI/4, PI/4); dir2.rot(new IVec(0,0,1), angle); int r = clr().getRed() + IRandom.getInt(-10, 10); int g = clr().getGreen() + IRandom.getInt(-10, 10); int b = clr().getBlue() + IRandom.getInt(-10, 10); new MyLineAgent(pos2, dir2).clr(r,g,b); } 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.