Skip to content

Instantly share code, notes, and snippets.

@takawo
Last active December 25, 2015 00:11
Show Gist options
  • Save takawo/c81d5b63a085e0759f3e to your computer and use it in GitHub Desktop.
Save takawo/c81d5b63a085e0759f3e to your computer and use it in GitHub Desktop.
float grid = 50;
ArrayList<Triangle> triangles = new ArrayList<Triangle>();
void setup() {
size(960, 540);
colorMode(HSB, 360, 100, 100, 100);
background(0, 0, 100);
for (float y= 0; y <= height+grid; y += grid) {
for (float x= 0; x <= width; x += grid) {
int r = int(random(1, 3));
for (int i = 0; i < r; i++) {
triangles.add(new Triangle(x, y, grid/2));
}
}
}
}
void draw() {
noStroke();
fill(0, 0, 100, 10);
rect(0, 0, width, height);
//background(0, 0, 100);
stroke(0, 0, 0);
fill(0, 0, 100);
for (Triangle t : triangles) {
t.update();
t.draw();
}
}
class Triangle {
PVector p1;
PVector p2;
PVector p3;
PVector tp1;
PVector tp2;
PVector tp3;
PVector center;
float grid;
float easing = int(random(1, 5))*0.01;
color c;
color tc;
int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 8};
public Triangle(float x, float y, float grid) {
center = new PVector(x, y);
this.grid = grid;
//配列のシャッフル
for (int i=(arr.length-1); i>0; --i) {
int j = (int)random(i+1);
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
p1 = setPoint(arr[0]);
p2 = setPoint(arr[1]);
p3 = setPoint(arr[2]);
tp1 = setPoint(arr[3]);
tp2 = setPoint(arr[4]);
tp3 = setPoint(arr[5]);
c = color(random(360), 80, 100, 50);
tc = color(random(360), 80, 100, 50);
}
PVector setPoint(int i) {
PVector p;
switch(i) {
case 0:
p = new PVector(center.x-grid, center.y-grid);
break;
case 1:
p = new PVector(center.x, center.y-grid);
break;
case 2:
p = new PVector(center.x+grid, center.y-grid);
break;
case 3:
p = new PVector(center.x-grid, center.y);
break;
case 4:
p = new PVector(center.x, center.y);
break;
case 5:
p = new PVector(center.x+grid, center.y);
break;
case 6:
p = new PVector(center.x-grid, center.y+grid);
break;
case 7:
p = new PVector(center.x, center.y+grid);
break;
case 8:
p = new PVector(center.x+grid, center.y+grid);
break;
default:
p = new PVector(center.x, center.y);
break;
}
return p;
}
void update() {
c = lerpColor(c, tc, easing);
if ((PVector.dist(p1, tp1) < 0.5) || (PVector.dist(p2, tp2) < 0.5) || (PVector.dist(p3, tp3) < 0.5)) {
tc = color(random(360), 80, 100, 50);
easing = int(random(1, 5))*0.02;
}
p1 = PVector.lerp(p1, tp1, easing);
p2 = PVector.lerp(p2, tp2, easing);
p3 = PVector.lerp(p3, tp3, easing);
if (PVector.dist(p1, tp1) < 0.5) {
tp1 = setPoint(arr[int(random(9))]);
}
if (PVector.dist(p2, tp2) < 0.5) {
tp2 = setPoint(arr[int(random(9))]);
}
if (PVector.dist(p3, tp3) < 0.5) {
tp3 = setPoint(arr[int(random(9))]);
}
}
void draw() {
fill(c);
noStroke();
triangle(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment