Last active
December 17, 2019 10:44
-
-
Save marcedwards/0334f40591331659c9169a5a9d707e73 to your computer and use it in GitHub Desktop.
A zigzag-y spiral thing made in Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Zigzag spiral. | |
// Created using Processing 3.5.3. | |
// | |
// Code by @marcedwards from @bjango. | |
// | |
// A GIF of this code can be seen here: | |
// https://twitter.com/marcedwards/status/1206887873338691584 | |
// | |
void setup() { | |
size(500, 500, P2D); | |
frameRate(30); | |
smooth(2); | |
noStroke(); | |
fill(#ffffff); | |
blendMode(ADD); | |
} | |
void draw() { | |
background(0); | |
drawSpiral(#ff0000, 0.0); | |
drawSpiral(#00ffff, 0.1); | |
} | |
void drawSpiral(color col, float offset) { | |
float step = TAU / 14.0; | |
float precision = 0.5; | |
fill(col); | |
beginShape(); | |
vertex(width / 2, height / 2); | |
for (float j = 0; j < TAU; j += step) { | |
for (float rad = 0; rad < width; rad += precision) { | |
setVector(j + (0.001 * rad), rad, 0 + offset); | |
} | |
for (float rad = width; rad > 0; rad -= precision) { | |
setVector(j + (0.001 * rad), rad, step + offset); | |
} | |
} | |
endShape(); | |
} | |
void setVector(float j, float rad, float step) { | |
float spiralness = 0.0001; | |
float wavefreq = 40.0; | |
float waveamp = 2.0; | |
float angle = j + (rad * spiralness) + step * 0.5; | |
float anglealt = ((rad / wavefreq) % 1.0) * waveamp; | |
float x = cos(angle) * rad + width / 2; | |
float y = sin(angle) * rad + height / 2; | |
float amount = Ease.hermite5(Ease.tri(gradientSpiral(x, y, timeLoop(240), 0.6), 2)) * 0.15; | |
float finalangle = lerp(angle, angle + anglealt, amount); | |
x = cos(finalangle) * rad + width / 2; | |
y = sin(finalangle) * rad + height / 2; | |
vertex(x, y); | |
} | |
float timeLoop(float totalframes, float offset) { | |
return (frameCount + offset) % totalframes / totalframes; | |
} | |
float timeLoop(float totalframes) { | |
return timeLoop(totalframes, 0); | |
} | |
float gradientSpiral(float x, float y, float offset, float frequency) { | |
float xc = width / 2; | |
float yc = height / 2; | |
float normalisedRadius = length(x - xc, y - yc) / max(xc, yc); | |
float plotAngle = atan2(y - yc, x - xc); | |
float waveAngle = normalisedRadius * TAU * frequency; | |
return wrap(radWrap(plotAngle + waveAngle) / TAU, 1 - offset); | |
} | |
float length(float x, float y) { | |
return sqrt(x * x + y * y); | |
} | |
float radWrap(float rad) { | |
float r = rad % TAU; | |
return r < 0 ? r + TAU : r; | |
} | |
float wrap(float value, float offset) { | |
return (value + offset) % 1; | |
} | |
static class Ease { | |
static public float hermite5(float t) { | |
return t * t * t * (t * (t * 6 - 15) + 10); | |
} | |
static public float hermite5(float t, int repeat) { | |
for (int i = 0; i < repeat; i++) { | |
t = hermite5(t); | |
} | |
return t; | |
} | |
static public float tri(float t, float repeat) { | |
return t * repeat * 2 % 2 <= 1 ? t * repeat * 2 % 2 : 2 - (t * repeat * 2 % 2); | |
} | |
static public float tri(float t) { | |
return t < 0.5 ? t * 2 : 2 - (t * 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment