quaddamage

How to grow a brain

Mar 18th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.93 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. color[] carnival = {#824C96,#433466,#FFAF4F,#ED733F,#FFFFFF};
  4. color[] wine = {#35013F,#561050,#951556,#E9B5D2, #FFFFFF};
  5. color[] forestdew= {#7dd0ae,#66bf9b,#53a584,#479071, #387e62};
  6. color[] random1 = {#ca054d,#3b1c32,#a4d4b4,#ffcf9c, #387e62};
  7. color[] test = {#f94e01,#2873bd,#bc99fe,#f7db6f, #b96d40};
  8. color[] lpalette = random1;
  9. color[] rpalette = forestdew;
  10. float side, theta, t0, limit;
  11. PVector  a;
  12. PShape s0;
  13. Square[] squares, arr2;
  14. int count,ps;
  15. float attr;
  16. String [] mode = {"lr","lr","ll","ll"};
  17. //String [] mode = {"ll","ll","ll","ll"};
  18. //String [] mode = {"lr","ll","lr","ll"};
  19. //String [] mode = {"lr","ll","lr","ll"};
  20. //String [] mode = {"ll","lr","lr","ll"};
  21.  
  22. void setup()
  23. {
  24.   size(1600, 1000);
  25.   background(0);
  26.   smooth();
  27.   noStroke();
  28.   frameRate(999);
  29.   side = 200;
  30.   limit = 2;
  31.   theta = radians (30f);
  32.   t0 = radians(180f);
  33.   attr = 1.15;
  34.   ps=2;
  35.   squares = new Square[200000];
  36.   count=0;
  37.  
  38.   color initcol = lpalette[0];
  39.   a = new PVector(800,900);
  40.   fill(initcol);
  41.   s0=createShape(RECT, 0, 0, side, side);
  42.   s0.rotate(t0);
  43.   squares[0] = new Square(s0,a,1);
  44.   iterate(a, side, t0, 1, true, true, initcol);
  45.  
  46.   arr2 = Arrays.copyOf(squares, count-2);
  47.   Arrays.sort(arr2);
  48.   render();
  49. }
  50.  
  51. void draw()
  52. {
  53.    int i = min(count-2, 25000) - frameCount;
  54.   //if (i>=0) {arr2[i].draw();}
  55.   //render();
  56.   noLoop();
  57. }
  58.  
  59. void keyPressed() {
  60.   if (key==' ') {save("cabbage"+"-"+hour()+"-"+minute()+"-"+second()+".png");}
  61. }
  62.  
  63. void iterate(PVector v, float m, float t, int gen, boolean lef, boolean larg, color cc) {
  64.   PVector v1, v2;
  65.   PShape s1, s2;
  66.   float l1, l2, l, r, ngl, ngr;
  67.   boolean rle, rla, lle, lla;
  68.   color cl, cr;
  69.   int cas = (int(larg) + (int(lef)+1)*2 -2);
  70.   String opt = mode[cas];
  71.     // Big square side
  72.     l1 = m * cos(theta);    
  73.     // Small square side
  74.     l2 = m * sin(theta);
  75.  
  76.   // Si je suis en mode Large Left ...
  77.   if (opt=="ll") {
  78.     // If it's a left square, we span a large left and a small right (auto-clone)
  79.     l = l1;
  80.     r = l2;
  81.     ngl = t+theta;
  82.     ngr = t-(PI/2-theta);
  83.    
  84.     if (!lef) {
  85.       cl = color (red(lpalette[gen%5])/pow(attr,gen-ps), green(lpalette[gen%5])/pow(attr,gen-ps), blue(lpalette[gen%5])/pow(attr,gen-ps));
  86.       cr = color (red(cc)/attr, green(cc)/attr, blue(cc)/attr);
  87.     } else {
  88.       cl = color (red(cc)/attr, green(cc)/attr, blue(cc)/attr);
  89.       cr = color (red(rpalette[gen%5])/pow(attr,gen-ps), green(rpalette[gen%5])/pow(attr,gen-ps), blue(rpalette[gen%5])/pow(attr,gen-ps));
  90.     }
  91.   }
  92.   //Si je suis en mode Large Right
  93.   else {
  94.     // If it's a right  square, we span a small left and a large right (counter-phase)
  95.     // We also switch to a new color for left line
  96.     l = l2;
  97.     r = l1;
  98.     ngl = t+(PI/2-theta);
  99.     ngr = t-theta;
  100.     if (!lef) {
  101.       cl = color (red(lpalette[gen%5])/pow(attr,gen-ps), green(lpalette[gen%5])/pow(attr,gen-ps), blue(lpalette[gen%5])/pow(attr,gen-ps));
  102.       cr = color (red(cc)/attr, green(cc)/attr, blue(cc)/attr);
  103.     } else {
  104.       cl = color (red(cc)/attr, green(cc)/attr, blue(cc)/attr);
  105.       cr = color (red(rpalette[gen%5])/pow(attr,gen-ps), green(rpalette[gen%5])/pow(attr,gen-ps), blue(rpalette[gen%5])/pow(attr,gen-ps));
  106.     }
  107.   }
  108.  
  109.   // Update vectors
  110.     v1 = new PVector (m*cos(t+PI/2),m*sin(t+PI/2));
  111.     v1.add(v);
  112.     v2 = new PVector (l*cos(ngr+PI/2), l*sin(ngr+PI/2));
  113.     v2.add(v1);
  114.  
  115.   // Create Squares  
  116.     s1=createShape();
  117.     s1.beginShape();  
  118.     s1.fill(cl);
  119.     s1.vertex(0,0);
  120.     s1.vertex(0,l);
  121.     s1.vertex(l,l);
  122.     s1.vertex(l,0);
  123.     s1.endShape();
  124.    
  125.     s2=createShape();
  126.     s2.beginShape();  
  127.     s2.fill(cr);
  128.     s2.vertex(0,0);
  129.     s2.vertex(0,r);
  130.     s2.vertex(r,r);
  131.     s2.vertex(r,0);
  132.     s2.endShape();
  133.    
  134.   // rotate squares
  135.     s1.rotate(ngl);
  136.     s2.rotate(ngr);
  137.    
  138.   // Push into squares array
  139.     squares[count+1] = new Square(s1, v1, gen);
  140.     squares[count+2] = new Square(s2, v2, gen);
  141.     count+=2;
  142.    
  143.   // Iterate
  144.     if (m>limit) {
  145.       // Iterate s1
  146.       //iterate(v1, l, ngl, gen+1, ((opt.substring(1,2)=="l")), ((opt.substring(0,1)=="l")), cl);
  147.       iterate(v1, l, ngl, gen+1, true, ((opt.substring(0,1)=="l")), cl);
  148.       // Iterate s2
  149.       //iterate(v2, r, ngr, gen+1, ((opt.substring(1,2)=="l")), ((opt.substring(0,1)=="l")), cr);
  150.       iterate(v2, r, ngr, gen+1, false, ((opt.substring(0,1)=="l")), cr);
  151.     }
  152. }
  153.  
  154. void render() {
  155.   // Render backwards for better visual effect: That was the main quest!
  156.  
  157.   for (int i=count-3; i>=0; i--) {
  158.     arr2[i].draw();
  159.   }
  160. }
  161.  
  162. class Square implements Comparable {
  163.   PVector pos;
  164.   PShape s;
  165.   int n;
  166.  
  167.   Square (PShape sh, PVector v, int depth) {  
  168.     pos = v;
  169.     s = sh;
  170.     n = depth;
  171.   }
  172.  
  173.   int compareTo(Object o)
  174.   {
  175.     Square other=(Square)o;
  176.     if (other.n > n)  return -1;
  177.     if (other.n == n) return 0;
  178.     return 1;
  179.   }  
  180.  
  181.   void draw() {
  182.     shape(s, pos.x, pos.y);
  183.   }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment