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

📄 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()
{
}

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
	
	// Do not call CWnd::OnPaint() for painting messages
}


#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;
}

#define BASEX 400
#define BASEY 300

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

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

  srand(time(NULL));
  cl_white = putpixel(0+BASEX,0+BASEY, RGB(0xFF, 0xFF, 0xFF));
  
  cl_red = putpixel(0+BASEX,0+BASEY, cl_red);

  r0 = 5;
  do {
   r = r0+5;
   rmax = r0 + 40;
   rx = (2*r+1)*_rand() - r;
   //rv = r - abs(rx);
   //ry = rv * sgn(_rand()-0.5);
   ry = (2*r+1)*_rand() - r;
   x = rx;
   y = ry;
   for(; ;) {
     xb = x;
     yb = y;
     dist = abs(x) + abs(y);
     cyt = getpixel(x+BASEX, y+BASEY+1);
     cyb = getpixel(x+BASEX, y+BASEY-1);
     cyr = getpixel(x+BASEX+1, y+BASEY);
     cyl = getpixel(x+BASEX-1, y+BASEY);

     if (cyt != cl_white || cyb  != cl_white || cyr != cl_white || cyl != cl_white) {
	putpixel(x+BASEX,y+BASEY, 0);
	++n;
	if (dist > r0) r0 = dist;
	break;
     }
     if (dist > rmax) {
       putpixel(x+BASEX,y+BASEY, cl_white);
       break;
     }

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

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

	do_paint(pDC);

	ReleaseDC(pDC);
}

⌨️ 快捷键说明

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