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

📄 treeview.cpp

📁 收录的是《分形算法与程序设计VC版》文件夹<第3章VC>包括: <3_1>:内含Koch曲线的LS生成源代码。双击Debug下的Tree.exe文件
💻 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.1415926

#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()
	ON_WM_CREATE()
	//}}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[0] =   "F";//公理
	a[1]="F";  unit[1] = "F-F++F-F";
	//unit[2] =   "FF-[-F+F+F]+[+F-F-F]";
	
	len =10;  //线段长度
	angle  = 60.0 ;  //转角
	degree = 4; //迭代次数
	ox = 900 ;
	oy = 450 ;
	

}

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 = 180 ;
		
		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 ((int)mNextKnot.x ,(int)mNextKnot.y);
					mCurKnot = mNextKnot ;
					break ;
				case '[':
					stack[stackpushpos] = mCurKnot ;
					stackpushpos ++;
					break;
				case ']':
					mCurKnot = stack[stackpushpos-1]; 
					stackpushpos -- ;
					pDC ->MoveTo ((int)mCurKnot.x,(int)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[0] ;//公理
	tree.Empty ();
	//////////////////////////////////////////////////////////////
	
	for ( i = 1 ; i <= degree ; i++)
	{
		int curlen = formtree.GetLength ();
		int pos=0 , j = 0 ;
		while (j < curlen)
		{
			if(formtree[j] == a[1] )
			{
			
				tree += unit[1];
				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);
}

int CMyTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	//////初始化///////////////////////////////////////////////////
	for (int i = 0 ; i <1024 ; i++ )
	{
		stack[i].x =  0 ;
		stack[i].direction = NULL ;
	}
	stackpushpos = 0 ;
	formtree  = unit[0] ;//公理
	tree.Empty ();
	//////////////////////////////////////////////////////////////
	
	for ( i = 1 ; i <= degree ; i++)
	{
		int curlen = formtree.GetLength ();
		int pos=0 , j = 0 ;
		while (j < curlen)
		{
			if(formtree[j] == a[1] )
			{
				
				tree += unit[1];
				j++;
				pos = tree.GetLength () -1 ;
			}
			else 
			{	
				
				tree += formtree[j] ;
				pos ++;
				j++;
			}
		}
		
		formtree = tree ;
		
		tree.Empty ();
	}
	tree = formtree ;
	Invalidate();
	
	return 0;
}

⌨️ 快捷键说明

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