⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 childview.cpp

📁 在分形几何里面
💻 CPP
字号:
// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "dla.h"
#include "ChildView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChildView

CChildView::CChildView()
{
	r0=5;
	  srand(time(NULL));
}

CChildView::~CChildView()
{
}
Randomize();
BEGIN_MESSAGE_MAP(CChildView,CWnd )
	//{{AFX_MSG_MAP(CChildView)
	ON_WM_PAINT()
	ON_COMMAND(ID_PAINT, OnDoPaint)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);

	return TRUE;
}

void CChildView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	AfxGetMainWnd()->SetWindowText("垂直生长的DLA模型(分形频道:fractal.cn)2004");
	// TODO: Add your message handler code here

    dc.MoveTo(100,500);
	dc.LineTo(700,500);

	r0=5;
	do_paint(&dc);
}


#define putpixel(x, y, c) pDC->SetPixel(x,y,c);
#define getpixel(x,y) pDC->GetPixel(x,y)
#define _rand() ((double)(rand())/RAND_MAX)

int sgn(double x) 
{
	if (x > 0) return 1;
	else if (x < 0) return -1;
	else return 0;
}



void CChildView::do_paint(CDC *pDC)
{
  int x, y, xb, yb;
  int rx, ry, rv, rmax, r, dist;
  int cyt, cyb, cyr, cyl;
  int n=0;
  int twd[3];
  int d=500;

  static COLORREF cl_white = RGB(0xFF, 0xFF, 0xFF);
  static COLORREF cl_red = RGB(0xFF, 0, 0);


  do 
  {
      
   x=int(400*_rand()+200);
   y = d - r0;
   
   for(; ;)
   {
     xb = x;
     yb = y;
    
	
     cyt = getpixel(x, y+1);
     cyb = getpixel(x, y-1);
     cyr = getpixel(x+1, y);
     cyl = getpixel(x-1, y);
 
	 if (unsigned(cyt) != cl_white ||unsigned(cyb) != cl_white || unsigned(cyr) != cl_white || unsigned(cyl) != cl_white) 
	 {
	   putpixel(x,y, 0);
	   ++n;
	   d=y;
	   break;
     }

     if (x < 100 || x > 700) 
	 {
       putpixel(x,y, cl_white);
       break;
     }

     twd[1] = 0;
     twd[2] = 0;
     twd[(int)(2*_rand())+1] = sgn(_rand()-0.5);
     x += twd[1];
     y += 1;
     putpixel(xb,yb, cl_white);
     putpixel(x,y, cl_red);
  
 } /* end for*/
  
 }while (n < 200000);
}

void CChildView::OnDoPaint() 
{
	CDC* pDC = GetDC();

	do_paint(pDC);

	ReleaseDC(pDC);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -