Skip to content

Instantly share code, notes, and snippets.

@RemyPorter
Last active April 12, 2021 18:09
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 RemyPorter/47f59e796f6d24c7dcc49cafe50e3497 to your computer and use it in GitHub Desktop.
Save RemyPorter/47f59e796f6d24c7dcc49cafe50e3497 to your computer and use it in GitHub Desktop.
A math-y CGA generator. Use lots of bitwise operations in `f`, use `p` and `d` to convert to polar coordinates.
boolean save = true //set to false while prototyping
int palettes[][] = {
{#000000, #00AA00, #AA0000, #AA5500},
{#000000, #55FF55, #FF5555, #FFFF55},
{#000000, #00AAAA, #AA00AA, #AAAAAA},
{#000000, #55FFFF, #FF55FF, #FFFFFF},
{#000000, #00AAAA, #AA0000, #AAAAAA},
{#000000, #55FFFF, #FF5555, #FFFFFF}
};
//pick one of the above palettes
int palette[] = palettes[5];
void setup() {
size(320,200,P3D);
}
//this is the function you edit.
int f(int x, int y, int t) {
if (x < 0 || y < 0 || t < 0) return 0;
int val = 0;
val ^= p(t^p(x,y^t>>2),t>>2);
return abs(
val
) % palette.length;
}
//integral sine function, scales the range to positive, and the same number of units as our palette
int sine(float x) {
return ((int)abs(sin(x)*palette.length));
}
//integral power function
int pow(int x, int y) {
return (int)Math.pow(x, y);
}
//polar angle relative to center, scaled to have the same number of units as our palette length
int p(int x, int y) {
return (int)((atan2(y-height/2, x-width/2)+PI) * 2 * PI * palette.length);
}
//distance from center
int d(int x, int y) {
return (int)(dist(x, y, width/2, height/2));
}
int sz = 1;
int loopCount = 0;
void draw() {
background(0);
if (frameCount > 512) exit();
noStroke();
for (int x = 0; x < width; x += sz) {
for (int y = 0; y < height; y += sz) {
fill(palette[f(x,y,frameCount)]);
rect(x, y, sz, sz);
}
}
if (save) {
saveFrame("frames/####.tiff");
}
}
//generate a palette from the frames with ffmpeg, using: `ffmpeg -i frames/%4d.tiff -vf palettegen=8 palette.png`
//then create a gif with that palette: `ffmpeg -i frames/%4d.tiff -i palette.png -filter_complex "paletteuse" output.gif`
//while the images are all 4 colors, I found letting FFMPEG generate a 4 color palette wouldn't be true output, so I went up to 8.
//if you figure out 4, it'll have smaller file sizes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment