/* Generate the Thue-Morse sequence word as a texture. * * The Thue-Morse sequence is the morphic word generated by the limit * of the L-system * a where a → ab and b → ba * * This code is just copied from fibword.c. * * The first few iterations of this L-system play out as follows: * * a * ab * abba * abbabaab * */ #include "ppmp6.h" enum { ww = 512, hh = 512 }; ppm_p6_color image[hh][ww]; typedef struct { ppm_p6_color *outp; int remaining_pixels; ppm_p6_color palette[256]; } output; static inline int append(output *out, char color) { if (!out->remaining_pixels) return 0; *out->outp++ = out->palette[(int)(unsigned char)color]; out->remaining_pixels--; return 1; } static inline int generate_a(output *out, int maxdepth); static inline int generate_b(output *out, int maxdepth) { if (maxdepth) { return generate_b(out, maxdepth-1) && generate_a(out, maxdepth-1); } return append(out, 'b'); } static inline int generate_a(output *out, int maxdepth) { if (maxdepth) { return generate_a(out, maxdepth-1) && generate_b(out, maxdepth-1); } return append(out, 'a'); } int main(int argc, char **argv) { output out = {image[0], ww*hh}; ppm_p6_color white = { r: 1, g: 1, b: 1 }; out.palette[(int)'a'] = white; generate_a(&out, 100); ppm_p6_output_image(image[0], ww, hh); return 0; }