topical media & game development

talk show tell print

#dynamic-FR4.pde / pde



  
Foxes and Rabbits.

  
    float rabbit = 100.0;  // rabbits at t=0
    float rabbit_old=1000;
    float fox = 10;        // foxes at t=0
    float a = 0.4;         // birth rate rabbits
    float b = 0.001;       // mortality of rabbtis due to food limitations
    float c = 0.01;        // mortality of rabbits due to foxes
    float d = 0.1;         // mortality of foxes
    float e = 0.002;       // growth of foxes by eating rabbits
    float scaleR=3;
    float scaleF=1;
    int time=0;
    int offsetx=200;
    int offsety=50;
    int j = 0;
    PImage f;
    PImage r;
  
    
  void setup() {
  
    PFont fontA;
    f = loadImage("dynamic-data-fox.gif");
    r = loadImage("dynamic-data-rabbit.gif");
    size(1200, 800);
    frameRate(30);
    colorMode(RGB, 100);
    background(25);
    noFill();
    // AE: fontA = loadFont("ArialMT-20.vlw");
    // Load the font. Fonts must be placed within the data 
    // directory of your sketch. Use Tools > Create Font 
    // to create a distributable bitmap font. 
    // For vector fonts, use the createFont() function. 
    //  fontA = loadFont("Arial-Black-24.vlw");
    // Set the font and its size (in units of pixels)
    // AE: textFont(fontA, 20);
  }
  
  void draw() {
    
  //  a=0;
  //  b=0;
  //  c=0;
  //  d=0;
  //  e=0;
        fill(100);
        stroke(0);
        ellipse(scaleR*rabbit+10, scaleF*((800)-10*fox)-20,10,10);
        stroke(100);
        line(0, 780, 600, 780);
        line(10, 800, 10, 200);
        fill(0,0,255);
        noStroke();
        rectMode(CORNER);
        imageMode(CENTER);
        rabbit_old=rabbit;
        rabbit=rabbit+a*rabbit-b*rabbit*rabbit-c*rabbit*fox;
        fox=fox-d*fox+e*rabbit*fox;
        fill(25,25,25);
        rect(0,0,1200,200);
        rect(600,0,600,400);
        fill(255,255,255);
        if (rabbit_old<rabbit) {
        text("The rabbit population grows, when there is enough food",600,300);
        text("and only a few foxes",600,330);
        } else
        {
        text("Otherwise the rabbit population decreases",600,350);
        text("and later also the foxes",600,380);
        }
        rabbit_old=rabbit;
        stroke(100);
        line(600, 700, 1200, 700);
        line(600, 700, 600, 400);
        noStroke();
        fill(255,0,0);
        ellipse(600+time*2, 700-fox*2,2,2);
        fill(0,255,0);
        ellipse(600+time*2, 700-rabbit*2,2,2);
        time=time+1;
  //  foxes
        int maxfox=int(fox);
        int number_f= int(1+maxfox-maxfox%20)/20;
        int i_f=number_f;     
        while (i_f>0)
        {
          j = 0;
          while (j<20)
          {
            image(f,22*j+offsetx,offsety+40*(number_f-i_f),30,30);        
            fill(255,255,255);
            j=j+1;
          }
          j = 0;
          while (j<maxfox%20)
          {
            image(f,22*(maxfox%20-j-1)+offsetx,offsety+40*(number_f),30,30);
            fill(255,255,255);
            j=j+1;
          }
          i_f=i_f-1;
          fill(255,255,255);
        }
  //  rabbits  
        int maxrabbit =int(rabbit);
        int number_r= int(1+maxrabbit-maxrabbit%20)/20;
        int i_r=number_r;
        while (i_r>0)
        {
          int j_r = 0;
          while (j_r<20)
          {
            image(r,20*j_r+offsetx+500,offsety+40*(number_r-i_r),40,40);
  //          text(j_r%10+1,20*j_r+offsetx+500,offsety+40*(number_r-i_r));
            j_r=j_r+1;
          }
          j_r = 0;
          while (j_r<maxrabbit%20)
          {
            image(r,20*(maxrabbit%20-j_r-1)+offsetx+500,offsety+40*(number_r),40,40);
  //          text(j_r+1,20*j_r+offsetx+500,offsety+40*(number_r));
            j_r=j_r+1;
          }
          i_r=i_r-1;
          fill(255,255,255);
          text("rabbits: "+int(rabbit),10,offsety+20);
          text("foxes: "+int(fox),10,offsety);
  //        text("fox rest: "+int(maxfox%20),10,offsety+40);
  //        text("rabbit rest: "+int(maxrabbit%20),10,offsety+60);
        }  
  
  }
  
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.