📄 分形.cpp
字号:
//#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
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::CMyTreeView()
{
unit[0]="F";
a[1]="F"; unit[1]="F-F++F-F";
len=10;
angle=60.0;
degree=4;
ox=900;
oy=450;
}
CMyTreeView::~CMyTreeView()
{
}
BOOL CMyTreeView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
void CMyTreeView::OnDraw(CDC* pDC)
{
CTreeDoc*pDoc = GetDocment();
ASSERT_VALID(pDoc);
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++;
}
}
}
/////////////////////////////////////////////////////////////
//CMyTreeView Printing
BOOL CMyTreeView::OnPreparePrinting(CPrintInfo*pInfo)
{
return DoPreparePrinting(pInfo);
}
void CMyTreeView::OnBeginPrinting(CDC* /*pDC*/,CPrintInfo*/*pInfo*/)
{
}
void CMyTreeView::OnEndPrinting(CDC* /*pDC*/,CPrintInfo*/*pInfo*/)
{
}
#ifdef _DEBUG
void CMyTreeView::AssertValid() const
{
CView::AssertValid();
}
void CMyTreeView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTreeDoc*CMyTreeView::GetDocument()
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTreeDoc)));
return(CTreeDoc*)m_pDocument;
}
#endif
void CMyTreeView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
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(UINTnScrollCode,UINTnPos,BOOLbDoScroll)
{
return CView::OnScroll(nScrollCode,nPos,bDoScrooll);
}
int CMyTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CView::OnCreate(lpCreateStruct)==-1)
return -1;
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++
}
}
fprmtree=tree;
tree.Empty();
}
tree=fortree;
Invalidate();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -