文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Mandelbrot的C++实现方法

Mandelbrot的C++实现方法

时间:2010-11-28  来源:wgzlilu

Mandelbrot图像中的每个位置都对应于公式N = x+y*i中的一个复数,实数部分是x,虚数部分是y,i是-1的平方根,图像中的每个位置的x和y坐标对应于虚数的x和y坐标. 图像中的每个位置用参数N来表示,它是x*x+y*y的平方根.如果这个值大于或等于2.则这个数字对应的位置值是0. 如果参数N的值小于2,就把N的值改为N*N-N(即N = (x*x-y*y-x) + (2*x*y-y)*i),并再次测试这个N的值, 如果这个值大于或等于2,则这个数字对应的位置值是1.   下面给出C++的实现方法        

// Mandelbrot.cpp : 定义控制台应用程序的入口点。

//


#include "stdafx.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    double realCoord,imagCoord;
    double realMax = 1.77;
    double realMin = -0.6;
    double imagMax = -1.2;
    double imagMin = 1.2;
    double realStep;
    double imagStep;
    double realTemp,imagTemp,realTemp2,arg;
    
    int iterations;
    
    while(true)
    {
        realStep = (realMax - realMin)/79;
        imagStep = (imagMax - imagMin)/48;
        
        for(imagCoord = imagMin; imagCoord >= imagMax;imagCoord+=imagStep)\
        {
            for(realCoord = realMin;realCoord<=realMax;realCoord +=realStep)
            {
                iterations = 0;
                realTemp = realCoord;
                imagTemp = imagCoord;
                arg = (realCoord * realCoord) + (imagCoord * imagCoord);
                while((arg < 4) && (iterations < 40))
                {
                    realTemp2 = (realTemp * realTemp) - (imagTemp * imagTemp) - realCoord;
                    imagTemp = (2 * realTemp * imagTemp) - imagCoord;
                    realTemp = realTemp2;
                    arg = (realTemp * realTemp)+(imagTemp * imagTemp);
                    iterations +=1;
                }
                switch(iterations % 4)
                {
                case 0:
                    cout<<".";
                    break;
                case 1:
                    cout<<"o";
                    break;
                case 2:
                    cout<<"O";
                    break;
                case 3:
                    cout<<"@";
                    break;
                }
            }
            cout<<"\n";
            
        }
           cout<<"Current limits:";
           cout<<"realCoord: from "<<realMin<<" to "<<realMax<<endl;
           cout<<"imagCoord: from "<<imagMin<<" to "<<imagMax<<endl;
           cout<<"Enter new limits:"<<endl;
           cout<<"realCoord: from:"<<endl;
           cin>>realMin;
         cout<<"realCoord: to:"<<endl;
         cin>>realMax;
         cout<<"imagCoord: from:"<<endl;
         cin>>imagMin;
         cout<<"imagCoord: to:"<<endl;
         cin>>imagMax;
    }
    
    getch();
    return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载