|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
![]() IntroductionThis program implements a simple way to see a Mandelbrot set. There are tons of programs like this one. But because of a Science library, SCI[1] , it allows us to calculate Main Loop of CodeThere is a pseudo-code on Wikipedia[2]. Sci.Math.ComplexNumber C = z;
int iteration = 0;
while (z.Modulus < escapeRadius && iteration < maxIteration)
{
z = z * z + C;
iteration++;
}
int colorIndex = 0;
if (iteration < maxIteration)
{
z = z * z + C; iteration++;
z = z * z + C; iteration++;
double mu = iteration - (Math.Log(Math.Log(z.Modulus))) / logEscapeRadius;
colorIndex = (int)(mu / maxIteration * 768);
if (colorIndex >= 768) colorIndex = 0;
if (colorIndex < 0) colorIndex = 0;
}
For each point Then, according to the iteration steps, decide what color would be used for this coordinate. There is a Color array for speeding the look up. for (int i=0;i<768;i++)
{
int colorValueR=0;
int colorValueG=0;
int colorValueB=0;
if (i >= 512)
{
colorValueR = i - 512;
colorValueG = 255 - colorValueR;
}
else if (i >= 256)
{
colorValueG = i - 256;
colorValueB = 255 - colorValueG;
}
else
{
colorValueB = i;
}
Colors[i] = Color.FromArgb(colorValueR, colorValueG, colorValueB);
}
The values inside index Red Green Blue
0 0 0 0
. 0 0 .
255 0 0 255
256 0 0 255
. 0 . .
384 0 128 127
. 0 . .
511 0 255 0
512 0 255 0
. . . 0
640 128 127 0
. . . 0
767 255 0 0
Screen Point To Coordinate On Plane ConversionWhen the cursor moves on an image, there is a label showing the current coordinate on the plane: int x = e.X;
int y = (control.Height - 1) - e.Y;
Sci.Math.ComplexNumber P = P2 + new Sci.Math.ComplexNumber((double)x *
(P1.Re - P2.Re) / (p.Width - 1),
(double)y * (P1.Im - P2.Im) / (p.Height - 1));
pos.Text = String.Format("({0:E},{1:E})", P.Re, P.Im);
The Scaling ImageWhen the user double-clicks, the clicked point on the image has to be converted into the coordinate of plane. The coordinate of the clicked point would be the center point of the new image, with scaling factor 2. int x = e.X;
int y = (control.Height - 1) - e.Y;
Sci.Math.ComplexNumber P = P2 + new Sci.Math.ComplexNumber((double)x *
(P1.Re - P2.Re) / (p.Width - 1),
(double)y * (P1.Im - P2.Im) / (p.Height - 1));
Sci.Math.ComplexNumber diff = new Sci.Math.ComplexNumber();
switch (e.Button)
{
case MouseButtons.Left:
// Zoom in
diff = (P1 - P2) / 2 / 2;
break;
case MouseButtons.Right:
// Zoom out
diff = (P1 - P2) / 2 * 2;
break;
}
// New Region
P1 = P + diff;
P2 = P - diff;
This program use two Some ScreenshotsAll are with maximal iteration=150 and escape radius=2:
ConclusionThe implementation of the Mandelbrot set is really easy. But the drawing part might meet with a bottleneck. Therefore, the code uses a lower-level method to draw, instead of calling LinksHistory
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||