Created
June 9, 2022 12:27
-
-
Save laserbat/847f48e5bb8cd3d1d2802a4d08395632 to your computer and use it in GitHub Desktop.
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
#include <stdbool.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define W (1080 / 2) | |
#define H (1920 / 2) | |
#define MAX_HEADER 128 | |
static inline uint32_t rule(const uint16_t i, const uint16_t j, const uint32_t world[W][H]) { | |
uint32_t T[9], V = 0; | |
uint32_t m = UINT32_MAX; | |
uint32_t idx = 0; | |
for (int k = 0; k < 9; k++) | |
T[k] = world[(i + (k / 3) + W - 1) % W][(j + (k % 3) + H - 1) % H]; | |
for (int k = 0; k < 9; k++) | |
V |= T[k]; | |
for (int k = 0; k < 9; k++) { | |
uint32_t q = 5 * T[k]; | |
q = (q - V) ^ V; | |
if (q <= m) { | |
m = q; | |
idx = k; | |
} | |
} | |
return T[idx]; | |
} | |
int main(void) { | |
static uint32_t world[2][W][H]; | |
static char frame[W * H * 3 + MAX_HEADER]; | |
char *image; | |
uint16_t i, j, k, flag = 0; | |
int header_len = snprintf(frame, MAX_HEADER, "P6\n%d %d\n255\n", H, W); | |
int frame_len = 3 * W * H + header_len; | |
image = frame + header_len; | |
srand48(time(NULL)); | |
for (i = 0; i < W; i++) | |
for (j = 0; j < H; j++) { | |
world[0][i][j] = lrand48(); | |
} | |
while (1) { | |
for (i = 0; i < W; i++) { | |
for (j = 0; j < H; j++) { | |
uint32_t color; | |
world[!flag][i][j] = rule(i, j, world[flag]); | |
color = world[!flag][i][j]; | |
image[3 * (i * H + j)] = color; | |
image[3 * (i * H + j) + 1] = color >> 8; | |
image[3 * (i * H + j) + 2] = color >> 16; | |
} | |
} | |
flag ^= 1; | |
fwrite(frame, 1, frame_len, stdout); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment