The simplest algorithm for fractal image generation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.