之前在 C#.Net Mandelbrot 集合上发布了一个问题,该问题得到了有益的回答,但是我不得不回到这个 Mandelbrot 集合并在其定义(虚构和真实)坐标的双变量上实现一个结构。
作为一个刚接触结构并且对它们有点生疏的人,我想就我做错了什么以及如何通过查看它来改进上述代码提供一些建议,我相信它可以稍微优化一下. 这是使用结构的正确方法吗?如果不是,有哪些替代方案或最佳技术?谢谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace wmcMandelbrot
{
struct doubleVar
{
public double realCoord, imagCoord;
public double realTemp, imagTemp, realTemp2, arg;
public double newIcoord, newIcoord1, newIcoord2;
public double rCoord, rCoord1, rCoord2;
}
class Program
{
static void Main(string[] args)
{
doubleVar doubleNum;
int iterations;
Console.WriteLine("Enter first imaginary coord: ");
doubleNum.newIcoord = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter second imaginary coord: ");
doubleNum.newIcoord1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter third imaginary coord: ");
doubleNum.newIcoord2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter first real coord: ");
doubleNum.rCoord = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter second real coord: ");
doubleNum.rCoord1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter third real coord: ");
doubleNum.rCoord2 = Convert.ToDouble(Console.ReadLine());
for (doubleNum.imagCoord = doubleNum.newIcoord; doubleNum.imagCoord >= -doubleNum.newIcoord1; doubleNum.imagCoord -= doubleNum.newIcoord2)
{
for (doubleNum.realCoord = -doubleNum.rCoord; doubleNum.realCoord <= doubleNum.rCoord1; doubleNum.realCoord += doubleNum.rCoord2)
{
iterations = 0;
doubleNum.realTemp = doubleNum.realCoord;
doubleNum.imagTemp = doubleNum.imagCoord;
doubleNum.arg = (doubleNum.realCoord * doubleNum.realCoord) + (doubleNum.imagCoord * doubleNum.imagCoord);
while ((doubleNum.arg < 4) && (iterations < 40))
{
doubleNum.realTemp2 = (doubleNum.realTemp * doubleNum.realTemp) - (doubleNum.imagTemp * doubleNum.imagTemp) - doubleNum.realCoord;
doubleNum.imagTemp = (2 * doubleNum.realTemp * doubleNum.imagTemp) - doubleNum.imagCoord;
doubleNum.realTemp = doubleNum.realTemp2;
doubleNum.arg = (doubleNum.realTemp * doubleNum.realTemp) + (doubleNum.imagTemp * doubleNum.imagTemp);
iterations += 1;
}
switch (iterations % 4)
{
case 0:
Console.Write(".");
break;
case 1:
Console.Write("o");
break;
case 2:
Console.Write("O");
break;
case 3:
Console.Write("@");
break;
}
}
Console.Write("\n");
}
Console.ReadKey();
}
}
}