Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <cairo/cairo.h>
- #define PI 3.141592653589793
- #define PI2 (PI * 2.0)
- #define BASE_POINTS 3
- #define SCALE 0.5
- #define IMAX 100000
- #define RAND_MOD(mp_mod) (rand() % (mp_mod))
- #define RAND_FLOAT() (rand() / (RAND_MAX - 0.0))
- struct point2d
- {
- double x;
- double y;
- };
- struct color
- {
- double r;
- double b;
- double g;
- double a;
- };
- void
- render_circle(
- cairo_t* const cr,
- struct point2d cp,
- double r,
- struct color c
- ) {
- cairo_new_path(cr);
- cairo_arc(cr, cp.x, cp.y, r, 0, PI2);
- cairo_set_source_rgba(cr, c.r, c.g, c.b, c.a);
- cairo_set_line_width(cr, 1.0);
- cairo_stroke(cr);
- }
- struct point2d
- get_scaled_point(
- struct point2d p0,
- struct point2d p1,
- double scale
- ) {
- struct point2d spt = {
- p0.x + (p1.x - p0.x) * scale,
- p0.y + (p1.y - p0.y) * scale
- };
- return spt;
- }
- void
- chaos_game_init(
- struct point2d* result,
- size_t depth
- ) {
- double astep = PI2 / depth;
- for (size_t i = 0; i < depth; ++i)
- {
- double angle = astep * i;
- result[i].x = cos(angle);
- result[i].y = sin(angle);
- }
- }
- void
- chaos_game_plot(
- cairo_t* const cr,
- struct point2d origin,
- double radius,
- struct point2d* points,
- size_t depth,
- size_t imax,
- double scale
- ) {
- struct point2d z = {
- -1.0 + RAND_FLOAT(),
- -1.0 + RAND_FLOAT()
- };
- for (size_t i = 0; i < imax + 100; ++i)
- {
- struct point2d* rpt = &points[RAND_MOD(depth)];
- struct point2d npt = get_scaled_point(*rpt, z, scale);
- struct point2d ppt = {
- npt.x * radius + origin.x,
- npt.y * radius + origin.y
- };
- if (i >= 100)
- {
- render_circle(cr, ppt, 1.0, (struct color) { 1.0, 1.0, 1.0, 1.0 });
- }
- printf("computing iteration: %lu of %lu\r", (unsigned long)i, (unsigned long)(imax + 100));
- z = npt;
- }
- puts("\nComplete!");
- }
- #define OUTPUT "C:\\Users\\Chris\\Desktop\\ct_crude_chaos_game_p0.png"
- int main(void)
- {
- srand(12345678);
- {
- unsigned int width = 640;
- unsigned int height = 640;
- cairo_surface_t *surface = NULL;
- cairo_t *cr = NULL;
- surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
- cr = cairo_create(surface);
- cairo_rectangle(cr, 0.0, 0.0, width, height);
- cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
- cairo_fill(cr);
- {
- struct point2d points[BASE_POINTS] = { { 0.0 } };
- struct point2d origin = { width / 2.0, height / 2.0 };
- double radius = (height / 2.0) - 2.0;
- render_circle(
- cr,
- origin,
- radius,
- (struct color) {
- 1.0, 0.0, 0.0, 1.0
- }
- );
- chaos_game_init(points, BASE_POINTS);
- chaos_game_plot(cr, origin, radius, points, BASE_POINTS, IMAX, SCALE);
- }
- cairo_surface_write_to_png(surface, OUTPUT);
- cairo_destroy(cr);
- cairo_surface_destroy(surface);
- //system("mspaint " OUTPUT);
- }
- //puts("\nProgram Complete!");
- //getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement