• fullscreen
  • Carre.pde
  • tissueGenerator.pde
  • class Carre {
      
      PVector pos;
      PVector dep;
      PVector vel;
      PVector posa;
      PVector posb;
      PVector posc;
      PVector posd;
      float cote;
      int r;
      int g;
      int b;
      int t;
      
      Carre(float xin,float yin, float cotein, int rin, int gin, int bin, int tin) {
        pos = new PVector(xin, yin);
        dep = new PVector(0,0);
        vel = new PVector(0,0);
        cote = cotein;
        posa = new PVector(pos.x+cote/2,pos.y+cote/2);
        posb = new PVector(pos.x+cote/2,pos.y-cote/2);
        posc = new PVector(pos.x-cote/2,pos.y-cote/2);
        posd = new PVector(pos.x-cote/2,pos.y+cote/2);
        r = rin;
        g = gin;
        b = bin;
        t = tin;
      }
      
      void run() {
        frame();
        update();
        display();
      }
      
      void display() {
        noStroke();
        fill(r,g,b,t);
        rectMode(CORNERS);
        float subwsize = wsize/division;
        for (int i = 0; i < division; i++) {
          for (int j = 0; j < division; j++) {
            if (symetrie) {
              if (i%2 == 0 && j%2 == 0) {
                rect(i*subwsize,posa.y/division+j*subwsize,i*subwsize+subwsize,posb.y/division+j*subwsize);
                rect(posa.x/division+i*subwsize,j*subwsize,posc.x/division+i*subwsize,j*subwsize+subwsize);
              } else if (i%2 == 0 && j%2 == 1) {
                rect(i*subwsize,subwsize-(posa.y/division)+j*subwsize,i*subwsize+subwsize,subwsize-(posb.y/division)+j*subwsize);
                rect(posa.x/division+i*subwsize,j*subwsize,posc.x/division+i*subwsize,j*subwsize+subwsize);
              } else if (i%2 == 1 && j%2 == 0) {
                rect(i*subwsize,posa.y/division+j*subwsize,i*subwsize+subwsize,posb.y/division+j*subwsize);
                rect(subwsize-(posa.x/division)+i*subwsize,j*subwsize,subwsize-(posc.x/division)+i*subwsize,j*subwsize+subwsize);
              } else if (i%2 == 1 && j%2 == 1) {
                rect(i*subwsize,subwsize-(posa.y/division)+j*subwsize,i*subwsize+subwsize,subwsize-(posb.y/division)+j*subwsize);
                rect(subwsize-(posa.x/division)+i*subwsize,j*subwsize,subwsize-(posc.x/division)+i*subwsize,j*subwsize+subwsize);
              }
            } else {
              rect(i*subwsize,posa.y/division+j*subwsize,i*subwsize+subwsize,posb.y/division+j*subwsize);
              rect(posa.x/division+i*subwsize,j*subwsize,posc.x/division+i*subwsize,j*subwsize+subwsize);
            }
          }
        }
        if (show) {
          noFill();
          stroke(0);
          strokeWeight(1);
          beginShape();
          vertex(posa.x/division,posa.y/division);
          vertex(posb.x/division,posb.y/division);
          vertex(posc.x/division,posc.y/division);
          vertex(posd.x/division,posd.y/division);
          endShape(CLOSE);
        }
      }
      
      void frame() {
        if (pos.x > wsize-cote/2-ecart/2) {
          pos.x = wsize-cote/2-ecart/2;
        }
        if (pos.x < cote/2+ecart/2) {
          pos.x = cote/2+ecart/2;
        }
        if (pos.y > wsize-cote/2-ecart/2) {
          pos.y = wsize-cote/2-ecart/2;
        }
        if (pos.y < cote/2+ecart/2) {
          pos.y = cote/2+ecart/2;
        }
      }
      
      void update() {
        if (diagonale) {
          if (pos.y != pos.x) {
            dep.y -= pos.y-pos.x;
          }
        }
        vel.add(dep);
        vel.mult(damping);
        pos.add(vel);
        dep.mult(0);
        posa = new PVector(pos.x+cote/2,pos.y+cote/2);
        posb = new PVector(pos.x+cote/2,pos.y-cote/2);
        posc = new PVector(pos.x-cote/2,pos.y-cote/2);
        posd = new PVector(pos.x-cote/2,pos.y+cote/2);
      }
      
    }
    
    ArrayList carres = new ArrayList();
    int wsize = 400;
    float ecart = 5;
    int carrecount = 4;
    int division = 4;
    float tramewidth = wsize/carrecount;
    boolean show = true;
    boolean symetrie = true;
    boolean diagonale = true;
    float damping = 0.5;
    float cotemin = 5;
    
    void setup() {
      size (400,400);
      smooth();
      tramewidth = wsize/carrecount;
      for (int i=0; i < carrecount; i++) {
        carres.add(new Carre(random(wsize), 
                             random(wsize), 
                             random(cotemin,wsize/carrecount),
                             round(random(0,255)), 
                             round(random(0,255)), 
                             round(random(0,255)),
                             round(random(0,150))));
      }
    }
    
    void draw(){
      background(255);
      collision();
      for (int i=0; i < carrecount; i++) {
        Carre carre = (Carre) carres.get(i);
        carre.run();
      }
      fill(0);
      text("a/z:scale | q/s:stripes | w:diagonal | x:symetric | c:show | space:reset",10,wsize-10);
    }
    
    void collision() {
      for (int i=0; i < carres.size(); i++) {
        Carre carrei = (Carre) carres.get(i);
        for (int j=0; j < carres.size(); j++) {
          if (j > i) {
            Carre carrej = (Carre) carres.get(j);
            if (abs(carrej.pos.x-carrei.pos.x) < carrej.cote/2+carrei.cote/2+ecart) {
              if (carrei.pos.x > carrej.pos.x) {
                carrei.dep.x += ((carrej.cote/2+carrei.cote/2+ecart)-abs(carrej.pos.x-carrei.pos.x))/2;
                carrej.dep.x -= ((carrej.cote/2+carrei.cote/2+ecart)-abs(carrej.pos.x-carrei.pos.x))/2;
              } else {
                carrei.dep.x -= ((carrej.cote/2+carrei.cote/2+ecart)-abs(carrej.pos.x-carrei.pos.x))/2;
                carrej.dep.x += ((carrej.cote/2+carrei.cote/2+ecart)-abs(carrej.pos.x-carrei.pos.x))/2;
              }
            }
            if (abs(carrej.pos.y-carrei.pos.y) < carrej.cote/2+carrei.cote/2+ecart) {
              if (carrei.pos.y > carrej.pos.y) {
                carrei.dep.y += ((carrej.cote/2+carrei.cote/2+ecart)-abs(carrej.pos.y-carrei.pos.y))/2;
                carrej.dep.y -= ((carrej.cote/2+carrei.cote/2+ecart)-abs(carrej.pos.y-carrei.pos.y))/2;
              } else {
                carrei.dep.y -= ((carrej.cote/2+carrei.cote/2+ecart)-abs(carrej.pos.y-carrei.pos.y))/2;
                carrej.dep.y += ((carrej.cote/2+carrei.cote/2+ecart)-abs(carrej.pos.y-carrei.pos.y))/2;
              }
            }
          }
        }
      }
    }
    
    void keyPressed() {
      if (key == ' ') {
        carres.clear();
        setup();
      } else if (key == 'z') {
        if (division < 10) {
          division++;
        }
      } else if (key == 'a') {
        if (division > 1) {
          division--;
        }
      } else if (key == 's') {
        if (carrecount < 10) {
          carrecount++;
          carres.add(new Carre(random(wsize), 
                               random(wsize), 
                               random(cotemin,wsize/carrecount),
                               round(random(0,255)), 
                               round(random(0,255)), 
                               round(random(0,255)),
                               round(random(0,150))));
          Carre carre = (Carre) carres.get(carres.size()-1);
          for (int i = 0; i < carres.size()-1; i++) {
            Carre carrei = (Carre) carres.get(i);
            carrei.cote -= carre.cote/(carres.size()-1);
            if (carrei.cote < cotemin) {
              carrei.cote = cotemin;
            }
          }
        }
      } else if (key == 'q') {
        if (carrecount > 1) {
          carrecount--;
          carres.remove(carres.size()-1);
        }
      } else if (key == 'c') {
        show = !show;
      } else if (key == 'x') {
        symetrie = !symetrie;
      } else if (key == 'w') {
        diagonale = !diagonale;
      }
    }
    

    code

    tweaks (0)

    about this sketch

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

    license

    advertisement

    gwenn

    Tissue Generator

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

    An error while dealing with square collision led to this...
    Positions of squares defines positions of stripes.
    The pos.x and pos.y of the squares cannot overlap each other, but independently (that was the 'mistake')
    If the keys are not totally displayed :
    a/z : scale the drawing
    q/s : number of stripes
    w : diagonal arrangement of the squares (pos.y tends to pos.x)
    x : the copies of the pattern base module are symmetrically arranged
    c : show the squares
    space : reset
    and, the sketch got a bit of "physics spirit" in the form finding behavior of the stripes/squares ( acceleration, veloc

    gwenn
    3 Jun 2011
    sometimes squares get stuck on the same y axe, it seems... I will have to work that out, if i got the time... sorry!
    gwenn
    3 Jun 2011
    always doing mistakes!
    You need to login/register to comment.