イラスト、モデリング、Unity、VR関連

unityとかblenderとかvr関連の作業メモ

Processing 正六角形

int g_w = 1024;
//int g_h = 1024;
int g_h = 1024;
int g_black = color(0);
int g_white = color(255);
int g_red = color(255,0,0);
int g_green = color(0,255,0);
int g_blue = color(0,0,255);
//int g_cx = rp(0,g_w-1);
//int g_cy = rp(0,g_h-1);
int g_r = round(g_w/16);
int g_cx = 0;
int g_cy = 0;
int g_limit = 100;
int g_cnt = 0;



void setup(){
  //size(1024,1024);
  //size(1181,1024);
  size(1178,1024);//適宜サイズ調整

  noSmooth();
  background(g_black);
  //frameRate(0.5);
  //background(g_white);
  stroke(g_white);
  strokeWeight(3);
  //noStroke();
  noFill();
  //fill(g_black);
  //fill(g_white);
}

void draw(){ 
  background(g_black);
  
  int r_leng = round(g_w/(2+2*sqrt(1-sq(cos(to_rad(30))))))/2;

  int num = 100;

  int sr_leng = round(r_leng*cos(to_rad(30))) ;
  int tr_leng = round(sqrt(sq(r_leng)-sq(sr_leng)));
  for(int i=0;i<num+6;i++){
    for(int j=0;j<num+4;j++){
      if(i%2==0){
        hexagon(r_leng,sr_leng*j*2,i*(r_leng+tr_leng),30);
      }else{
        hexagon(r_leng,sr_leng*j*2-sr_leng,i*(r_leng+tr_leng),30);
      }
    }
  }

void hexagon(int leng,int x,int y,float deg){ //中点から頂点への距離,中点座標,角度
  int ax = leng*1 ;
  int ay = 0;
  int bx = round(leng*0.5);
  int by = round(leng*0.866);
  int cx = round(leng*(-0.5));
  int cy = round(leng*0.866);
  int dx = leng*(-1);
  int dy = 0;
  int ex = round(leng*(-0.5));
  int ey = round(leng*(-0.866));
  int fx = round(leng*0.5);
  int fy = round(leng*(-0.866));

  int rax = rot_x(ax,ay,deg); int ray = rot_y(ax,ay,deg);
  int rbx = rot_x(bx,by,deg); int rby = rot_y(bx,by,deg);
  int rcx = rot_x(cx,cy,deg); int rcy = rot_y(cx,cy,deg);
  int rdx = rot_x(dx,dy,deg); int rdy = rot_y(dx,dy,deg);
  int rex = rot_x(ex,ey,deg); int rey = rot_y(ex,ey,deg);
  int rfx = rot_x(fx,fy,deg); int rfy = rot_y(fx,fy,deg);
  
  rax += x;ray += y;
  rbx += x;rby += y;
  rcx += x;rcy += y;
  rdx += x;rdy += y;
  rex += x;rey += y;
  rfx += x;rfy += y;

  line(rax,ray,rbx,rby);
  line(rbx,rby,rcx,rcy);
  line(rcx,rcy,rdx,rdy);
  line(rdx,rdy,rex,rey);
  line(rex,rey,rfx,rfy);
  line(rfx,rfy,rax,ray);
}

int rot_x(int x,int y,float deg){
  float rad = to_rad(deg);
  return round( x*cos(rad) - y*sin(rad));
}

int rot_y(int x,int y,float deg){
  float rad = to_rad(deg);
  return round( x*sin(rad) + y*cos(rad));
}

void rot_at_center(int deg){
  translate(g_w/2,g_h/2);
  rotate(to_rad(deg));
  translate(-g_w/2,-g_h/2);
}

float to_rad(float deg){
  return deg*(PI/180);
}

float to_deg(float rad){
  return rad*(180/PI);
}

void draw_triangle(int cx,int cy,int r,int deg){
  //deg = 180;
  triangle(cx+r*cos*1,r*sin*2+cy,
           cx+r*cos*3,cy+r*sin*4,
           cx+r*cos*5,cy+r*sin*6
          );
}

void keyPressed(){
  if(keyCode==ENTER){
    String fname = str(day())+str(minute())+str(second())+str(millis())+".png";
    save(fname);

    PGraphics pg = createGraphics(g_w,g_h);
    pg.beginDraw();
    pg.tint(0,0);
    PImage img = loadImage(fname);
    img.loadPixels();
    for(int j=0;j<g_w;j++){
      for(int k=0;k<g_h;k++){
        int c = img.get(j,k);
        if(!compare_color(c,g_black)){
          pg.fill(c);
          pg.stroke(c);
          pg.square(j,k,1);
        }          
      }
    }
    pg.endDraw();      
    pg.save("trans"+fname);
  }
}




boolean compare_color(int a,int b){
  return (red(a)==red(b) && green(a)==green(b) && blue(a)==blue(b));
}

*1:90+deg)*(PI/180

*2:90+deg)*(PI/180

*3:210+deg)*(PI/180

*4:210+deg)*(PI/180

*5:330+deg)*(PI/180

*6:330+deg)*(PI/180