hozer

CG_LAB1

Sep 18th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. class square
  2. {
  3.  float x, y, a;
  4.  
  5.  square(float xp, float yp, float ap)
  6.  {
  7.    x = xp; y = yp; a = ap;
  8.  }
  9. }
  10.  
  11. square[] sqrs = new square[16];
  12. int a = 0, del = 0;
  13. int colorch = 0, sqcol = 0;
  14. void setup()
  15. {
  16.   size(800, 600);
  17.   frame.setResizable(true);
  18. }
  19.  
  20. void draw() {
  21.   float padding = 40;
  22.   float minSide = height;
  23.   float offsetTop, offsetLeft, rA;
  24.   //calc center and sides
  25.   background(255);
  26.   if (minSide > width)
  27.   {
  28.     minSide = width;
  29.     rA = minSide-padding;
  30.     offsetTop = height - height / 2 - rA/2;
  31.     offsetLeft = padding/2;
  32.    
  33.   }
  34.   else
  35.   {
  36.     rA = minSide-padding;
  37.     offsetLeft = width - width / 2 - rA/2;
  38.     offsetTop = padding/2;
  39.   }
  40.   //calc squares pos's and sizes
  41.   sqrs[0] = new square(offsetLeft, offsetTop, rA/4);
  42.   sqrs[1] = new square(offsetLeft+rA/4, offsetTop, rA/4);
  43.   sqrs[2] = new square(offsetLeft, offsetTop+rA/4, rA/4);
  44.   sqrs[3] = new square(offsetLeft+rA/4, offsetTop+rA/4, rA/4);
  45.  
  46.   sqrs[4] = new square(offsetLeft+rA/2, offsetTop, rA/4);
  47.   sqrs[5] = new square(offsetLeft+rA/4+rA/2, offsetTop, rA/4);
  48.   sqrs[6] = new square(offsetLeft+rA/2, offsetTop+rA/4, rA/4);
  49.   sqrs[7] = new square(offsetLeft+rA/4+rA/2, offsetTop+rA/4, rA/4);
  50.  
  51.   sqrs[8] = new square(offsetLeft, offsetTop+rA/2, rA/4);
  52.   sqrs[9] = new square(offsetLeft+rA/4, offsetTop+rA/2, rA/4);
  53.   sqrs[10] = new square(offsetLeft, offsetTop+rA/4+rA/2, rA/4);
  54.   sqrs[11] = new square(offsetLeft+rA/4, offsetTop+rA/4+rA/2, rA/4);
  55.  
  56.   sqrs[12] = new square(offsetLeft+rA/2, offsetTop+rA/2, rA/4);
  57.   sqrs[13] = new square(offsetLeft+rA/4+rA/2, offsetTop+rA/2, rA/4);
  58.   sqrs[14] = new square(offsetLeft+rA/2, offsetTop+rA/4+rA/2, rA/4);
  59.   sqrs[15] = new square(offsetLeft+rA/4+rA/2, offsetTop+rA/4+rA/2, rA/4);
  60.  
  61.   //draw squares
  62.   for(int i = 0; i < 16; i++) {
  63.     rect(sqrs[i].x, sqrs[i].y, sqrs[i].a, sqrs[i].a);;
  64.   }
  65.  
  66.   //rotate
  67.   for(int i = 0, k = 1; i < 16; i++)
  68.   {
  69.     rotate(sqrs[i], a*k);
  70.     if ((i) % 2==0) k *= -1;
  71.   }
  72.  
  73.   //increase angel
  74.   if (a < 360) a++;
  75.   else a = 0;
  76.  
  77.   delay(del);
  78. }
  79.  
  80. void rotate(square sq, int ang)
  81. {
  82.   float rad, x, a1;
  83.   boolean neg = false;
  84.   //stab ang
  85.   if (ang < 0) {neg = true; ang = abs(ang);}
  86.   if (ang > 90) ang -= int(ang/90)*90;
  87.   if (neg) ang = 90-ang;
  88.   rad = radians(ang);
  89.   //calculate
  90.   x = int(sq.a/(sin(rad) + cos(rad)));
  91.   a1 = int(x*sin(rad));
  92.   //draw
  93.   line(sq.x, sq.y+a1, sq.x+sq.a-a1, sq.y);
  94.   line(sq.x+sq.a-a1, sq.y, sq.x+sq.a, sq.y+sq.a-a1);
  95.   line(sq.x+sq.a, sq.y+sq.a-a1, sq.x+a1, sq.y+sq.a);
  96.   line(sq.x+a1, sq.y+sq.a, sq.x, sq.y+a1);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment