hozer

two-point perspective

Dec 10th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.95 KB | None | 0 0
  1. class Cube
  2. {
  3.   float y1, y2, y3, y4, x1, x2, x3, x4, y5, y6;
  4.   float pt1, pt2, midpoint;
  5.   float mX, mY;
  6.   float amt;
  7.  
  8.   color light, darkLeft, darkRight;
  9.   color left, right;
  10.  
  11.   Cube() {}
  12.  
  13.   Cube(float mouse_X, float mouse_Y, float amount, float point1, float point2, float midPoint)
  14.   {
  15.     amt = amount;
  16.     mX = mouse_X;
  17.     mY = mouse_Y;
  18.     pt1 = point1;
  19.     pt2 = point2;
  20.     midpoint = midPoint;
  21.   }
  22.    
  23.   void cubeMath()
  24.   {
  25.     y1 = lerp(0, mY, amt); //change this amount 0.8
  26.     y2 = lerp(400, mY, amt);
  27.    
  28.     y3 = lerp(y1, midpoint, 0.3);
  29.     y4 = lerp(y2, midpoint, 0.3);
  30.    
  31.     x1 = lerp(mX, pt1, 0.3);
  32.     x2 = lerp(mX, pt2, 0.3);
  33.    
  34.     float XYdiff = lerp(x1, x2, 0.5);
  35.     x3 = lerp(mX, XYdiff, 1.52);
  36.     y5 = lerp(y1, midpoint, 0.46);
  37.     y6 = lerp(y2, midpoint, 0.46);
  38.  
  39.     float diff = mX/2.35; //scale mouse to RGB range
  40.     float diffDiff = 255 - diff;
  41.    
  42.     //println(diff + " " + diffDiff);
  43.    
  44.     darkLeft = color(diff);
  45.     light = color(255);
  46.     left = lerpColor(darkLeft, light, 0.5);
  47.    
  48.     darkRight = color(diffDiff);
  49.     right = lerpColor(darkRight, light, 0.5);
  50.   }
  51.    
  52.   void display()
  53.   {
  54.      
  55.     stroke(200);
  56.     strokeWeight(0.5);
  57.    
  58.     line(x1, 0, x1, height);
  59.     line(x2, 0, x2, height);
  60.     line(0, y1, 600, y1);
  61.     line(0, y2, 600, y2);
  62.    
  63.     line(0, y5, 600, y5);
  64.     line(0, y6, 600, y6);
  65.    
  66.     //front
  67.     line(mX, 0, mX, height);
  68.     line(x3, 0, x3, height);
  69.    
  70.     stroke(0);
  71.     strokeWeight(1);
  72.      
  73.     //left
  74.     line(pt1, midpoint, mX, y1);
  75.     line(pt1, midpoint, mX, y2);
  76.    
  77.     //right
  78.     line(pt2, midpoint, mX, y1);
  79.     line(pt2, midpoint, mX, y2);
  80.    
  81.     //top
  82.     line(pt1, midpoint, x2, y3);
  83.     line(pt2, midpoint, x1, y3);
  84.    
  85.     //bottom
  86.     line(pt1, midpoint, x2, y4);
  87.     line(pt2, midpoint, x1, y4);
  88.    
  89.     stroke(255, 0, 0);
  90.     strokeWeight(1);
  91.     line(mX, y1, mX, y2);
  92.      
  93.     fill(right, 150);
  94.    
  95.     //TOP Counter clockwise
  96.     beginShape();
  97.     vertex(x1, y3);
  98.     vertex(mX, y1);
  99.     vertex(x2, y3);
  100.     vertex(x3, y5);
  101.     vertex(x1, y3);
  102.     endShape();
  103.    
  104.     fill(left, 150);
  105.    
  106.     //BOTTOM Clockwise
  107.     beginShape();
  108.     vertex(x1, y4);
  109.     vertex(mX, y2);
  110.      vertex(x2, y4);
  111.     vertex(x3, y6);
  112.     vertex(x1, y4);
  113.     endShape();
  114.    
  115.     line(x3, y5, x3, y6);
  116.    
  117.     //LEFT SIDE Counter clockwise
  118.     beginShape();
  119.     vertex(mX, y1);
  120.     vertex(x1, y3);
  121.     vertex(x1, y4);
  122.     vertex(mX, y2);
  123.     vertex(mX, y1);
  124.     endShape();
  125.    
  126.     fill(right, 100);
  127.    
  128.     //RIGHT SIDE Clockwise
  129.     beginShape();
  130.     vertex(mX, y1);
  131.     vertex(x2, y3);
  132.     vertex(x2, y4);
  133.     vertex(mX, y2);
  134.     vertex(mX, y1);
  135.     endShape();
  136.    
  137.   fill(0);
  138.   /*
  139. //  //Clockwise
  140.   text(int(x1) + ", " + int(y3), x1, y3); //point 1
  141.   text(int(x2) + ", " + int(y3), x2, y3); //point 2
  142.   text(int(x2) + ", " + int(y4), x2, y4); //point 3
  143.   text(int(x1) + ", " + int(y4), x1, y4); //point 4
  144. //
  145.   text(mX + ", " + int(y1), mX, y1);
  146.   text(mX + ", " + int(y2), mX, y2);*/
  147.   }
  148. }
  149.  
  150. Cube cube;
  151. PFont font;
  152. float mX, mY;
  153. float pt1, pt2;
  154. float amt;
  155. float midpoint;
  156.  
  157. void setup()
  158. {
  159.   size(600, 600);
  160.   smooth();
  161.   //String[] fontlist = PFont.list();
  162.   //println(fontlist);
  163.   amt = 1.4;
  164.   pt1 = 3;
  165.   pt2 = 597;
  166.   midpoint = 300;
  167. }
  168.  
  169. void draw()
  170. {
  171.   background(235, 255, 255, 255);
  172.   fill(0);
  173.   text("Two Point Perspective", 9, 20);
  174.   fill(100);
  175.   text("Drag the points to change width", 9, 40);
  176.   text("Use the UP and DOWN arrows to change height", 9, 60);
  177.    
  178.   mX = mouseX;
  179.   mY = mouseY;
  180.   println(mouseX + " " + mouseY);
  181.    
  182.   fill(255,0,0);
  183.   stroke(0);
  184.   strokeWeight(1);
  185.      
  186.   line(0, midpoint, 600, midpoint); //horizon line
  187.    
  188.   ellipse(pt1, midpoint, 6, 6);
  189.   ellipse(pt2, midpoint, 6, 6);
  190.    
  191.   cube = new Cube(mX, mY, amt, pt1, pt2, midpoint);
  192.   cube.cubeMath();
  193.   cube.display();
  194.    
  195.   fill(0);
  196.   if(pt1 < 250)
  197.   {
  198.     text(pt1 + ", " + midpoint, pt1, midpoint-10);
  199.   }
  200.   if(pt1 > 250)
  201.   {
  202.     text(pt1 + ", " + midpoint, pt1-50, midpoint-10);
  203.   }
  204.    
  205.   if(pt2 > 550)
  206.   {
  207.     text(pt2 + ", " + midpoint, pt2-55, midpoint-10);
  208.   }
  209.   if(pt2 < 550)
  210.   {
  211.     text(pt2 + ", " + midpoint, pt2, midpoint-10);
  212.   }
  213.    
  214. }
  215.  
  216. void mouseDragged()
  217. {
  218.   if((mX > pt1-10) && (mX < (pt1+10)) && (mY > midpoint-10) && (mY < midpoint+10))
  219.   {
  220.     pt1 = mouseX;
  221.        
  222.     if(pt1 < 3)
  223.     {
  224.       pt1 = 3;
  225.     }
  226.     if(pt1 > 294)
  227.     {
  228.       pt1 = 294;
  229.     }
  230.   }
  231.    
  232.   if((mX > pt2-10) && (mX < (pt2+10)) && (mY > midpoint-10) && (mY < midpoint+10))
  233.   {
  234.     pt2 = mouseX;
  235.        
  236.     if(pt2 > 597)
  237.     {
  238.       pt2 = 597;
  239.     }
  240.     if(pt2 < 306)
  241.     {
  242.       pt2 = 306;
  243.     }
  244.   }
  245. }
  246.  
  247. void keyReleased()
  248. {
  249.   if(keyCode == UP)
  250.   {
  251.     amt = amt + 0.1;
  252.     if(amt > 2.0)
  253.     {
  254.       amt = 2.0;
  255.     }
  256.   }
  257.    
  258.   if(keyCode == DOWN)
  259.   {
  260.     amt = amt - 0.1;
  261.     if(amt < 1.0)
  262.     {
  263.       amt = 1.0;
  264.     }
  265.   }
  266.  
  267.   println(amt);
  268. }
Advertisement
Add Comment
Please, Sign In to add comment