Generative Art #5
星使った絵が描きたくなって,円が重ならないように敷き詰めるアルゴリズム使って星を敷き詰めた.この手の反復計算をp5.jsで書くとめっちゃ重くなるのでProcessingだと雑に書きやすくてよい.
final int MAX_TRIAL = 1000;
int[] nVerticesList;
Star[] starList;
color[] palette;
void setup() {
size(800, 800);
nVerticesList = new int[] {5, 8, 9, 12};
palette = selectPalette();
initStarList();
noLoop();
}
void initStarList() {
starList = new Star[100];
for (int k = 0; k < starList.length; k++) {
int trial = 0;
while (trial < MAX_TRIAL) {
int n = nVerticesList[(int)random(nVerticesList.length)];
float r = random(24.0, 256.0);
float x = random(-width / 2.0, width / 2.0);
float y = random(-height / 2.0, height / 2.0);
Star newStar = new Star(n, new PVector(x, y), r);
if (!intersects(starList, newStar)) {
starList[k] = newStar;
break;
}
trial++;
}
if (trial == MAX_TRIAL) {
starList[k] = new Star(0, new PVector(0.0, 0.0), -1.0);
}
}
}
void draw() {
translate(width / 2.0, height / 2.0);
rotate(TWO_PI / (int)random(13));
background(24);
strokeWeight(1.5);
for (Star star : starList) {
stroke(palette[(int)random(palette.length)]);
star.draw();
}
filter(BLUR, 0.7);
}
void mousePressed() {
initStarList();
palette = selectPalette();
redraw();
}
void keyReleased() {
saveFrame(String.format("frames/%s", timestamp("Project", ".png")));
}
static final String timestamp(final String name, final String ext) {
return name + "-" + year() + nf(month(), 2) + nf(day(), 2) +
"-" + nf(hour(), 2) + nf(minute(), 2) + nf(second(), 2) + ext;
}
color[] selectPalette() {
color[][] palettes = new color[][] {
{color(0xFF, 0xCB, 0x3B), color(0x2E, 0xEF, 0xFF), color(0xE3, 0x76, 0x6B)},
{color(0xFB, 0xF8, 0x8B), color(0xF6, 0xD0, 0x26), color(0xEE, 0x66, 0x00)},
{color(0x55, 0xD0, 0xA9), color(0xFA, 0xF4, 0x21), color(0xEE, 0x66, 0x00)},
{color(0xFB, 0xFA, 0x2C), color(0x1F, 0x0E, 0x96), color(0xDB, 0x22, 0x46)},
{color(0xFF, 0xF4, 0x4B), color(0x00, 0x81, 0xD9), color(0x5D, 0x0E, 0x9F), color(0xF8, 0x55, 0x0D)},
{color(0xFF, 0xF8, 0x23), color(0x00, 0xF1, 0xEC), color(0xFF, 0x2B, 0xDE)},
};
return palettes[(int)random(palettes.length)];
}
class Star {
int n;
PVector pos;
float r;
Star(int n, PVector pos, float r) {
this.n = n;
this.pos = pos;
this.r = r;
}
void draw() {
if (r < 0.0) {
return;
}
pushMatrix();
translate(this.pos.x, this.pos.y);
beginShape(LINES);
for (int k = 0; k < this.n; k++) {
float t1 = TWO_PI * k / (float)this.n;
float t2 = TWO_PI * ((k + this.n - 3) % this.n) / (float)this.n;
float x1 = this.r * cos(t1 - HALF_PI);
float y1 = this.r * sin(t1 - HALF_PI);
float x2 = this.r * cos(t2 - HALF_PI);
float y2 = this.r * sin(t2 - HALF_PI);
vertex(x1, y1);
vertex(x2, y2);
}
endShape();
popMatrix();
}
}
boolean intersects(Star[] stars, Star newStar) {
for (Star star : stars) {
if (star != null) {
PVector diffVec = PVector.sub(star.pos, newStar.pos);
float d = diffVec.mag();
if (d < star.r + newStar.r) {
return true;
}
}
}
return false;
}
}
class Star {
int n;
PVector pos;
float r;
Star(int n, PVector pos, float r) {
this.n = n;
this.pos = pos;
this.r = r;
}
void draw() {
if (r < 0.0) {
return;
}
pushMatrix();
translate(this.pos.x, this.pos.y);
beginShape(LINES);
for (int k = 0; k < this.n; k++) {
float t1 = TWO_PI * k / (float)this.n;
float t2 = TWO_PI * ((k + this.n - 3) % this.n) / (float)this.n;
float x1 = this.r * cos(t1 - HALF_PI);
float y1 = this.r * sin(t1 - HALF_PI);
float x2 = this.r * cos(t2 - HALF_PI);
float y2 = this.r * sin(t2 - HALF_PI);
vertex(x1, y1);
vertex(x2, y2);
}
endShape();
popMatrix();
}
}
boolean intersects(Star[] stars, Star newStar) {
for (Star star : stars) {
if (star != null) {
PVector diffVec = PVector.sub(star.pos, newStar.pos);
float d = diffVec.mag();
if (d < star.r + newStar.r) {
return true;
}
}
}
return false;
}
いただいたサポートは主にクリエイターとしての活動費になります