我是编码新手,我通过学习 C# 作为我的第一门编程语言开始了我的旅程。作为一项任务,我正在尝试编写一个绘制 Mandelbrot 集的程序,但它没有绘制它应该绘制的内容,而且在调试时我似乎找不到任何问题。它只是在左上角绘制了一个带有几个黑色像素的白色正方形。
这是我的代码中最重要的部分(PaintEventArgs 方法):
//Magnitude and mandelnum @ start
int mandelnum = 0;
double Magnitude = 0; //Magnitude is the distance of (a,b) to middle of my picturebox
//a and b @ start. a and b are all (x,y) pixels of my bitmap reformed with the
following formulas: (a*a-b*b+x, 2*a*b+y)
double a = 0;
double b = 0;
int x = 0;
int y = 0;
int picboxmiddleX = (50 + pictureBox1.Width) / 2;
int picboxmiddleY = (110 + pictureBox1.Height) / 2; //the location of picturebox is
(50,110)
//loop through all pixels, calc magnitude for every (x,y) and stop if magnitude is
larger than 2, attach color black or white to ever pixel dependent on even or odd
mandelnums.
for (x = 0; x < pictureBox1.Width; x++)
{
for (y = 0; y < pictureBox1.Height; y++)
{
while (Magnitude < 2.0) {
mandelgetal++;
if (mandelgetal < 100)
{
break;
}
a = (a * a) - (b * b) + x;
b = (2 * a * b) + y;
Magnitude = Math.Sqrt(Math.Pow((a - middenpanelX), 2) + Math.Pow((b -
middenpanelY), 2));
}
//pixels with even mandelnum get color white and odd is black
if (mandelgetal % 2 == 0)
{
bm.SetPixel(x, y, Color.White);
}
else if (mandelgetal % 2 != 0)
{
bm.SetPixel(x, y, Color.Black);
}
}
}
pictureBox1.Image = bm;
}
我希望有人能帮帮忙!