Skip to content

Instantly share code, notes, and snippets.

@peternewman22
Created July 30, 2021 06:45
Show Gist options
  • Save peternewman22/ee52a91fc1372c64da48b12c9faecfee to your computer and use it in GitHub Desktop.
Save peternewman22/ee52a91fc1372c64da48b12c9faecfee to your computer and use it in GitHub Desktop.
Reusing some tiling code and experimenting with triggering a cycle in each hex to get the ripple effect
class Hex {
PVector pos;
int state;
float a; // tracking point in the cycle
float thisR; // current radius
float dist2c;
Hex(float x, float y) {
pos = new PVector(x, y);
state = 1;
thisR = bigR;
dist2c = PVector.dist(c, pos);
}
void updateHex(){
//state 1 is checking if it should start - could be set to a counter?
if(state == 1 && dist2c < rippleR){
state = 2;
} else if(state == 2){
thisR = map(cos(a), -1, 1, 0.8*bigR, bigR); // scale the hex between 80% and 100% of full
a+=frameDuration; // makes sure it takes "frames" to get through the cycle
if(a >= TWO_PI){
a = 0;
state = 1; // marks the beginning of the cycle
}
}
}
void showHex() {
int opacity = int(map(thisR, 0.8*bigR, bigR,75,255)); // setting the opacity by the current radius
fill(0,0,255,opacity);
stroke(0);
strokeWeight(2);
// drawing the actual hex
beginShape();
for (int i = 0; i < 6; i++) {
vertex(pos.x + thisR*cos(i*TWO_PI/6), pos.y + thisR*sin(i*TWO_PI/6));
}
endShape(CLOSE);
}
}
float bigR, sep, r, rippleR, maxR;
Hex[][] grid;
int visCount, count;
float yOff;
int frames = 180;
float rIncrement;
boolean isPlaying;
PVector[][] corners;
PVector c;
float frameDuration;
boolean isRecording;
void setup() {
size(500, 585);
isPlaying = false;
noLoop();
noFill();
stroke(0);
c = new PVector(width/2, height/2); // getting the centre of the screen
count = 16;
visCount = count - 1; //some hexes appear off screen
rippleR = 1;
maxR = height/sqrt(2); // really should be the distance to the furthermost hex centre...
//locking the cycles of ripples going out to the cycle of the hexes
//note:
frameDuration = TWO_PI/frames;
rIncrement = maxR/frames;
// establishing the size of the hexes
sep = width/(visCount-1);
bigR = sep/1.5; // circumscribed radius
r = bigR*cos(PI/6); //inscribed radius
//yOff = spacing/2*cos(PI/6);
textAlign(CENTER); // for debugging purposes
grid = new Hex[count][count];
initHexGrid();
isRecording = false;
}
void draw() {
background(0);
drawHexes();
noFill();
// ellipse(width/2, height/2, 2*rippleR, 2*rippleR); //show the ripple
if(rippleR < maxR){
rippleR += rIncrement;
}
// hacky code to get the loop - I want to start this after the first wave has gone out
if(frameCount > 10 && grid[8][8].state == 1){
isRecording = !isRecording;
}
if(isRecording){
saveFrame("output/gif-####.png");
}
if(frameCount > 200 && grid[8][8].state == 1){
noLoop(); // stop recording
}
}
void initHexGrid(){
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
if (i%2 == 1) {
grid[i][j] = new Hex((i-1)*bigR*1.5, (j+0.5 - 1)*2*r);
} else {
grid[i][j] = new Hex((i - 1)*bigR*1.5, (j - 1)*2*r);
}
}
}
}
void drawHexes() {
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
grid[i][j].updateHex();
grid[i][j].showHex();
}
}
}
// so I can stop and start the animation for screen capturing purposes
void keyPressed(){
if(keyCode == ' '){
isPlaying = !isPlaying;
}
if(isPlaying){
loop();
} else{
noLoop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment