Skip to content

Instantly share code, notes, and snippets.

@Cairnarvon
Created October 25, 2011 22:41
Show Gist options
  • Save Cairnarvon/1314586 to your computer and use it in GitHub Desktop.
Save Cairnarvon/1314586 to your computer and use it in GitHub Desktop.
What ho, chaos game.
/* gcc -Wall -Wextra `allegro-config --libs` -lm w.c */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <allegro.h>
#define POINTS 3
#define WIDTH 600
#define HEIGHT 600
#define REST 0
#define FNAME "screen.pcx"
int main(int argc, char **argv)
{
struct point { double x, y; } *pts;
int n_pts = POINTS, w = WIDTH, h = HEIGHT, r = REST,
i, white;
double x, y;
char *fname = FNAME;
PALETTE pal;
/* Parse options */
if (argc > 1) n_pts = atoi(argv[1]);
if (argc > 2) {
w = atoi(argv[2]);
if (argc == 3) h = w;
}
if (argc > 3) h = atoi(argv[3]);
if (argc > 4) r = atoi(argv[4]);
if (n_pts < 1 || w < 1 || h < 1 || r < 0 || argc > 5) {
fprintf(stderr,
"USAGE: %s [ N_POINTS [ WIDTH [ HEIGHT [ REST [ FILENAME ] ] ] ] ]\n",
argv[0]);
return 1;
}
/* Initialise Allegro */
allegro_init();
install_timer();
install_keyboard();
set_color_depth(32);
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, w, h, 0, 0) != 0) return 2;
white = makecol(255, 255, 255);
get_palette(pal);
set_trans_blender(0, 0, 0, 10);
drawing_mode(DRAW_MODE_TRANS, 0, 0, 0);
/* Determine attractor points (just on a circle (or ellipse)) */
pts = malloc(sizeof(struct point) * n_pts);
for (i = 0; i < n_pts; ++i) {
pts[i].x = w / 2 + sin(2 * 3.14159 * i / n_pts) * w / 2;
pts[i].y = h / 2 - cos(2 * 3.14159 * i / n_pts) * h / 2;
putpixel(screen, pts[i].x, pts[i].y, makecol(255, 0, 0));
}
/* Random numbers */
srandom((unsigned int)time(NULL));
x = (double)(random() % w);
y = (double)(random() % h);
/* Main loop */
while (!keypressed()) {
int n = random() % n_pts;
putpixel(screen, x, y, white);
x = (x + pts[n].x) / 2;
y = (y + pts[n].y) / 2;
rest(r);
}
/* Save bitmap */
save_pcx(fname, screen, pal);
/* Unnecessary */
free(pts);
return 0;
} END_OF_MAIN()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment