Advertisement
xeromino

rotStars

Feb 12th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. int i, col, unit, radius, np, num=7;
  2. float r1, r2, t;
  3. Star[] stars = new Star[num*num];
  4. color[] palette = {
  5.   #490A3D, #BD1550, #E97F02, #F8CA00, #8A9B0F
  6. }
  7. ;
  8.  
  9. void setup() {
  10.   size(500, 500, P3D);
  11.   unit = width/(num+2);
  12.   radius = unit/2;
  13.   noStroke();
  14.  
  15.   for (int y=unit+unit/2; y<height-unit; y += unit) {
  16.     for (int x=unit+unit/2; x<width-unit; x += unit) {
  17.       r1 = random(radius*.25, radius*.65) ;
  18.       r2 = random(radius*.75, radius*.90);
  19.       np = (int) random(5, 10);
  20.       col = (int) random(0, 5);
  21.       stars[i]= new Star(x, y, r1, r2, np, col, t);
  22.       i++;
  23.       t += (TWO_PI/(num*num));
  24.     }
  25.   }
  26. }
  27.  
  28. void draw() {
  29.   background(20);
  30.   for (int j=0; j<stars.length; j++) {
  31.     stars[j].display();
  32.   }
  33.  
  34.   if (frameCount%4==0 && frameCount<241) saveFrame("image-###.gif");
  35. }
  36.  
  37. class Star {
  38.   float x, y, radius1, radius2, angle, halfAngle, theta;
  39.   int npoints, col;
  40.  
  41.   Star(float _x, float _y, float _radius1, float _radius2, int _npoints, int _col, float _theta) {
  42.     x = _x;
  43.     y = _y;
  44.     radius1 = _radius1;
  45.     radius2 = _radius2;
  46.     npoints = _npoints;
  47.     col = _col;
  48.     angle = TWO_PI / npoints;
  49.     halfAngle = angle/2.0;
  50.     theta = _theta;
  51.     //theta = random(TWO_PI);
  52.   }
  53.  
  54.   void display() {
  55.     fill(palette[col]);
  56.     pushMatrix();
  57.     translate(x,y);
  58.     rotateY(theta);
  59.     rotateZ(theta/2);
  60.     beginShape();
  61.     for (float a = 0; a < TWO_PI; a += angle) {
  62.       float sx = cos(a) * radius2;
  63.       float sy = sin(a) * radius2;
  64.       float sz = map(sin(theta),-1,1,0,20);
  65.       //float sz = 0;
  66.       vertex(sx, sy,sz);
  67.       sx = cos(a+halfAngle) * radius1;
  68.       sy = sin(a+halfAngle) * radius1;
  69.       vertex(sx, sy,sz);
  70.     }
  71.     endShape(CLOSE);
  72.     popMatrix();
  73.    
  74.     theta += 0.0523;
  75.        
  76.   }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement