Skip to content

Instantly share code, notes, and snippets.

@EDDxample
Created June 6, 2019 12:39
Show Gist options
  • Save EDDxample/b8513d139e46099fdb4b47d7610c323a to your computer and use it in GitHub Desktop.
Save EDDxample/b8513d139e46099fdb4b47d7610c323a to your computer and use it in GitHub Desktop.
Lattice Renderer: 3b1b-like lattice transformation
int FRAME = 0;
int ROWS = 50;
float SCALE = 50;
void setup() {
size(854, 480);
}
void draw() {
background(0);
translate(width/2, height - height/5);
scale(1,-1);
grid(true);
grid(false);
lattice();
stroke(255,0,0);
drawline(0, 0, SCALE, 0, false);
stroke(0,255,0);
drawline(0, 0, 0, SCALE, false);
stroke(255,0,255);
drawline(0, 0, SCALE*2, SCALE*2, false);
stroke(255,0,255);
drawline(0, 0, SCALE*-5, SCALE*5, false);
if (FRAME > 50) saveFrame("frames/lcg_###");
FRAME += 1;
}
static final int lcg(int seed) {
int A = 11, B = 0, C = 20;
int next = (A*seed + B) % C;
return next < 0 ? next + C : next;
}
void lattice() {
noStroke();
for (int x = -ROWS; x <= ROWS; ++x) {
drawDot(x*SCALE, lcg(x)*SCALE);
}
}
void grid(boolean flag) {
if (flag) stroke(128);
else stroke(43,138,177);
for (int x = -ROWS; x <= ROWS; ++x) {
drawline(x*SCALE, -ROWS*SCALE, x*SCALE, ROWS*SCALE, flag);
drawline(-ROWS*SCALE, x*SCALE, ROWS*SCALE, x*SCALE, flag);
}
if (flag) stroke(130);
else stroke(255);
drawline(0, -ROWS*SCALE, 0, ROWS*SCALE, flag);
drawline(-ROWS*SCALE, 0, ROWS*SCALE, 0, flag);
}
void drawDot(float x, float y) {
float[] dot = transform(x,y);
fill(255, 255, 0);
int radius = 10;
ellipse(dot[0], dot[1], radius, radius);
}
void drawline(float x, float y, float x1, float y1, boolean flag) {
if (flag) line(x, y, x1, y1);
float[]
t = transform(x,y);
x = t[0];
y = t[1];
t = transform(x1, y1);
x1 = t[0];
y1 = t[1];
line(x, y, x1, y1);
}
float[] transform(float x, float y) {
float[] result = {x, y};
int delay = 50, cap = 90;
if (FRAME <= 50) return result;
FRAME = FRAME-delay > cap ? cap+delay : FRAME;
float c = cos(radians(FRAME - 50)),
s = sin(radians(FRAME - 50));
float[][] m = {
//{ c, s}, {-s, c}
//{0.5,0},{-1,1}
//{1,0},{0,1/5.0}
//{0.5,1},{-1,1/5.}
//{1,0},{0,1}
{.25, .25},{-.1, .1}
};
float
x1 = x * m[0][0] + y * m[0][1],
y1 = x * m[1][0] + y * m[1][1];
result[0] = x + (x1 - x) * s;
result[1] = y + (y1 - y) * s;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment