星の絨毯 prpcessing 星 ★

f:id:peroon:20090805012600j:image
メイン関数。

int colN = 14;
int rowN = 10;
int W = 40;
Star[][] star = new Star[colN][rowN];

void setup(){
  size(640, 480);
  background(255);
  strokeWeight(3);
  stroke(0);
  noLoop();
  colorMode(HSB);
  
  
  for(int i=0; i<colN; i++){
    for(int j=0; j<rowN; j++){
      star[i][j] = new Star(55 + i*W, 60 + j*W, W);
    }
  }
}

void draw(){
  background(255);
  
  for(int i=0; i<colN; i++){
    for(int j=0; j<rowN; j++){
      star[i][j].SET();
      star[i][j].RAN();
      star[i][j].DRAW();
    }
  }
  saveFrame("hoge.jpg");
}

星クラス

class Star{
  int cX, cY;//center
  int W;
  int R;
  color col;
  float theta;
  float th = TWO_PI/5;
  
  int[] X;
  int[] Y;
  int[] Z;
  
  Star(int cX, int cY, int W){
    this.cX = cX;
    this.cY = cY;
    this.W  = W;
    R = W/2;
    theta = random( 3.14 );
    
    X = new int[5];
    Y = new int[5];
    Z = new int[5];
    
    col = color( random(150,255), random(200,255), random(200,255) );
  }
  
  void SET(){
    for(int i=0; i<5; i++){
      X[i] = cX + int( R * cos( theta + th*i ) );
      Y[i] = cY + int( R * sin( theta + th*i ) );
      print(R + "\n");
    }
  }
  
  void RAN(){
    int R = 5;
    for(int i=0; i<5; i++){
      X[i] += (int)random(-R, R);
      Y[i] += (int)random(-R, R);
    }
  }
  
  void DRAW(){
    stroke(col);
    line( X[0], Y[0], X[2], Y[2] );
    line( X[2], Y[2], X[4], Y[4] );
    line( X[4], Y[4], X[1], Y[1] );
    line( X[1], Y[1], X[3], Y[3] );
    line( X[3], Y[3], X[0], Y[0] );
  }
}