Generation of fractal images (C ++)

Source: Internet
Author: User
Tags getcolor
Recently, I saw the Delphi Program for a lifetime of component graphics on the Internet. The generated graphics are very beautiful, so I changed it to C ++ and improved the generation efficiency.
The Code is as follows (the original code uses the plural operation and is removed for efficiency, but is still kept in the comment)
// This function is used to obtain the color. S is between [0, 1] and the color is continuous.
Colorref getcolor (double S)
{
Int R, G, B;
If (s <= 0.25)
{
G = int (255 * s/0.25 );
Returns RGB (0, G, 255 );
}
Else if (S> 0.25 & S <= 0.5)
{
B = 255-int (255*(s-0.25)/0.25 );
Return RGB (0,255, B );
}
Else if (S> 0.5 & S <= 0.75)
{
R = int (255*(s-0.5)/0.25 );
Return RGB (R, 255, 0 );
}
Else
{
G = 255-int (255*(s-0.75)/0.25 );
Return RGB (255, G, 0 );
}
}

//////////////////////////////////////// /////////////////////////////////////
// Cmyview drawing

Void cmyview: ondraw (CDC * PDC)
{
Cmydoc * pdoc = getdocument ();
Complex <float> C, Z;
Float Cr, CI, Zr, Zi, PR, PI;
Int I, J, K;
Colorref color [256];
For (I = 0; I <256; I ++)
{
Color [I] = getcolor (I/256.0 );
}
Int Cx = 610, Cy = 511;
Byte * Data = new byte [CX * 4 * Cy];
Memset (data, 205, CX * 4 * CY );
// C = complex <float> (0.45,-0.1428 );
// C = complex <float> (-0.75, 0.0 );
// C = complex <float> (0.285, 0.0 );
// C = complex <float> (-0.8, 0.156 );
// C = complex <float> (-0.70176,-0.3842 );
// C = complex <float> (0.285, 0.01 );
// You can change the initial value to get different results.
Cr = 0.285f;
Ci = 0.01f;
For (I =-300; I <= 300; I ++)
{
For (j =-255; j <= 255; j ++)
{
// Z = complex <float> (I/255.0f, J/255.0f );
ZR = I/255.0f;
Zi = J/255.0f;
For (k = 0; k <= 255; k ++)
{
// If (SQRT (Z. Real () * z. Real () + Z. imag () * z. imag ()> 2.0)
If (SQRT (Zr * zR + Zi * zi)/2> 2.0)
{
Break;
}
Else
{
// Z = z * z + C;
PR = Zr * Zr-zi * Zi;
Pi = 2.0f * Zr * Zi;
ZR = Pr + CR;
Zi = PI + ci;
}
}
Data [4 * (CX * (cy-j-255) + I + 300)] = getbvalue (color [k]);
Data [4 * (CX * (cy-j-255) + I + 300) + 1] = getgvalue (color [k]);
Data [4 * (CX * (cy-j-255) + I + 300) + 2] = getrvalue (color [k]);
}
}
Bitmapinfo sbmpinfo;
: Zeromemory (& sbmpinfo, sizeof (bitmapinfo ));
Sbmpinfo. bmiheader. bisize = sizeof (bitmapinfoheader );
Sbmpinfo. bmiheader. biwidth = Cx;
Sbmpinfo. bmiheader. biheight = Cy;
Sbmpinfo. bmiheader. bibitcount = 32;
Sbmpinfo. bmiheader. biplanes = 1;
Sbmpinfo. bmiheader. bicompression = bi_rgb;
Sbmpinfo. bmiheader. bisizeimage = 32 * CX;
Stretchdibits (PDC-> m_hdc, CX, Cy, CX, Cy, Data, & sbmpinfo, dib_rgb_colors, srccopy );
Delete [] data;
Assert_valid (pdoc );
// Todo: Add draw code for native data here
}

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.