Advertisement
Chris_M_Thomasson

Crude Chaos Game For Rick

Mar 21st, 2016
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <cairo/cairo.h>
  5.  
  6.  
  7. #define PI 3.141592653589793
  8. #define PI2 (PI * 2.0)
  9. #define BASE_POINTS 3
  10. #define SCALE 0.5
  11. #define IMAX 100000
  12. #define RAND_MOD(mp_mod) (rand() % (mp_mod))
  13. #define RAND_FLOAT() (rand() / (RAND_MAX - 0.0))
  14.  
  15.  
  16. struct point2d
  17. {
  18.     double x;
  19.     double y;
  20. };
  21.  
  22. struct color
  23. {
  24.     double r;
  25.     double b;
  26.     double g;
  27.     double a;
  28. };
  29.  
  30.  
  31. void
  32. render_circle(
  33.     cairo_t* const cr,
  34. struct point2d cp,
  35.     double r,
  36. struct color c
  37.     ) {
  38.     cairo_new_path(cr);
  39.     cairo_arc(cr, cp.x, cp.y, r, 0, PI2);
  40.     cairo_set_source_rgba(cr, c.r, c.g, c.b, c.a);
  41.     cairo_set_line_width(cr, 1.0);
  42.     cairo_stroke(cr);
  43. }
  44.  
  45.  
  46. struct point2d
  47.     get_scaled_point(
  48. struct point2d p0,
  49. struct point2d p1,
  50.     double scale
  51.     ) {
  52.     struct point2d spt = {
  53.         p0.x + (p1.x - p0.x) * scale,
  54.         p0.y + (p1.y - p0.y) * scale
  55.     };
  56.  
  57.     return spt;
  58. }
  59.  
  60.  
  61. void
  62. chaos_game_init(
  63. struct point2d* result,
  64.     size_t depth
  65.     ) {
  66.     double astep = PI2 / depth;
  67.  
  68.     for (size_t i = 0; i < depth; ++i)
  69.     {
  70.         double angle = astep * i;
  71.  
  72.         result[i].x = cos(angle);
  73.         result[i].y = sin(angle);
  74.     }
  75. }
  76.  
  77.  
  78. void
  79. chaos_game_plot(
  80.     cairo_t* const cr,
  81. struct point2d origin,
  82.     double radius,
  83. struct point2d* points,
  84.     size_t depth,
  85.     size_t imax,
  86.     double scale
  87.     ) {
  88.     struct point2d z = {
  89.         -1.0 + RAND_FLOAT(),
  90.         -1.0 + RAND_FLOAT()
  91.     };
  92.  
  93.     for (size_t i = 0; i < imax + 100; ++i)
  94.     {
  95.         struct point2d* rpt = &points[RAND_MOD(depth)];
  96.         struct point2d npt = get_scaled_point(*rpt, z, scale);
  97.  
  98.         struct point2d ppt = {
  99.             npt.x * radius + origin.x,
  100.             npt.y * radius + origin.y
  101.         };
  102.  
  103.         if (i >= 100)
  104.         {
  105.             render_circle(cr, ppt, 1.0, (struct color) { 1.0, 1.0, 1.0, 1.0 });
  106.         }
  107.  
  108.         printf("computing iteration: %lu of %lu\r", (unsigned long)i, (unsigned long)(imax + 100));
  109.  
  110.         z = npt;
  111.     }
  112.  
  113.     puts("\nComplete!");
  114. }
  115.  
  116.  
  117.  
  118.  
  119. #define OUTPUT "C:\\Users\\Chris\\Desktop\\ct_crude_chaos_game_p0.png"
  120.  
  121. int main(void)
  122. {
  123.     srand(12345678);
  124.  
  125.     {
  126.         unsigned int width = 640;
  127.         unsigned int height = 640;
  128.  
  129.         cairo_surface_t *surface = NULL;
  130.         cairo_t *cr = NULL;
  131.  
  132.         surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
  133.         cr = cairo_create(surface);
  134.  
  135.         cairo_rectangle(cr, 0.0, 0.0, width, height);
  136.         cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
  137.         cairo_fill(cr);
  138.  
  139.         {
  140.             struct point2d points[BASE_POINTS] = { { 0.0 } };
  141.             struct point2d origin = { width / 2.0, height / 2.0 };
  142.             double radius = (height / 2.0) - 2.0;
  143.  
  144.             render_circle(
  145.                 cr,
  146.                 origin,
  147.                 radius,
  148.                 (struct color) {
  149.                 1.0, 0.0, 0.0, 1.0
  150.             }
  151.             );
  152.  
  153.             chaos_game_init(points, BASE_POINTS);
  154.             chaos_game_plot(cr, origin, radius, points, BASE_POINTS, IMAX, SCALE);
  155.         }
  156.  
  157.         cairo_surface_write_to_png(surface, OUTPUT);
  158.         cairo_destroy(cr);
  159.         cairo_surface_destroy(surface);
  160.  
  161.         //system("mspaint " OUTPUT);
  162.     }
  163.  
  164.     //puts("\nProgram Complete!");
  165.     //getchar();
  166.  
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement