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

📄 treeview.cpp

📁 随机LS文法生成树(在LS文法中引入随机变量
💻 CPP
字号:
// TreeView.cpp : implementation of the CMyTreeView class
//

#include "stdafx.h"
#include "Tree.h"

#include "TreeDoc.h"
#include "TreeView.h"
#include "math.h"
#define  PI 3.14159

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

/////////////////////////////////////////////////////////////////////////////
// CMyTreeView

IMPLEMENT_DYNCREATE(CMyTreeView, CView)

BEGIN_MESSAGE_MAP(CMyTreeView, CView)
	//{{AFX_MSG_MAP(CMyTreeView)
	ON_WM_LBUTTONDBLCLK()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMyTreeView construction/destruction

CMyTreeView::CMyTreeView()
{
	// TODO: add construction code here
	unit[0] =   "F[+F]F[-F]F";
	unit[1] =   "F[+F]F[-F[+F]]";
	unit[2] =   "FF-[-F+F+F]+[+F-F-F]";
	
	len =3 ;  //象素长
	angle  = 30 ;  //转角为30度
	degree = 5; //迭代次数
	ox = 450 ;
	oy = 650 ;
	

}

CMyTreeView::~CMyTreeView()
{
}

BOOL CMyTreeView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
 
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyTreeView drawing

void CMyTreeView::OnDraw(CDC* pDC)
{
	CTreeDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	AfxGetMainWnd()->SetWindowText("随机L系统(分形频道:fractal.cn)2004    请双击窗体");
	if(tree.IsEmpty ())
		return ;
	else 
	{
		CKNOT  mNextKnot ,mCurKnot;
  		mCurKnot.x = ox ;
		mCurKnot.y = oy ;
		mCurKnot.direction = 90 ;
		
		int treelength = tree.GetLength ();
		int  i = 0;
		pDC ->MoveTo (ox,oy);
		while (i<treelength)
		{
			switch(tree[i])
			{
				case 'F':
		 			mNextKnot.x = mCurKnot.x  + len * cos(mCurKnot.direction * PI /180);
					mNextKnot.y = mCurKnot.y  - len * sin(mCurKnot.direction * PI /180);
					mNextKnot.direction = mCurKnot.direction ;
					pDC ->LineTo (mNextKnot.x ,mNextKnot.y);
					mCurKnot = mNextKnot ;
					break ;
				case '[':
					stack[stackpushpos] = mCurKnot ;
					stackpushpos ++;
					break;
				case ']':
					mCurKnot = stack[stackpushpos-1]; 
					stackpushpos -- ;
					pDC ->MoveTo (mCurKnot.x,mCurKnot.y);
					break;
				case '+':
					mCurKnot.direction = mCurKnot.direction + angle;
					break;
				case '-':
					mCurKnot.direction = mCurKnot.direction - angle;
					break;
				default:
					;
			}
			i++;
		}
	}
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMyTreeView printing

BOOL CMyTreeView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMyTreeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMyTreeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMyTreeView diagnostics

#ifdef _DEBUG
void CMyTreeView::AssertValid() const
{
	CView::AssertValid();
}

void CMyTreeView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTreeDoc* CMyTreeView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTreeDoc)));
	return (CTreeDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyTreeView message handlers

void CMyTreeView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	///////////////////////////////////////////////////////////////
	//////初始化///////////////////////////////////////////////////
	for (int i = 0 ; i <1024 ; i++ )
	{
		stack[i].x =  0 ;
		stack[i].direction = NULL ;
	}
	stackpushpos = 0 ;
	formtree  = unit[rand() % 3] ;
	tree.Empty ();
	//////////////////////////////////////////////////////////////
	int random ;
	for ( i = 1 ; i <= degree ; i++)
	{
		int curlen = formtree.GetLength ();
		int pos=0 , j = 0 ;
		while (j < curlen)
		{
			if(formtree[j] == 'F' )
			{
				random = rand() % 3 ;
 				tree += unit[random];
				j++;
				pos = tree.GetLength () -1 ;
			}
			else 
			{	
				tree += formtree[j] ;
				pos ++;
				j++;
			}
		}
 
		formtree = tree ;
 
		tree.Empty ();
	}
	tree = formtree ;
	Invalidate();
	
	 
}

BOOL CMyTreeView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CView::OnScroll(nScrollCode, nPos, bDoScroll);
}

⌨️ 快捷键说明

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