FractalView.cpp
资源名称:Fractal.rar [点击查看]
上传用户:dxkjfz
上传日期:2021-06-06
资源大小:1930k
文件大小:15k
源码类别:
分形几何
开发平台:
Visual C++
- // FractalView.cpp : implementation of the CFractalView class
- //
- #include "stdafx.h"
- #include "Fractal.h"
- #include "FractalDoc.h"
- #include "FractalView.h"
- #include <math.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define TREE_TIMER 10
- #define SIERPINSKI_TIMER 11
- #define FLOWER_TIMER 12
- #define CROWN_TIMER 13
- const double PI = 3.11415926;
- const double PI_tree=3.11415926/180;
- /////////////////////////////////////////////////////////////////////////////
- // CFractalView
- IMPLEMENT_DYNCREATE(CFractalView, CView)
- BEGIN_MESSAGE_MAP(CFractalView, CView)
- //{{AFX_MSG_MAP(CFractalView)
- ON_COMMAND(ID_DRAWTREE, OnDrawtree)
- ON_WM_TIMER()
- ON_COMMAND(ID_DRAWSTOP, OnDrawstop)
- ON_COMMAND(ID_DRAWSIERPINSKI, OnDrawsierpinski)
- ON_COMMAND(ID_DRAWFLOWER, OnDrawflower)
- ON_COMMAND(ID_DRAWCROWN, OnDrawcrown)
- ON_COMMAND(ID_DRAWNEW, OnDrawnew)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFractalView construction/destruction
- CFractalView::CFractalView()
- {
- // TODO: add construction code here
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_bgColor = RGB(0, 0, 0);
- m_fgColor = RGB(0, 255, 0);
- m_dir = TRUE;
- tree.m_bRunning = FALSE;
- tree.m_Interval = 10;
- sier.m_bRunning = FALSE;
- sier.m_Interval = 10;
- flower.m_bRunning = FALSE;
- flower.m_Interval = 10;
- crown.m_bRunning = FALSE;
- crown.m_Interval = 1;
- crown.m_C = 9;
- crown.x=5;
- crown.y=0; //与x,y无关
- crown.m_pColor= RGB(0,255,0);
- crown.stop=false;
- crown.ondraw=false;
- crown.cxmin=-2;
- crown.cxmax=(float)0.8;
- crown.cymin=(float)-0.3;
- crown.cymax=(float)0.3;
- crown.cxspeed=(float)0.2;
- crown.cyspeed=0.25;
- crown.cx=crown.cxmin;
- crown.cy=0;
- crown.cxdirect=1;
- crown.cydirect=-1;
- crown.cnt = 0;
- crown.count = 0;
- }
- CFractalView::~CFractalView()
- {
- }
- BOOL CFractalView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFractalView drawing
- void CFractalView::OnDraw(CDC* pDC)
- {
- CFractalDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- static BOOL bDrawflag = true;
- //SetIcon(m_hIcon, TRUE); // Set big icon
- //SetIcon(m_hIcon, FALSE); // Set small icon
- if (bDrawflag){
- AfxGetMainWnd()->SetWindowText("分形算法演示");
- /************************************************************************/
- //获取窗口大小
- GetClientRect(&m_Rect);
- //得到物理画布
- m_DC.Attach(::GetDC(*this));
- //创建内存画布
- m_MemDC.CreateCompatibleDC(&m_DC);
- m_Bitmap.CreateCompatibleBitmap(&m_DC, m_Rect.Width(), m_Rect.Height());
- m_MemDC.SelectObject(&m_Bitmap);
- //设置画笔颜色
- m_Pen.CreatePen(PS_SOLID, 1, m_fgColor);
- m_MemDC.SelectObject(&m_Pen);
- //填充背景颜色
- m_MemDC.FillSolidRect(&m_Rect, m_bgColor);
- m_DC.BitBlt(0, 0, m_Rect.Width(), m_Rect.Height(), &m_MemDC, 0, 0, SRCCOPY);
- bDrawflag = FALSE;
- }
- /************************************************************************/
- /*
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- //SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- m_DC.BitBlt(0, 0, m_Rect.Width(), m_Rect.Height(), &m_MemDC, 0, 0, SRCCOPY);
- //CFractalView::OnDraw(pDC);
- }*/
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFractalView printing
- BOOL CFractalView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CFractalView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CFractalView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFractalView diagnostics
- #ifdef _DEBUG
- void CFractalView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CFractalView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CFractalDoc* CFractalView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFractalDoc)));
- return (CFractalDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CFractalView message handlers
- void CFractalView::OnDrawtree()
- {
- // TODO: Add your command handler code here
- AfxGetMainWnd()->SetWindowText("摇曳的分形树");
- if (tree.m_bRunning) {
- KillTimer(TREE_TIMER);
- tree.m_bRunning = FALSE;
- } else {
- OnDrawstop();
- m_MemDC.FillSolidRect(&m_Rect, m_bgColor);
- SetTimer(TREE_TIMER, tree.m_Interval, NULL);
- tree.m_bRunning = TRUE;
- }
- //CDialog::OnLButtonDown(nFlags, point);
- }
- void CFractalView::OnDrawsierpinski()
- {
- // TODO: Add your command handler code here
- AfxGetMainWnd()->SetWindowText("摇摆的Sierpinski三角形");
- if (sier.m_bRunning) {
- KillTimer(SIERPINSKI_TIMER);
- sier.m_bRunning = FALSE;
- } else {
- OnDrawstop();
- m_MemDC.FillSolidRect(&m_Rect, m_bgColor);
- SetTimer(SIERPINSKI_TIMER, sier.m_Interval, NULL);
- sier.m_bRunning = TRUE;
- }
- }
- void CFractalView::OnDrawflower()
- {
- // TODO: Add your command handler code here
- AfxGetMainWnd()->SetWindowText("旋转万花筒");
- if (flower.m_bRunning) {
- KillTimer(FLOWER_TIMER);
- flower.m_bRunning = FALSE;
- } else {
- OnDrawstop();
- m_MemDC.FillSolidRect(&m_Rect, m_bgColor);
- SetTimer(FLOWER_TIMER, flower.m_Interval, NULL);
- flower.m_bRunning = TRUE;
- }
- }
- void CFractalView::OnDrawcrown()
- {
- // TODO: Add your command handler code here
- AfxGetMainWnd()->SetWindowText("王冠(Julia集)");
- if (crown.m_bRunning) {
- KillTimer(CROWN_TIMER);
- crown.m_bRunning = FALSE;
- } else {
- OnDrawstop();
- m_MemDC.FillSolidRect(&m_Rect, m_bgColor);
- SetTimer(CROWN_TIMER, crown.m_Interval, NULL);
- crown.m_bRunning = TRUE;
- }
- }
- void CFractalView::OnDrawstop()
- {
- // TODO: Add your command handler code here
- KillTimer(TREE_TIMER);
- tree.m_bRunning = FALSE;
- KillTimer(SIERPINSKI_TIMER);
- sier.m_bRunning = FALSE;
- KillTimer(FLOWER_TIMER);
- flower.m_bRunning = FALSE;
- KillTimer(CROWN_TIMER);
- crown.m_bRunning = FALSE;
- }
- void CFractalView::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- static float tree_k = 9;
- static float sier_k = 0;
- static float flower_k = 0;
- //在内存画布中清屏
- // if (CROWN_TIMER!=nIDEvent)
- {
- m_MemDC.FillSolidRect(&m_Rect, m_bgColor);
- }
- switch(nIDEvent){
- /* 分形树定时器 */
- case TREE_TIMER:
- int x, y;
- x = m_Rect.Width()/2;
- y = m_Rect.Height()-50;
- //在内存画布中绘制树
- drawLeaf(x, y, m_Rect.Height()/5, 270, 50, tree_k);
- if (m_dir) {
- tree_k += 0.5;
- if (tree_k > 18)
- m_dir = FALSE;
- } else {
- tree_k -= 0.5;
- if (tree_k < -18)
- m_dir = TRUE;
- }
- break;
- /* Sierpinski三角形定时器 */
- case SIERPINSKI_TIMER:
- if (m_dir) {
- sier_ifs(&m_MemDC, sier_k+=0.01);
- if (sier_k > 0.5)
- m_dir = FALSE;
- } else {
- sier_ifs(&m_MemDC, sier_k-=0.01);
- if (sier_k<0)
- m_dir =TRUE;
- }
- break;
- /* 万花筒定时器 */
- case FLOWER_TIMER:
- if (m_dir) {
- flower_ifs(&m_MemDC, flower_k+=5);
- if (flower_k > 500)
- m_dir = FALSE;
- } else {
- flower_ifs(&m_MemDC, flower_k-=5);
- if (flower_k<-500)
- m_dir =TRUE;
- }
- break;
- /* 王冠定时器 */
- case CROWN_TIMER:
- drawCrown();
- break;
- default:
- break;
- }
- //把树拷贝到物理画布中
- m_DC.BitBlt(0, 0, m_Rect.Width(), m_Rect.Height(), &m_MemDC, 0, 0, SRCCOPY);
- CView::OnTimer(nIDEvent);
- }
- void CFractalView::drawLeaf(double x, double y, double L, double A,float B,float C){
- double s1, s2, s3;
- s1 = 2;
- s2 = 3;
- s3 = 1.3;
- int x1,y1,x1L,y1L,x1R,y1R,x2,y2,x2R,y2R,x2L,y2L;
- if(L > s1)
- {
- x2 = (int)(x + L * cos(A * PI_tree));
- y2 = (int)(y + L * sin(A * PI_tree));
- x2R = (int)(x2 + L / s2 * cos((A + B) * PI_tree));
- y2R = (int)(y2 + L / s2 * sin((A + B) * PI_tree));
- x2L = (int)(x2 + L / s2 * cos((A - B) * PI_tree));
- y2L = (int)(y2 + L / s2 * sin((A - B) * PI_tree));
- x1 = (int)(x + L / s2 * cos(A * PI_tree));
- y1 = (int)(y + L / s2 * sin(A * PI_tree));
- x1L = (int)(x1 + L / s2 * cos((A - B) * PI_tree));
- y1L = (int)(y1 + L / s2 * sin((A - B) * PI_tree));
- x1R = (int)(x1 + L / s2 * cos((A + B) * PI_tree));
- y1R = (int)(y1 + L / s2 * sin((A + B) * PI_tree));
- m_MemDC.MoveTo((int)x,(int)y);
- m_MemDC.LineTo(x2,y2);
- m_MemDC.LineTo(x2R,y2R);
- m_MemDC.MoveTo(x2,y2);
- m_MemDC.LineTo(x2L,y2L);
- m_MemDC.MoveTo(x1,y1);
- m_MemDC.LineTo(x1L,y1L);
- m_MemDC.MoveTo(x1,y1);
- m_MemDC.LineTo(x1R,y1R);
- drawLeaf(x2, y2, L / s3, A + C, B, C);
- drawLeaf(x2R, y2R, L / s2, A + B, B, C);
- drawLeaf(x2L, y2L, L / s2, A - B, B, C);
- drawLeaf(x1L, y1L, L / s2, A - B, B, C);
- drawLeaf(x1R, y1R, L / s2, A + B, B, C);
- }
- }
- void CFractalView::sier_ifs(CDC *pDC, float k)
- {
- float x=0; // 仿射变换中的自变量
- float y=0;
- float newx, newy; //仿射变换产生的新点
- float a, b, c, d, e, f; //仿射变幻中的系数
- long n=10000; // 迭代次数
- float R; //随机变量
- float m[7][7]={0}; // 存放IFS码
- //IFS码赋值
- m[0][0] = 0.5; m[0][1] = 0; m[0][2] = 0; m[0][3] = k; m[0][4] = 0; m[0][5] = 0; m[0][6] = 0.333;
- m[1][0] = 0.5; m[1][1] = 0; m[1][2] = 0; m[1][3] = 0.5; m[1][4] =0.5; m[1][5] = 0; m[1][6] = 0.333;
- m[2][0] = k; m[2][1] = 0; m[2][2] = 0; m[2][3] = 0.5; m[2][4] = 0.25; m[2][5] = 0.5; m[2][6] = 0.334;
- //循环迭代,在不同的概率空间下,赋不同的IFS码值
- while (n > 0) {
- R = (float)rand()/RAND_MAX;
- if (R <= m[0][6]) {
- a = m[0][0]; b = m[0][1]; c = m[0][2]; d = m[0][3]; e = m[0][4]; f = m[0][5];
- } else if (R <= m[0][6] + m[1][6]) {
- a = m[1][0]; b = m[1][1]; c = m[1][2]; d = m[1][3]; e = m[1][4]; f = m[1][5];
- } else {
- a = m[2][0]; b = m[2][1]; c = m[2][2]; d = m[2][3]; e = m[2][4]; f = m[2][5];
- }
- newx = (a * x) + (b * y) + e;
- newy = (c * x) + (d * y) + f;
- x = newx; y = newy;
- pDC->SetPixelV((1000 + 7000 * x)/15, (6500 - 6000 * y)/15, RGB(x * 500 * R, R * 100, y * 2000 * R));
- n--;
- }
- }
- void CFractalView::flower_ifs(CDC *pDC, float k){
- float x=0; // '仿射变换中的自变量
- float y=0;
- float newx, newy; //'仿射变换产生的新点
- float a, b, c, d, e, f; //'仿射变幻中的系数
- long n=10000; // '迭代次数
- float R; //'随机变量
- float m[7][7]={0}; // '存放IFS码
- //'IFS码赋值
- m[0][0] = 0.2; m[0][1] = 0.2; m[0][2] = 0; m[0][3] = 0; m[0][4] = 0.7; m[0][5] = 0; m[0][6] = 0.2;
- m[1][0] = 0.2; m[1][1] = 0.2; m[1][2] = 0; m[1][3] = 0; m[1][4] =-0.7; m[1][5] = 0; m[1][6] = 0.2;
- m[2][0] = 0.2; m[2][1] = 0.2; m[2][2] = 0; m[2][3] = 0; m[2][4] = 0; m[2][5] = 0.7; m[2][6] = 0.2;
- m[3][0] = 0.2; m[3][1] = 0.2; m[3][2] = 0; m[3][3] = 0; m[3][4] = 0; m[3][5] = -0.7; m[3][6] = 0.2;
- m[4][0] = 0.85; m[4][1] = 0.85; m[4][2] = k; m[4][3] = k; m[4][4] = 0; m[4][5] = 0; m[4][6] = 0.2;
- //'循环迭代,在不同的概率空间下,赋不同的IFS码值
- while (n > 0) {
- R = (float)rand()/RAND_MAX;
- if (R <= m[0][6])
- {
- a = m[0][0]; b = m[0][1]; c = m[0][2]; d = m[0][3]; e = m[0][4]; f = m[0][5];
- } else if (R <= m[0][6] + m[1][6])
- {
- a = m[1][0]; b = m[1][1]; c = m[1][2]; d = m[1][3]; e = m[1][4]; f = m[1][5];
- } else if (R <= m[0][6] + m[1][6] + m[2][6])
- {
- a = m[2][0]; b = m[2][1]; c = m[2][2]; d = m[2][3]; e = m[2][4]; f = m[2][5];
- } else if (R <= m[0][6] + m[1][6] + m[2][6] + m[3][6])
- {
- a = m[3][0]; b = m[3][1]; c = m[3][2]; d = m[3][3]; e = m[3][4]; f = m[3][5];
- } else
- {
- a = m[4][0]; b = m[4][1]; c = m[4][2]; d = m[4][3]; e = m[4][4]; f = m[4][5];
- }
- newx = (a * x*cos(c/180)) - (b * y*sin(d/180)) + e;
- newy = (a * x*sin(c/180)) + (b * y*cos(d/180)) + f;
- x = newx; y = newy;
- pDC->SetPixelV((300 + 200 * x), (220 - 200 * y), RGB(x * 500 * R, R * 100, y * 2000 * R));
- n--;
- }
- }
- void CFractalView::drawCrown()
- {
- static float k;
- crown.count++;
- for(int i=0;i<12000;i++)
- {
- k=(float)rand();
- crown.rnd=(float)(k/RAND_MAX);
- crown.wx=crown.x-crown.cx;
- crown.wy=crown.y-crown.cy;
- if(crown.wx==0){
- crown.theta=PI/2;
- }
- else if(crown.wx>0){
- crown.theta=atan(crown.wy/crown.wx);
- }
- else if(crown.wx<0){
- crown.theta=PI-atan(crown.wy/crown.wx);
- }
- crown.theta=crown.theta/2;
- crown.r=sqrt(crown.wx*crown.wx+crown.wy*crown.wy);
- if(crown.rnd<0.5){
- crown.r=sqrt(crown.r);
- }else{
- crown.r=-sqrt(crown.r);
- }
- crown.x=crown.r*cos(crown.theta);
- crown.y=crown.r*sin(crown.theta);
- m_MemDC.SetPixel((int)(2000*crown.x/15+3500/15+0.5),(int)(2000*crown.y/15+3500/15+0.5),0x00ff00);
- //Invalidate(false);
- if(crown.count==2)
- {
- crown.count=0;
- }
- }
- crown.cx=crown.cx+0.002*crown.cxdirect;
- if(crown.cx>crown.cxmax && crown.cxdirect==1)
- {
- crown.cxdirect=-1;
- crown.cnt++;
- }
- else if(crown.cx<crown.cxmin && crown.cxdirect==-1)
- {
- crown.cxdirect=1;
- crown.cnt++;
- }
- if(crown.cnt%3==0 &&crown.cnt!=0)
- {
- crown.cy=crown.cy+0.003*crown.cydirect;
- }
- if(crown.cy>crown.cymax && crown.cydirect==1)
- {
- crown.cydirect=-1;
- }
- else if(crown.cy<crown.cymin && crown.cydirect==-1)
- {
- crown.cydirect=1;
- }
- }
- void CFractalView::OnDrawnew()
- {
- // TODO: Add your command handler code here
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_bgColor = RGB(0, 0, 0);
- m_fgColor = RGB(0, 255, 0);
- tree.m_Interval = 10;
- sier.m_Interval = 10;
- flower.m_Interval = 10;
- crown.m_Interval = 1;
- crown.m_C = 9;
- crown.x=5;
- crown.y=0; //与x,y无关
- crown.m_pColor= RGB(0,255,0);
- crown.stop=false;
- crown.ondraw=false;
- crown.cxmin=-2;
- crown.cxmax=(float)0.8;
- crown.cymin=(float)-0.3;
- crown.cymax=(float)0.3;
- crown.cxspeed=(float)0.2;
- crown.cyspeed=0.25;
- crown.cx=crown.cxmin;
- crown.cy=0;
- crown.cxdirect=1;
- crown.cydirect=-1;
- crown.cnt = 0;
- crown.count = 0;
- }