Last active
September 4, 2015 02:44
-
-
Save gregnavis/250994 to your computer and use it in GitHub Desktop.
A simple generator of Clifford attractors. Issue `make` to build it (requires `libnetpbm10-dev` on Debian).
This file contains hidden or 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 <math.h> | |
#include <ppm.h> | |
int render_clifford(pixel **img, int width, int height, double a, double b, double c, double d) | |
{ | |
int i, x, y, max = 0; | |
double x0 = 0.0, y0 = 0.0, x1, y1; | |
for(y = 0; y < height; y++) | |
{ | |
for(x = 0; x < width; x++) | |
{ | |
img[y][x].r = 0; | |
img[y][x].g = 0; | |
img[y][x].b = 0; | |
} | |
} | |
for(i = 0; i < 15 * width * height; ++i) | |
{ | |
x1 = sin(a * y0) + c * cos(a * x0); | |
y1 = sin(b * x0) + d * cos(b * y0); | |
x0 = x1; | |
y0 = y1; | |
x = width * (1 + fabs(c) + x1) / (2.0 + 2.0 * fabs(c)); | |
y = height * (1 + fabs(d) + y1) / (2.0 + 2.0 * fabs(d)); | |
if(img[y][x].r < PGM_MAXMAXVAL) | |
{ | |
img[y][x].r++; | |
img[y][x].g++; | |
img[y][x].b++; | |
} | |
if(img[y][x].r > max) | |
{ | |
max = img[y][x].r; | |
} | |
} | |
return max; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
double a, b, c, d; | |
int width, height, max; | |
pixel **img; | |
if(argc != 7) | |
{ | |
printf("Usage:\n clifford width height a b c d > output.ppm\n"); | |
return 0; | |
} | |
width = atoi(argv[1]); | |
height = atoi(argv[2]); | |
a = atof(argv[3]); | |
b = atof(argv[4]); | |
c = atof(argv[5]); | |
d = atof(argv[6]); | |
ppm_init(&argc, argv); | |
img = ppm_allocarray(width, height); | |
max = render_clifford(img, width, height, a, b, c, d); | |
ppm_writeppm(stdout, img, width, height, max, 0); | |
ppm_freearray(img, height); | |
return 0; | |
} |
This file contains hidden or 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
CFLAGS=-lm -lnetpbm | |
clifford: clifford.c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment