Early stage Later stage
Ant_1.png Ant_2.png
//Bjork B. Christensen
// A script that simulates how ants scavenge for food each morning. Full description in the bottom.
// Key words: Array, Classes, Array Classes, methods, pixel recognition, random, key Input
 
//=================== Declare Global Variables========================================
// Integer arrays
int[] Screen = {
  800,600}; //define screen dimensions. Kind of stupid to use and array, bit it keeps the two values together
int[] NestCoord = new int[2]; //Declare array for Nest Coordinates
// Float arrays with two array string in each
 
// integers (whole numbers)
int NumFood = 70; //number of food pieces on the screen
int NumAnt = 200; //Number of ants on the screen
int DistAnt = (Screen[0] + Screen[1])/5; //How far can the ants go from the nest
 
 
float[][] FoodPos; //Declare array with two strings for Food Coordinates
float[][] AntPos; //Declare array with two strings for Ant Coordinates
 
// Declaration of classes found below
clsNest nest;
clsAnt ant;
clsFood food;
 
//=====================Setup====================================================================
void setup()  {
  size(Screen[0], Screen[1]);
  frameRate(30); //Adjust the speed of the program
 
  NestCoord[0] = width/2; //Set NestCoord to the middle of the screen
  NestCoord[1] = height/2;
 
  nest = new clsNest(); //Initialize the clsNest class, which makes it possible
  //to actually start making copies/instances of it
  //From now on use address “Nest” to use the “clsNest” class
 
  food = new clsFood(); //Initialize the clsFood class
  food.Generate(NumFood); //Creates as many “food” copies as the value NumFood is.
 
  ant = new clsAnt(); //Use the class “clsAnt” by using “ant”
  ant.Generate(nest.X(), nest.Y(), DistAnt, NumAnt); //Call the method “Generate” in the clsAnt class.
  // the method requires 4 input. The last (NumAnt) is used to define how many ants will be created
}
//==================================Draw====================================================================
void draw(){
  background(255); //Set the background to white
  nest.Display(); // call the Display method within the clsNest class
 
  ant.Move(); //Call the Move method within the clsAnt class
  ant.Display();//Call the Display method within the clsAnt class
 
 
  ant.FoodTjeck(); //call Foodtjeck..
  food.Display2(); //call Display2..
 
}
//=====================KeyPressed============
void keyPressed() { //will save image when “s” is pressed
  if (key == 115) //the letter "s" has the value 115
  {
    PImage b = get();
    //b.format = RGB; // commented out..
    b.save("Test.png");
  }
}
//=================== The Ant Class===========================================================
class clsAnt {
// Data fields
  float[] CoordX; // The X coordinates for each ant generated
  float[] CoordY; // The Y coordinates for each ant generated
  float CoordXStart; //starting coordinates for each ant. Should be alligened the position of the nest
  float CoordYStart;
  float[] Ang; // Will constraint the angle of the ants next move I recording to its current position
  float[] Dist; // how far can each ant go from the nest. Needs to be array because individual for each ant
  float inc = 2; //Effects the distance between each ant step => their speed
  int num;
  long Rounds = 1; // a value of the type “long” to keep track of how many steps the ants have taken.
 
//----------The Generate Method----------------
  void Generate(float X,float Y,int Dista, int Numb) { //This creates the desired amount of ants
    num = Numb; //the number of ants
    AntPos = new float[width][height]; //Creates an array with the same amount of entries as there is pixels.
 
    CoordX = new float[num]; //Used to translate the Generate-method inputs to each specific ant
    CoordY = new float[num]; //Used to translate the Generate-method inputs to each specific ant
    Ang = new float[num]; //Used to translate the Generate-method inputs to each specific ant
    Dist = new float[num]; //Used to translate the Generate-method inputs to each specific ant
 
    for(int h = 0; h < num; h++) {
      CoordX[h] = X;
      CoordY[h] = Y;
      CoordXStart = X;
      CoordYStart = Y;
      Ang[h] = random(1,360); //Define the direction the ants is initially spawn out from the nest
      Dist[h] = random(Dista - Dista/3, Dista + Dista/3); // could be done simpler. Set the max distance for each ants
    }
 
  }
//----------The Move Method----------------
  void Move() {
    Rounds ++;
    if (1 == 2) {  //the first if statement does nothing, it is the “else” statement below who does the trick
      for(int p = 0; p < num; p++) {
      }
    }
    else {
      for(int g = 0; g < num; g++) { //go through the stack of ant and change the positions according to below calculations.
        CoordX[g] = CoordX[g] + cos(radians(random(Ang[g]-90,Ang[g]+90)))*inc;
        CoordY[g] = CoordY[g] + sin(radians(random(Ang[g]-90,Ang[g]+90)))*inc;
 
        if(CoordX[g] >= CoordXStart + Dist[g] || CoordX[g] <= CoordXStart - Dist[g] ||
        CoordY[g] >= CoordYStart + Dist[g]|| CoordY[g] <= CoordYStart - Dist[g]) {
          Ang[g] = random(1,360);  // if the limit of the ant is reached,
                   //move the ant closer to the nest and give it a new direction
        }
        if(Rounds >= 200 && CoordX[g] >= CoordXStart - 20 && CoordX[g] <= CoordXStart + 20 &&
    CoordY[g] >= CoordYStart - 20 && CoordY[g] <= CoordYStart + 20) { //after 200 rounds, if the ant is walked within
            // 20 pixel of the nest, just keep it the nest center (aka the ant is crawled inside
          CoordX[g] = CoordXStart;
          CoordY[g] = CoordYStart;
        }
 
        if (CoordX[g] <= 0) { //keep the ant within the screen boundaries’
          CoordX[g] = CoordX[g] + 2;
          Ang[g] = random(1,360);
        }
        if (CoordY[g] <= 0) { //keep the ant within the screen boundaries’
          CoordY[g] = CoordY[g] + 2;
          Ang[g] = random(1,360);
        }
        if (CoordX[g] >= width) { //keep the ant within the screen boundaries’
          CoordX[g] = CoordX[g] - 2;
          Ang[g] = random(1,360);
        }
        if (CoordY[g] >= height) { //keep the ant within the screen boundaries’
          CoordX[g] = CoordX[g] - 2;
          Ang[g] = random(1,360);
        }
        if (CoordX[g] > 0 && CoordY[g] > 0 && CoordY[g] < height && CoordX[g] < width) {
    //update the ants position so for the FoodTjeck method.
          AntPos[int(CoordX[g])][int(CoordY[g])] = 1;
        }
      }
    }
 
  }
//----------The Food Tjeck Method----------------
  void FoodTjeck() { //tjeck whether a piece of food or an ant is at the same pixel (position on the screen).
    for(int xa = 1; xa < width -1; xa++) {
      for(int ya = 1; ya < height -1; ya++) {
        if((FoodPos[xa][ya] == 1 && AntPos[xa][ya] == 1) || (FoodPos[xa][ya] == 1 && AntPos[xa+1][ya+1] == 1) ||
     (FoodPos[xa][ya] == 1 && AntPos[xa-1][ya-1] == 1) || (FoodPos[xa][ya] == 1 && AntPos[xa+1][ya-1] == 1)||
     (FoodPos[xa][ya] == 1 && AntPos[xa-1][ya+1] == 1)) { //broaden this..
          noStroke();
          fill(3,107,4);
          ellipse(xa,ya, 20, 20);
        }
      }
    }
  }
//----------The FoodLines Method----------------
  void FoodLines() {
    for(int xa = 1; xa < width -1; xa++) {
      for(int ya = 1; ya < height -1; ya++) {
        if((FoodPos[xa][ya] == 1 && AntPos[xa][ya] == 1) || (FoodPos[xa][ya] == 1 && AntPos[xa+1][ya+1] == 1) ||
    (FoodPos[xa][ya] == 1 && AntPos[xa-1][ya-1] == 1) || (FoodPos[xa][ya] == 1 && AntPos[xa+1][ya-1] == 1)||
    (FoodPos[xa][ya] == 1 && AntPos[xa-1][ya+1] == 1)) { //broaden this..
          noStroke();
          fill(3,107,4);
          ellipse(xa,ya, 20, 20);
        }
      }
    }
  }
//----------The Display Method----------------
  void Display() {
    stroke(10);
    for(int f = 0; f < num; f++) {
      ellipse(CoordX[f] + 1,CoordY[f] + 1, 1, 1);
    }
  }
}
//=================== The Food Class===========================================================
class clsFood {
  // Data fields
  float[] foodX;
  float[] foodY;
  int FoodNum;
 
  //----------The Generate Method----------------
void Generate(int N) {
    FoodNum = N;
    foodX = new float[FoodNum];
    foodY = new float[FoodNum];
    FoodPos = new float[width][height];
 
    for(int i = 0; i < FoodNum ; i++) {
      //foodX[i] = int(random(1,width-1));
      //foodY[i] = int(random(1,height-1));
      FoodPos[int(random(1,width-1))][int(random(1,height-1))] = 1;
    }
  }
 
 
 //----------The “X” Method----------------
 float X(int N) {
    return foodX[N];
  }
//----------The “Y” Method----------------
  float Y(int N) {
    return foodY[N];
  }
//----------The Display Method----------------
  void Display2() {
    for(int x = 1; x < width -1; x++) {
      for(int y = 1; y < height -1; y++) {
        if(FoodPos[x][y] == 1) {
          noStroke();
          fill(34,95,33);
          ellipse(x,y,6,6);
        }
      }
    }
  }
}
//=================== The Nest Class===========================================================
class clsNest {
 
  // Data fields
//----------The “X” Method----------------
int X() {
    return NestCoord[0];
  }
//----------The “Y” Method----------------
  int Y() {
    return NestCoord[1];
  }
  void Display() {
    noStroke();
    fill(200,50,50);
    ellipse(NestCoord[0],NestCoord[1], 10, 10);
  }
}
 
/*
1.    First all the valuables are declared.
2.    In the Setup, the different classes are declared and the desired number of ants and food is created.
3.    Each time Draw runs, the ants and the food is drawn and their next position is calculated
4.    A method is called to check if an ant is above a piece of food. If it is, the food change size
I would recommend creating a new tab (icon with the arrow pointing right in the light gray row) for each class,
that is how I have it when I work with the script in Processing.
*/