Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created January 26, 2014 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanorelli/8639819 to your computer and use it in GitHub Desktop.
Save jordanorelli/8639819 to your computer and use it in GitHub Desktop.
Pulse
RegularGroup regulars;
boolean enableGuides = false;
boolean RECORD = true;
void setup() {
size(500, 500);
background(255);
regulars = new RegularGroup(width*0.5, height*0.5, 16, 6);
}
void draw() {
background(255);
guides();
regulars.draw();
export();
}
void guides() {
if (!enableGuides) {
return;
}
strokeWeight(1);
stroke(0);
line(width*0.5, 0, width*0.5, height);
line(0, height*0.5, width, height*0.5);
}
void export() {
if (!RECORD || frameCount >= 200) {
return;
}
if (frameCount % 2 != 0) {
return;
}
String fname = nf((frameCount), 3) + ".png";
println(fname);
save(fname);
}
float Regular__Jitter = 0;
int Regular__PulseLifetime = 60;
class Regular {
private int faces;
private float size;
private PVector origin;
private int pulseFrame = -1;
Regular(float x, float y, int faces, float size) {
this.origin = new PVector(x, y);
this.faces = faces;
this.size = size;
}
public void draw() {
float x;
float y;
float t = TWO_PI / this.faces;
pushMatrix();
translate(this.origin.x, this.origin.y);
setStyles();
beginShape();
for (int i = 0; i < this.faces; i++) {
x = size * cos(float(i) * t);
y = size * sin(float(i) * t);
vertex(x, y);
}
endShape(CLOSE);
popMatrix();
}
public void pulse() {
this.pulseFrame = frameCount;
}
private void setStyles() {
stroke(0);
strokeWeight(this.getWeight());
noFill();
}
private float getWeight() {
if (this.pulseFrame == -1) {
return 1.0;
}
int frameDiff = frameCount - pulseFrame;
frameDiff = min(frameDiff, Regular__PulseLifetime);
// when frameDiff is Regular__PulseLifetime, we should have an n of 0
float n = norm(frameDiff, Regular__PulseLifetime, 0);
rotate(TWO_PI * n * n / this.faces);
return max(1.0, n * n * 16.0);
}
}
class RegularGroup {
private ArrayList<Regular> shapes;
private int highlight;
RegularGroup(float x, float y, int n, int faces) {
this.highlight = -1;
this.shapes = new ArrayList<Regular>();
for (int i = 1; i <= n; i++) {
this.shapes.add(new Regular(x, y, faces, i * 15));
}
}
void draw() {
this.updateHighlight();
for (Regular shape : this.shapes) {
shape.draw();
}
}
private void updateHighlight() {
if (frameCount % 8 != 0) {
return;
}
this.highlight = (this.highlight + 1) % (this.shapes.size() + 8);
if (this.highlight >= this.shapes.size()) {
return;
}
Regular reg = this.shapes.get(this.highlight);
reg.pulse();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment