Advertisement
wblut

sketch_190823a_City_on_the_Hill

Aug 24th, 2019
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.45 KB | None | 0 0
  1. //Click to slice and dice.
  2.  
  3. Box root;
  4. int maxLevel;
  5. int gap;
  6. int nx, ny, nz, tmp;
  7. int[] range;
  8.  
  9. void setup() {
  10.   fullScreen(P3D);
  11.   smooth();
  12.   root=new Box(0, 0, 0, 200, 1200, 1200);
  13.   maxLevel=0;
  14.   gap=0;
  15.   range=new int[]{root.cx-root.dx/2,root.cx+root.dx/2,root.cy-root.dy/2,root.cy+root.dy/2,root.cz-root.dz/2,root.cz+root.dz/2};
  16. }
  17.  
  18. void draw() {
  19.   background(220);
  20.   ortho(-width/2, width/2, -height/2, height/2, 0.1, 100000);
  21.   translate(width/2, height/2, -10000);
  22.   rotate(PI/3.0);
  23.   directionalLight(255, 255, 255, 1, 1, -1);
  24.   directionalLight(127, 127, 127, -1, -1, 1);
  25.   rotateX(asin(1.0/sqrt(3.0)));
  26.   rotateY(QUARTER_PI);
  27.   noStroke();
  28.   translate(-(range[0]+range[1])/2,-(range[2]+range[3])/2,-(range[4]+range[5])/2);
  29.   scale(0.5);
  30.   root.draw();
  31. }
  32.  
  33. void sliceAndDice(){
  34. int roll=(int)random(6);//DICE
  35.   switch(roll) {//SLICE
  36.   case 0:
  37.     root.splitAndTranslateX(20*(int)random(-15, 15), 10*(int)random(-5, 5), 10*(int)random(-5, 5));
  38.     break;
  39.   case 1:
  40.     root.splitAndTranslateY(20*(int)random(-15, 15), 10*(int)random(-5, 5), 10*(int)random(-5, 5));
  41.     break;
  42.   case 2:
  43.     root.splitAndTranslateZ(20*(int)random(-15, 15), 10*(int)random(-5, 5), 10*(int)random(-5, 5));
  44.     break;
  45.   case 3:
  46.     root.splitAndRotateX(20*(int)random(-15, 15), 5*(int)random(-5, 5), 5*(int)random(-5, 5), (int)random(1, 3));
  47.     break;
  48.   case 4:
  49.     root.splitAndRotateY(20*(int)random(-15, 15), 5*(int)random(-5, 5), 5*(int)random(-5, 5), (int)random(1, 3));
  50.     break;
  51.   default:
  52.     root.splitAndRotateZ(20*(int)random(-15, 15), 5*(int)random(-5, 5), 5*(int)random(-5, 5), (int)random(1, 3));
  53.   }
  54.   root.getRange(range);
  55.   maxLevel++;  
  56. }
  57.  
  58. void mousePressed() {
  59.   sliceAndDice();
  60. }
  61.  
  62. class Box {
  63.   Box parent;
  64.   int cx, cy, cz;
  65.   int dx, dy, dz;
  66.   ArrayList<Box> children;
  67.   int level;
  68.   Box(int cx, int cy, int cz, int dx, int dy, int dz) {
  69.     parent=null;
  70.     this.cx=cx;
  71.     this.cy=cy;
  72.     this.cz=cz;
  73.     this.dx=dx;
  74.     this.dy=dy;
  75.     this.dz=dz;
  76.     children=new ArrayList<Box>();
  77.     level=0;
  78.   }
  79.  
  80.   void draw() {
  81.     if (children.size()==0) {
  82.       pushMatrix();
  83.       translate(cx, cy, cz);
  84.       box(dx, dy, dz);
  85.       popMatrix();
  86.     } else {
  87.       if (children.size()>0) for (Box child : children) child.draw();
  88.     }
  89.   }
  90.  
  91.   void splitAndTranslateX(int x, int y, int z) {
  92.     Box child1, child2;
  93.     if (children.size()==0) {
  94.       if (x<=cx-dx/2) {
  95.         child2=new Box(cx, cy, cz, dx, dy, dz);
  96.         child2.translateBox(gap, y, z);
  97.         child2.parent=this;
  98.         child2.level=level+1;
  99.         children.add(child2);
  100.       } else if (x>=cx+dx/2) {
  101.         child1=new Box(cx, cy, cz, dx, dy, dz);
  102.         child1.parent=this;
  103.         child1.level=level+1;
  104.         children.add(child1);
  105.       } else {
  106.         child1=new Box((x+cx-dx/2)/2, cy, cz, x-cx+dx/2, dy, dz);
  107.         child1.parent=this;
  108.         child1.level=level+1;
  109.         children.add(child1);
  110.         child2=new Box((cx+dx/2+x)/2, cy, cz, cx+dx/2-x, dy, dz);
  111.         child2.translateBox(gap, y, z);
  112.         child2.parent=this;
  113.         child2.level=level+1;
  114.         children.add(child2);
  115.       }
  116.     } else {
  117.       if (children.size()>0) for (Box child : children) child.splitAndTranslateX(x, y, z);
  118.     }
  119.   }
  120.  
  121.   void splitAndRotateX(int x, int y, int z, int r) {
  122.     Box child1, child2;
  123.     if (children.size()==0) {
  124.       if (x<=cx-dx/2) {
  125.         child2=new Box(cx, cy, cz, dx, dy, dz);
  126.         for (int i=0; i<r; i++) {
  127.           child2.rotateBoxX(y, z);
  128.         }
  129.         child2.translateBox(gap, 0, 0);
  130.         child2.parent=this;
  131.         child2.level=level+1;
  132.         children.add(child2);
  133.       } else if (x>=cx+dx/2) {
  134.         child1=new Box(cx, cy, cz, dx, dy, dz);
  135.         child1.parent=this;
  136.         child1.level=level+1;
  137.         children.add(child1);
  138.       } else {
  139.         child1=new Box((x+cx-dx/2)/2, cy, cz, x-cx+dx/2, dy, dz);
  140.         child1.parent=this;
  141.         child1.level=level+1;
  142.         children.add(child1);
  143.         child2=new Box((cx+dx/2+x)/2, cy, cz, cx+dx/2-x, dy, dz);
  144.         for (int i=0; i<r; i++) {
  145.           child2.rotateBoxX(y, z);
  146.         }
  147.         child2.translateBox(gap, 0, 0);
  148.         child2.parent=this;
  149.         child2.level=level+1;
  150.         children.add(child2);
  151.       }
  152.     } else {
  153.       if (children.size()>0) for (Box child : children) child.splitAndRotateX(x, y, z, r);
  154.     }
  155.   }
  156.  
  157.   void splitAndTranslateY(int y, int x, int z) {
  158.     Box child1, child2;
  159.     if (children.size()==0) {
  160.       if (y<=cy-dy/2) {
  161.         child2=new Box(cx, cy, cz, dx, dy, dz);
  162.         child2.translateBox(x, gap, z);
  163.         child2.parent=this;
  164.         child2.level=level+1;
  165.         children.add(child2);
  166.       } else if (y>=cy+dy/2) {
  167.         child1=new Box(cx, cy, cz, dx, dy, dz);
  168.         child1.parent=this;
  169.         child1.level=level+1;
  170.         children.add(child1);
  171.       } else {
  172.         child1=new Box(cx, (y+cy-dy/2)/2, cz, dx, y-cy+dy/2, dz);
  173.         child1.parent=this;
  174.         child1.level=level+1;
  175.         children.add(child1);
  176.         child2=new Box(cx, (cy+dy/2+y)/2, cz, dx, cy+dy/2-y, dz);
  177.         child2.translateBox(x, gap, z);
  178.         child2.parent=this;
  179.         child2.level=level+1;
  180.         children.add(child2);
  181.       }
  182.     } else {
  183.       if (children.size()>0) for (Box child : children) child.splitAndTranslateY(y, x, z);
  184.     }
  185.   }
  186.  
  187.   void splitAndRotateY(int y, int x, int z, int r) {
  188.     Box child1, child2;
  189.     if (children.size()==0) {
  190.       if (y<=cy-dy/2) {
  191.         child2=new Box(cx, cy, cz, dx, dy, dz);
  192.         for (int i=0; i<r; i++) {
  193.           child2.rotateBoxY(x, z);
  194.         }
  195.         child2.translateBox(0, gap, 0);
  196.         child2.parent=this;
  197.         child2.level=level+1;
  198.         children.add(child2);
  199.       } else if (y>=cy+dy/2) {
  200.         child1=new Box(cx, cy, cz, dx, dy, dz);
  201.         child1.parent=this;
  202.         child1.level=level+1;
  203.         children.add(child1);
  204.       } else {
  205.         child1=new Box(cx, (y+cy-dy/2)/2, cz, dx, y-cy+dy/2, dz);
  206.         child1.parent=this;
  207.         child1.level=level+1;
  208.         children.add(child1);
  209.         child2=new Box(cx, (cy+dy/2+y)/2, cz, dx, cy+dy/2-y, dz);
  210.         for (int i=0; i<r; i++) {
  211.           child2.rotateBoxY(x, z);
  212.         }
  213.         child2.translateBox(0, gap, 0);
  214.         child2.parent=this;
  215.         child2.level=level+1;
  216.         children.add(child2);
  217.       }
  218.     } else {
  219.       if (children.size()>0) for (Box child : children) child.splitAndRotateY(y, x, z, r);
  220.     }
  221.   }
  222.  
  223.   void splitAndTranslateZ(int z, int x, int y) {
  224.     Box child1, child2;
  225.     if (children.size()==0) {
  226.       if (z<=cz-dz/2) {
  227.         child2=new Box(cx, cy, cz, dx, dy, dz);
  228.         child2.translateBox(x, y, gap);
  229.         child2.parent=this;
  230.         child2.level=level+1;
  231.         children.add(child2);
  232.       } else if (z>=cz+dz/2) {
  233.         child1=new Box(cx, cy, cz, dx, dy, dz);
  234.         child1.parent=this;
  235.         child1.level=level+1;
  236.         children.add(child1);
  237.       } else {
  238.         child1=new Box(cx, cy, (z+cz-dz/2)/2, dx, dy, z-cz+dz/2);
  239.         child1.parent=this;
  240.         child1.level=level+1;
  241.         children.add(child1);
  242.         child2=new Box(cx, cy, (cz+dz/2+z)/2, dx, dy, cz+dz/2-z);
  243.         child2.translateBox(x, y, gap);
  244.         child2.parent=this;
  245.         child2.level=level+1;
  246.         children.add(child2);
  247.       }
  248.     } else {
  249.       if (children.size()>0) for (Box child : children) child.splitAndTranslateZ(z, x, y);
  250.     }
  251.   }
  252.  
  253.   void splitAndRotateZ(int z, int x, int y, int r) {
  254.     Box child1, child2;
  255.     if (children.size()==0) {
  256.       if (z<=cz-dz/2) {
  257.         child2=new Box(cx, cy, cz, dx, dy, dz);
  258.         for (int i=0; i<r; i++) {
  259.           child2.rotateBoxZ(x, y);
  260.         }
  261.         child2.translateBox(0, 0, gap);
  262.         child2.parent=this;
  263.         child2.level=level+1;
  264.         children.add(child2);
  265.       } else if (z>=cz+dz/2) {
  266.         child1=new Box(cx, cy, cz, dx, dy, dz);
  267.         child1.parent=this;
  268.         child1.level=level+1;
  269.         children.add(child1);
  270.       } else {
  271.         child1=new Box(cx, cy, (z+cz-dz/2)/2, dx, dy, z-cz+dz/2);
  272.         child1.parent=this;
  273.         child1.level=level+1;
  274.         children.add(child1);
  275.         child2=new Box(cx, cy, (cz+dz/2+z)/2, dx, dy, cz+dz/2-z);
  276.         for (int i=0; i<r; i++) {
  277.           child2.rotateBoxZ(x, y);
  278.         }
  279.         child2.translateBox(0, 0, gap);
  280.         child2.parent=this;
  281.         child2.level=level+1;
  282.         children.add(child2);
  283.       }
  284.     } else {
  285.       if (children.size()>0) for (Box child : children) child.splitAndRotateZ(z, x, y, r);
  286.     }
  287.   }
  288.  
  289.   void translateBox(int x, int y, int z) {
  290.     cx+=x;
  291.     cy+=y;
  292.     cz+=z;
  293.   }
  294.  
  295.   void rotateBoxZ(int rcx, int rcy) {
  296.     nx=cx-rcx;
  297.     ny=cy-rcy;
  298.     cx=-ny+rcx;
  299.     cy=nx+rcy;
  300.     tmp=dx;
  301.     dx=dy;
  302.     dy=tmp;
  303.   }
  304.  
  305.   void rotateBoxY(int rcx, int rcz) {
  306.     nx=cx-rcx;
  307.     nz=cz-rcz;
  308.     cx=-nz+rcx;
  309.     cz=nx+rcz;
  310.     tmp=dx;
  311.     dx=dz;
  312.     dz=tmp;
  313.   }
  314.  
  315.   void rotateBoxX(int rcy, int rcz) {
  316.     nz=cz-rcz;
  317.     ny=cy-rcy;
  318.     cz=-ny+rcz;
  319.     cy=nz+rcy;
  320.     tmp=dz;
  321.     dz=dy;
  322.     dy=tmp;
  323.   }
  324.  
  325.   void getRange(int[] range){
  326.     if (children.size()==0) {
  327.       range[0]=min(range[0],cx-dx/2);
  328.       range[1]=max(range[1],cx+dx/2);
  329.       range[2]=min(range[2],cy-dy/2);
  330.       range[3]=max(range[3],cy+dy/2);
  331.       range[4]=min(range[4],cz-dz/2);
  332.       range[5]=max(range[5],cz+dz/2);
  333.  
  334.     } else {
  335.       if (children.size()>0) for (Box child : children) child.getRange(range);
  336.     }  
  337.   }
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement