• fullscreen
  • Hex.pde
  • honeycomb2.pde
  • //my own object - hexagon
    
    class Hexagon {
     
      PVector pos; // position (x,y)
      int age; // how long have i been alive?
      int kid;//kids
      float rad; // my radius
      int maxAge = 30;//set max age
      
      Hexagon parent; // my parent
      
      
      Hexagon(PVector _pos, float _rad, Hexagon _parent){//constructor
         pos = _pos;
         parent = _parent;
         rad = _rad; //make my radius
         age = 0;//start age at 0
         kid = 0;//start number of kids counter at 0.
          
         hexagons.add(this);//add myself to the hexagons list
        
      
      }
    
    
    void hexagon(float r) {//Hexagon
      beginShape();
      float angle = 0;
      for (int v=0; v<6; v++) {
        vertex(cos(angle)*r, sin(angle)*r);
        angle += (TWO_PI/6.0);
      }
      endShape(CLOSE);
    }
    
    void render(){//draw this hexagon
     fill(235 -(age*15),0,0,255-(age*25));//change fill colour and satuation by age account
     pushMatrix();//save the current system 
     translate(pos.x,pos.y);//display haxagon at x,y position
     hexagon(rad);//draw hexagon
     popMatrix();//restore the previous system
     stroke(255);
     strokeWeight(1.0);
     
     if (parent != null){//ignore there are no parents
        
      line(pos.x,pos.y, parent.pos.x, parent.pos.y );//draw lines between hexagons and parent hexagons
     }
     
    }
    
    void grow(){//grow haxagons
    age++;
    //pick a spot to grow a new cell
    float angle = TWO_PI/12 + int (random(0,6))*(TWO_PI/4);//angle change
    
    float distance = rad*2.0; //radius distance change
    //decide position for growth hexagons
    PVector newpos = new PVector(pos.x + cos(angle)*(rad + distance), pos.y + sin(angle)*(rad + distance));
    
     
     //  check if that spot is free - check for collisions with all other hexagons
    
     for (Hexagon checkhexagon : hexagons){ // run through the hexagons list, and check each one
          float checkdist = newpos.dist(checkhexagon.pos);//how far the distance is
          if (checkdist <= rad*2) return; // if you get a collision, give up
        }
       
        // if you can read this, the space is free
        Hexagon newhexagon = new Hexagon(newpos, rad*0.95, this  ); // make a new hexagon
    
       }
    }
    
    
    ArrayList<Hexagon> hexagons = new ArrayList <Hexagon>();//list of all the living hexagons
    
    int maxhexagons = 200;// max number of hexagons
    
    float startrad = 20;
    float minrad = 5;//when reach minimum radius stop growth
    
    void setup(){ //set up
      size (700,700,P2D);
      background(255);
      //stroke(255);
      smooth();
      frameRate(5);//slow frame rate
      
      Hexagon firstHexagon = new Hexagon (new PVector(width/2,height/2),startrad,null);//make start hexagon
    }
    
    void draw(){
      background (255);//redraw background
      
      for (int h=0; h<hexagons.size(); h++){
        Hexagon hexagon = hexagons.get(h);//get each hexagon from the list
        hexagon.render(); // draw the hexagon
        if (hexagons.size() < maxhexagons && hexagon.rad > minrad) 
        //if the max number of hexagons been reached & radius greater than the min radius
        hexagon.grow();  
      }
     
    }
    
    void mouseReleased(){//mouse click
        background(255);//redraw background
        hexagons.clear();//clear hexagon list
        minrad = random(2,10);//random change radius
        Hexagon firstHexagon = new Hexagon(new PVector (mouseX,mouseY),random(startrad-5,startrad+10),null);//make new hexagon
         
      }
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Anaer Anaer

    Eden experiment--Honeycomb2

    Add to Faves Me Likey@! 1
    You must login/register to add this sketch to your favorites.

    mouse click to create a new one

    University of Canberra

    You need to login/register to comment.