#include <windows.h>
#include <d3d9.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#pragma comment(lib,"d3d9.lib")
//逃逸时间算法的最大迭代次数
#define MAX_ITERATIONS 200
//绘图窗口大小
#define WIDTH 255
#define HEIGHT 255
//上左的距离
#define MARGIN_TOP 30
#define MARGIN_LEFT 20
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
class CD3DFractal//绘制基类
{
public:
//Begin -- 顶点类型
struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; // The transformed position for the vertex.
D3DCOLOR color; // The vertex color.
};
//End -- 顶点类型
//Begin -- 复数运算类
class ComplexPoint
{
//直角坐标
double m_dReal;//实部
double m_dImag;//虚部
//极坐标
//double ;
//double m_dAlpha;
public:
ComplexPoint(double r=0, double i=0):m_dReal(r),m_dImag(i){}
void Set(double r=0,double i=0)
{
m_dReal = r;
m_dImag = i;
}
void Set(ComplexPoint p)
{
m_dReal = p.m_dReal;
m_dImag = p.m_dImag;
}
double GetReal()
{
return m_dReal;
}
double GetImag()
{
return m_dImag;
}
ComplexPoint operator +(const ComplexPoint &p) //加法运算
{
ComplexPoint ret;
ret.m_dReal = m_dReal + p.m_dReal;
ret.m_dImag = m_dImag + p.m_dImag;
return ret;
}
ComplexPoint operator -(const ComplexPoint &p) //减法运算
{
ComplexPoint ret;
ret.m_dReal = m_dReal - p.m_dReal;
ret.m_dImag = m_dImag - p.m_dImag;
return ret;
}
ComplexPoint operator *(const ComplexPoint &p) //乘法运算
{
ComplexPoint ret;
ret.m_dReal = m_dReal * p.m_dReal - m_dImag * p.m_dImag;
ret.m_dImag = m_dReal * p.m_dImag + m_dImag * p.m_dReal;
return ret;
}
ComplexPoint operator *(const double r) //乘法运算
{
ComplexPoint ret;
ret.m_dReal = m_dReal * r;
ret.m_dImag = m_dImag * r;
return ret;
}
void Cubic()
{
ComplexPoint p=*this;
Square();
*this = p**this;
}
void Square() // 平方运算
{
double real=m_dReal;
double imag=m_dImag;
m_dReal = real * real - imag * imag;
m_dImag = 2 * real * imag;
}
void SquareRoot() // 开方运算
{
double r = sqrt( m_dReal * m_dReal + m_dImag * m_dImag );
if( fabs(r) < 1e-10 )
return;
double alpha = acos (m_dReal / r);
r = sqrt(r);
alpha /= 2;
m_dReal = r * cos( alpha );
m_dImag = r * sin( alpha );
}
double Distance_Square() // 到原点距离的平方
{
return m_dReal * m_dReal + m_dImag * m_dImag;
}
};
//End -- 复数运算类
protected:
IDirect3D9 * m_pD3D;
IDirect3DDevice9 * m_pd3dDevice;
IDirect3DVertexBuffer9 * m_pVB;
int m_iCount;//点的个数
D3DCOLOR m_palette[MAX_ITERATIONS];//颜色,与迭代次数有关
public:
CD3DFractal():m_pD3D(NULL),m_pd3dDevice(NULL),m_pVB(NULL),m_iCount(0)
{
InitPalette();
}
~CD3DFractal()
{
Cleanup();
}
//初始化D3D设备
DWORD InitD3D9Device(HWND hWnd)
{
if( hWnd == NULL )
{
MessageBox( NULL, "窗口句柄为空", "出错了", MB_OK);
return E_FAIL;
}
if( NULL == ( m_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
{
MessageBox( NULL, "D3D9环境创建失败", "出错了", MB_OK);
return E_FAIL;
}
//描述信息
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
//创建设备
if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &m_pd3dDevice ) ) )
{
MessageBox( NULL, "D3D9设备创建失败", "出错了", MB_OK);
return E_FAIL;
}
return NOERROR;
}
//初始化缓存
DWORD InitVerticesBuffer(const CUSTOMVERTEX * vertices, const int count)
{
m_iCount = count;
if( m_iCount < 0 || NULL == vertices)
{
m_iCount = 0;
return E_FAIL;
}
if( NULL == m_pd3dDevice)
{
MessageBox( NULL, "D3D9设备未创建", "出错了", MB_OK);
return E_FAIL;
}
//顶点数组的大小
size_t SizeV = sizeof(CUSTOMVERTEX)*m_iCount;
//创建缓存
if( m_pVB == NULL )
{
if( FAILED( m_pd3dDevice->CreateVertexBuffer( SizeV,
0 /*Usage*/, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_pVB, NULL ) ) )
{
MessageBox( NULL, "Vertices Buffer创建失败", "出错了", MB_OK);
return E_FAIL;
}
}
VOID* pVertices;
//加锁
if( FAILED( m_pVB->Lock( 0, SizeV, (void**)&pVertices, 0 ) ) )
{
MessageBox( NULL, "缓存加锁失败", "出错了", MB_OK);
return E_FAIL;
}
//复制顶点数据
memcpy(pVertices, vertices, SizeV);
//解锁
m_pVB->Unlock();
return NOERROR;
}
//渲染
virtual VOID Render()
{
if( NULL == m_pd3dDevice)
{
MessageBox( NULL, "设备未初始化", "出错了", MB_OK);
return;
}
//清除背景缓存,并绘以蓝色
m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
//开始绘制场景
m_pd3dDevice->BeginScene();
//绘制点集合
m_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->DrawPrimitive( D3DPT_POINTLIST, 0, m_iCount );
//结束绘制场景
m_pd3dDevice->EndScene();
m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//绘制图形,纯虚函数,等待子类实现
virtual VOID FractalDraw() = 0;
//释放设备
VOID Cleanup()
{
if( m_pd3dDevice != NULL )
{
m_pd3dDevice->Release();
m_pd3dDevice = NULL;
}
if( m_pD3D != NULL )
{
m_pD3D->Release();
m_pD3D = NULL;
}
}
//初始化调色板
virtual VOID InitPalette()
{
double colFactor = 255.0 / log( MAX_ITERATIONS + 1.0 );
int iLevel;
for( int i = 0; i < MAX_ITERATIONS; ++ i )
{
iLevel = (int) ( colFactor * log( i + 1.0 ) );
m_palette[i] = D3DCOLOR_XRGB( iLevel, iLevel, 255-iLevel );
}
}
//迭代公式
ComplexPoint F(ComplexPoint & Z, ComplexPoint & C)
{
return Z*Z - C;
}
};
//背景颜色
#define COLORBACKGROUND D3DCOLOR_XRGB( 0, 0, 0 )
//M集图形,x轴的上下限
#define REAL_LOW -0.50 //-0.50
#define REAL_HIGH 2.00
//M集图形,y轴的上下限
#define IMAG_LOW -1.25 //-1.25
#define IMAG_HIGH 1.25 //1.25
//M集绘制类
class CMandelSet : public CD3DFractal
{
private:
CUSTOMVERTEX m_vertices[WIDTH*HEIGHT];
public:
//鼠标位置转化为M集图形坐标系中的坐标
VOID GetC(const UINT x, const UINT y, double &cx, double &cy)
{
double dx = ( REAL_HIGH - REAL_LOW ) / WIDTH;//每个象素偏移对应坐
分形图形——M集和J集图形对应关系浏览
5星 · 超过95%的资源 需积分: 15 14 浏览量
2008-12-16
19:51:26
上传
评论 2
收藏 79KB RAR 举报
mmmcd
- 粉丝: 37
- 资源: 9
最新资源
- 问题2.zip
- QT登录模块,登录时获取txt文件内账号密码进行判断,注册时写入到本地文件user.txt
- 湖南麒麟系统下,因某些原因无法正常进入系统时可以进入单用户模式
- tsunami-udp 是一款专为网络加速诞生的小工具 用TCP进行传输控制、用UDP进行数据传输
- 基于FreeRTOS、LCD1602 、STM32CubeMX、GP2Y0A21YK0F红外测距传感器的测距proteus仿真
- C语言中的一些算法和面试题
- 個人使用2個人使用2個人使用2個人使用2個人使用2
- A72BDB68-F5FA-4D0F-906E-EACAA6A1EFA5.rar
- TTP229-BSF数据手册
- 時間管理.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈