> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.7$yeLK68fnJ/rev.6673
 * 
 * authors: 
 *   
 *   
 *   malo
 *   
 *   
 *   
 *   
 *   
 *   
 *   
 *   
 *   jesuisRenaud
 *   
 *   Qui est ce ?
 *   Yo
 *   Yo
 *   
 *   
 *   
 *   
 *   alugourou
 *   
 *   
 *   

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



// This sketch builds on a prior work, "TriGrid un seul escargot", created by jesuisRenaud & Yo  alugourou

//globals

int trianglesSize;
int snailwidth = 0, snailheight = 0;
 
color[] colors = { //define colors here
    color(#9ABD8B), color(#A047FF), 
    color(#FFBE43), color(#FF8620),
    color(#2D3B31), color(#6EAECC)
};

 
int triangleHeight = 20;   //set triangle height
int triangleWidth = ceil(sqrt(3)/2*triangleHeight); //equilateral triangle
int w = triangleWidth;
int h = triangleHeight;
 
int direction = -1, layer = 1;
int xOffset = 0, yOffset = 0;
int memoy;
int xPos , yPos;
int i = 0; // Number of triangle or frame rate ??? 
int j = 0;
int valuesSum = 0;
int valueSumTmp = 0;

int[] values = {
10,22,20,44,15,90,88,45
};

int valueSumTmp = values[0];
 
String word;
String[] words = {
"bla6","bla22","bla20","bla44","bla15","bla90","bla88","bla45"

}; 

alTrianglesOffset = new ArrayList();

void setup() {    
    size(500, 500);
    background(255);
    //frameRate(5);
    stroke(215);
    smooth();
    build();

}

void build(){
    
    

    alTrianglesOffset.add({0,0}); //Premier triangle
    
    alTrianglesOffset.add({w, 0}); // goes right from the 1st position
    alTrianglesOffset.add({0, h/2}); // goes down from the last position
    alTrianglesOffset.add({0, h/2}); // goes down from the last position
    alTrianglesOffset.add({-w, 0}); // goes left from the last position
    alTrianglesOffset.add({0, -h/2}); // goes up from the last position
    
    for (int niveau = 2; niveau < 10; niveau++) {
        //debut du niveau un coup a gauche
        alTrianglesOffset.add({-w, 0}); // left
        // phase montante 
        for (int nb = 1; nb < niveau; nb+=1) { 
            alTrianglesOffset.add({0, -h/2}); // up
            alTrianglesOffset.add({0, -h/2}); // up
        }
        // debut phase montante droite
        for (int nb = 1; nb < niveau; nb+=1) {
            alTrianglesOffset.add({w, 0}); // right
            alTrianglesOffset.add({0, -h/2}); // up
        }
        // debut phase descendante droite
        alTrianglesOffset.add({w, 0});  // right
        for (int nb = 1; nb < niveau; nb+=1) {
            alTrianglesOffset.add({0, h/2}); // down
            alTrianglesOffset.add({w, 0}); // right
        }
        // debut phase descendante
        alTrianglesOffset.add({0, h/2}); // down
        for (int nb = 1; nb < niveau; nb+=1) {
            alTrianglesOffset.add({0, h/2}); // down
            alTrianglesOffset.add({0, h/2}); // down
        }
        // debut phase descendant gauche
        alTrianglesOffset.add({0, h/2}); // down
        for (int nb = 1; nb < niveau; nb+=1) {
            alTrianglesOffset.add({-w, 0}); // left
            alTrianglesOffset.add({0, h/2}); // down
        }
        // debut phase montant gauche
        alTrianglesOffset.add({-w, 0}); // left
        for (int nb = 1; nb < niveau; nb+=1) {
            alTrianglesOffset.add({0, -h/2}); // up
            alTrianglesOffset.add({-w, 0}); // left
        }
        // dernier mvt (début premier segment)
        alTrianglesOffset.add({0, -h/2}); // up 
    }
}
 
void drawGrid() {
    int sens = 1;
    for (int jBg = 0; jBg < height+ h/2; jBg += (h/2)) {
        for (int iBg = w/2; iBg < width + w; iBg += (w)) {
            Triangle bgTri = new Triangle(iBg, jBg, sens);
            bgTri.draw();
            sens *= -1;
        }
        sens *= 1;
    }
}
 
void drawSnail() {
    int nv = 0;
    if (i<values.length){
        for ( int nbt = 0 ; nbt < values[i] ; nbt += (((2*(nv++))+1)*6) ) ;
        if (j<values[i]) {
        int[2] offsetCouple = alTrianglesOffset.get(j); 
            xPos += offsetCouple[0]; // X Offset
            yPos += offsetCouple[1]; // Y Offset 
            Triangle Tri = new Triangle(xPos, yPos,direction);
            Tri.innerColor = colors[i % (colors.length)];
            if ( j==0 ) {
//                word = words[i];
//                pushStyle();
//                fill(0);
//                text(word, xPos, yPos+20);
//                popStyle();
                Tri.innerColor = color(0); //1er triangle de l'escargot en noir
                memoy = yPos;
                memox = xPos;
                }
            fill(Tri.innerColor);
            translate ((w * (nv + snailwidth) + ((w/2)*(nv % 2))),(memoy + (h * (nv + snailheight)) + ((h/2)*(nv % 2))));
// si translate ne modifie pas xPos et yPos, il faut mémoriser les valeurs de translate pour pouvoir déduire la position du prochain premier triangle
// oui translate ne corrompt pas les x et y.
            Tri.draw();
            j++;
            }else{
            j=0;
            xPos += (w * (nv + snailwidth)); //décalage nouveau polygone
            yPos = memoy;
            println("yPos "+yPos);
            if (((xPos + (w * ((nv) + snailwidth)))) > width ) {
                xPos = (w * (nv )); //+ snailwidth));
//                println("xpos "+xPos);
                yPos += (h * 6);
                }
            i++;
            direction = -1;
            // memo taille escargot
            snailwidth = nv;
            snailheigth = nv;
            }
        }
    }
 
boolean bBgInit = true, bFgInit = true;

void draw() {
    if(bBgInit) {
        drawGrid();
        bBgInit = false;
    } else {
        if (bFgInit) {
            stroke(255);
          //noStroke();
            bFgInit = false;
        }
        drawSnail();
    }
    direction *= -1;
}

void keyPressed() {
  if (key == 's' || key == 'S') {
      save(visualisation.png);
  }

  // hack for un-responsive non looping p5 sketches
  if (keyCode == ESC) {
    redraw();
  }
}
//CLASSES : 
//-------------- TRIANGLE ------------------
class Triangle {
  int x, y, direction;
  color innerColor;
 
  //constructor
  Triangle(int _x, int _y, int _direction)
    {
    this.x = _x;
    this.y = _y;
    this.direction = _direction;
    }
  
  void draw() {
    int x1 = this.x+floor(triangleWidth/2)*this.direction;
    int y1 = this.y-triangleHeight/2;
    int x2 = this.x+floor(triangleWidth/2)*this.direction;
    int y2 = this.y+triangleHeight/2;
    int x3 = this.x-floor(triangleWidth/2+1)*this.direction;
    int y3 = this.y;
    
    triangle( x1, y1, x2, y2, x3, y3 );
  }
  
}