This article will provide a complete C-language code that produces a fractal image file, and is extremely simple. I believe this is the simplest algorithm for fractal image generation. Most of the fractal image code is also very short, but a recursive iteration is difficult to understand. This code is understood, and the resulting image can be unexpected.
#include <iostream>#include<cmath>#include<cstdlib>#defineDIM 1000voidPixel_write (int,int); FILE*FP;intMain () {FP= fopen ("image.ppm","WB"); if(!FP) { return-1; } fprintf (FP,"p6\n%d%d\n255\n", Dim, dim); for(intj=0; j<dim;j++) { for(intI=0; i<dim;i++) {pixel_write (i,j); }} fclose (FP); return 0;}voidPixel_write (intIintj) { StaticUnsignedCharcolor[3]; floatT = j + i*0.001f; memcpy (color,&t,3); Fwrite (color,1,3, FP); //in fact, the more simple way to explode is//fwrite (&t, 1, 3, FP);}
When the code finishes running, it generates an image file in ppm format:
The simplest fractal figure should be a Cantor (Cantor) three-part set, just to keep the segments in Split. As shown:
The algorithm generates images that are somewhat similar to the Cantor three-part set. Especially its green channel:
By modifying the code, the fractal effect is more pronounced:
void pixel_write (intint j) { staticchar color[3 ]; float t = j/4 + i*0.001f; 3 ); 1 3 , FP);}
Related articles:
C language generates an image from 0 to 1000 floating-point numbers in a way that forces pointer-type conversions
The simplest algorithm for fractal image generation