Generative art #111
Code
int count = 5;
int actRandomSeed = (int)random(1000000);
void setup() {
size(840, 840, P2D);
pixelDensity(2);
smooth(8);
noStroke();
}
void draw() {
randomSeed(actRandomSeed);
background(#ffffff);
translate(width/2, height/2);
rotate(random(PI));
randomHex(0, 0, 700, count);
}
void hexagon(float x, float y, float s) {
color c = getCol();
pushMatrix();
translate(x, y);
rotate(radians(30));
rotate(radians(60) * int(random(3)));
beginShape();
for (float a = 0; a < TAU; a += TAU/6) {
float x_ = s*cos(a);
float y_ = s*sin(a);
if (random(1) < 0.1)fill(#ffffff);
else fill(c);
vertex(x_, y_);
}
endShape();
popMatrix();
}
void hexagonRec1(float x, float y, float s, int n) {
hexagon(x, y, s);
n--;
s *= 0.5;
if (n >= 0) {
pushMatrix();
translate(x, y);
rotate(radians(60)*int(random(3)));
randomHex(0, - s, s, n);
randomHex(0, s, s, n);
popMatrix();
}
}
void hexagonRec2(float x, float y, float s, int n) {
hexagon(x, y, s);
n--;
s /= 3;
if (n >= 0) {
randomHex(x, y, s, n);
pushMatrix();
translate(x, y);
for (int i = 0; i < 3; i ++) {
rotate(radians(60));
randomHex(0, - s*2, s, n);
randomHex(0, s*2, s, n);
}
popMatrix();
}
}
void hexagonRec3(float x, float y, float s, int n) {
hexagon(x, y, s);
n--;
s *= 0.5;
if (n >= 0) {
pushMatrix();
translate(x, y);
for (int i = 0; i < 3; i ++) {
rotate(radians(120));
randomHex(0, - s, s, n);
}
popMatrix();
}
}
void randomHex(float x, float y, float s, int n) {
float p = map(n, 0, count, 0.5, 0);
if (random(1) > p) {
if (random(1) > 0.66) {
hexagonRec1(x, y, s, n);
} else if(random(1) > 0.33) {
hexagonRec2(x, y, s, n);
}else{
hexagonRec3(x, y, s, n);
}
}
}
int[] colors = {#F77F7F, #F77FEC, #F7F77F, #F2B736};
int getCol() {
return colors[(int)random(colors.length)];
}
void mousePressed() {
actRandomSeed = (int)random(1000000);
}
void keyPressed() {
if (key == 's')saveFrame("####.png");
}
六角形を分割する方法をこの3パターンからランダムに選ばれるようにした。
三角形を分割させる方法も今まで3パターンぐらいやったと思うのでこの方法やってみたい。
Happy coding!
応援してくださる方!いつでもサポート受け付けてます!