Langton's ant: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "{{task|Celluar_Automaton}} Langton's ant models an ant sitting on a plane of cells facing in one of four directions. Each cell can either be black or white. ...")
 
(removed task)
Line 1: Line 1:
{{task|Celluar_Automaton}}
[[wp:Langton's ant|Langton's ant]] models an ant sitting on a plane of cells facing in one of four directions. Each cell can either be black or white. The ant moves according to the color of the cell it is current sitting in based on the following rules. If the cell is black it turns left, if it is white it turns right and then moves on cell forward. The color of the cell it was in is switched black/white.
[[wp:Langton's ant|Langton's ant]] models an ant sitting on a plane of cells facing in one of four directions. Each cell can either be black or white. The ant moves according to the color of the cell it is current sitting in based on the following rules. If the cell is black it turns left, if it is white it turns right and then moves on cell forward. The color of the cell it was in is switched black/white.
This rather simple ruleset leads some movement appearing like random (or maybe like some kind of edge detecion being run on random images) and after about 10000 a cyle appears that makes the ant move in a diagonal movements with a corridor about 10 pixels wide, which essentially means that the ant will move out of the screen.
This rather simple ruleset leads some movement appearing like random (or maybe like some kind of edge detecion being run on random images) and after about 10000 a cyle appears that makes the ant move in a diagonal movements with a corridor about 10 pixels wide, which essentially means that the ant will move out of the screen.

Revision as of 13:51, 30 October 2011

Langton's ant models an ant sitting on a plane of cells facing in one of four directions. Each cell can either be black or white. The ant moves according to the color of the cell it is current sitting in based on the following rules. If the cell is black it turns left, if it is white it turns right and then moves on cell forward. The color of the cell it was in is switched black/white. This rather simple ruleset leads some movement appearing like random (or maybe like some kind of edge detecion being run on random images) and after about 10000 a cyle appears that makes the ant move in a diagonal movements with a corridor about 10 pixels wide, which essentially means that the ant will move out of the screen.

The problem has recieved some analysis, for more details, please take a look at the Wikipedia article.

Processing

Processing implementation, this uses two notable features of Processing, first of all, the animation is calculated with the draw() loop, second the drawing on the screen is also used to represent the actual state. <lang processing>/*

* we use the following conventions:
* directions 0: up, 1: right, 2: down: 3: left
*
* pixel white: true, black: false
*
* turn right: true, left: false
*
*/

// number of iteration steps per frame // set this to 1 to see a slow animation of each // step or to 10 or 100 for a faster animation

final int STEP=100;

int x; int y; int direction;

void setup() {

 // 100x100 is large enough to show the
 // corridor after about 10000 cycles
 size(100, 100, P2D);
 background(#ffffff);
 x=width/2;
 y=height/2;
 direction=0;

}

int count=0;

void draw() {

 for(int i=0;i<STEP;i++) {
   count++;
   boolean pix=get(x,y)!=-1;
   setBool(x,y,pix);
 
   turn(pix);
   move();
 
   if(x<0||y<0||x>=width||y>=height) {
     println("finished");
     noLoop();
     break;
   }
 }
 if(count%1000==0) {
   println("iteration "+count);
 }

}

void move() {

 switch(direction) {
   case 0:
     y--;
     break;
   case 1:
     x++;
     break;
   case 2:
     y++;
     break;
   case 3:
     x--;
     break;
 }

}

void turn(boolean rightleft) {

 direction+=rightleft?1:-1;
 if(direction==-1) direction=3;
 if(direction==4) direction=0;

}

void setBool(int x, int y, boolean white) {

 set(x,y,white?#ffffff:#000000);

}</lang>