Created
June 2, 2020 00:42
-
-
Save marcedwards/fc137aa377b079e8dc156c079042e385 to your computer and use it in GitHub Desktop.
Zigzags 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
// | |
// Zigzags. | |
// Created using Processing 3.5.4. | |
// | |
// Code by @marcedwards from @bjango. | |
// | |
void setup() { | |
size(400, 400, P2D); | |
frameRate(30); | |
smooth(8); | |
noStroke(); | |
fill(#ffffff); | |
blendMode(ADD); | |
} | |
void draw() { | |
background(0); | |
float step = TAU / 24.0; | |
float spiralness = 0.05; | |
fill(#ff0000); | |
for (float j = 0; j < TAU; j += step) { | |
beginShape(); | |
vertex(width / 2, height / 2); | |
for (int i = 0; i < 30; i++) { | |
setVertex(i, j + i * spiralness, 0); | |
} | |
for (int i = 30; i > 0; i--) { | |
setVertex(i, j + i * spiralness, step); | |
} | |
endShape(); | |
} | |
fill(#00ffff); | |
for (float j = 0.015; j < TAU + 0.015; j += step) { | |
beginShape(); | |
vertex(width / 2, height / 2); | |
for (int i = 0; i < 30; i++) { | |
setVertex(i, j + i * spiralness, 0); | |
} | |
for (int i = 30; i > 0; i--) { | |
setVertex(i, j + i * spiralness, step); | |
} | |
endShape(); | |
} | |
} | |
void setVertex(int i, float j, float step) { | |
float size = 10 * i; | |
float zipscale = Ease.hermite5(Ease.tri(gradientDiamond(cos(j) * size + width / 2, cos(j) * size + height / 2, timeLoop(120))), 2) * 0.15; | |
float a = (i % 2) * zipscale + j + (step * 0.5); | |
float x = cos(a) * size; | |
float y = sin(a) * size; | |
vertex(x + width / 2, y + height / 2); | |
} | |
float timeLoop(float totalframes) { | |
return frameCount % totalframes / totalframes; | |
} | |
float gradientDiamond(float x, float y, float offset) { | |
float biggest = max(width, height); | |
float xd = abs((width / 2) - x); | |
float yd = abs((height / 2) - y); | |
return wrap((xd + yd) / biggest, 1 - offset); | |
} | |
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); | |
} | |
} | |
float wrap(float value, float offset) { | |
return (value + offset) % 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment