www.pudn.com > 用形态学方法提取骨架线.rar > LogView.cpp


// LogView.cpp : implementation of the CLogView class 
// 
 
#include "stdafx.h" 
#include "Log.h" 
#include "math.h" 
 
#include "LogDoc.h" 
#include "LogView.h" 
 
#include "WrapBitmap.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CLogView 
 
IMPLEMENT_DYNCREATE(CLogView, CView) 
 
BEGIN_MESSAGE_MAP(CLogView, CView) 
	//{{AFX_MSG_MAP(CLogView) 
	ON_COMMAND(ID_VIEW_LEAF, OnViewLeaf) 
	ON_COMMAND(ID_VIEW_TREE, OnViewTree) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CLogView construction/destruction 
 
CLogView::CLogView() 
{ 
	m_bFirstTree=true; 
} 
 
CLogView::~CLogView() 
{ 
} 
 
BOOL CLogView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CLogView drawing 
 
void CLogView::OnDraw(CDC* pDC) 
{ 
	CLogDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
 
	if(!memDC.m_hDC) 
	{ 
		memDC.CreateCompatibleDC(pDC); 
	}	 
	CWrapBitmap* pWrapBitmap=pDoc->GetWrapBitmap(); 
	if(!pWrapBitmap->GetpBitmap()) 
		return; 
	memDC.SelectObject(pWrapBitmap->GetpBitmap()); 
	pDC->BitBlt(0,0,pWrapBitmap->GetWidth(),pWrapBitmap->GetHeight(),&memDC,0,0,SRCCOPY); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CLogView diagnostics 
 
#ifdef _DEBUG 
void CLogView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CLogView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CLogDoc* CLogView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLogDoc))); 
	return (CLogDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CLogView message handlers 
 
 
 
void CLogView::DrawLeaf(CClientDC* pDC) 
{ 
	int randNum,k; 
	double ifs[4][6]={	0,0,0,0.16,0,0, 
						0.85,0.04,-0.04,0.85,0,1.6, 
						0.2,-0.26,0.23,0.22,0,1.6, 
						-0.15,0.28,0.26,0.24,0,0.44 
						}; 
 
	double x=0,y=0; 
	for(int i=1;i<30000;i++) 
	{ 
		randNum=rand()%100+1; 
		if(randNum<=85)	k=1; 
		if(randNum==86)	k=0; 
		if(randNum>86&&k<94)	k=2; 
		if(randNum>=94)	k=3; 
		x=ifs[k][0]*x+ifs[k][1]*y+ifs[k][4]; 
		y=ifs[k][2]*x+ifs[k][3]*y+ifs[k][5]; 
		pDC->SetPixel((int)(200+400*x/10),(int)(400*y/10),RGB(0,255,0)); 
	} 
} 
 
 
 
void CLogView::OnViewLeaf()  
{ 
	RedrawWindow(); 
	CClientDC dc(this); 
	DrawLeaf(&dc); 
} 
 
 
 
void CLogView::DrawTree(CClientDC *pDC, double Length, int n) 
{ 
	const double PI=3.1415927; 
	const N=10; 
	const double END=2002.12; 
	static double rootX=300.0,rootY=400.0; 
	static double lArea=0.7,sArea=0.5; 
 
	static double branch[][2]={{40.0,0.5},{-60.0,0.48},{3.0,0.85},END}; 
 
	static double LPX=rootX,LPY=rootY,ANGLE=90; 
 
	if(m_bFirstTree==false) 
	{ 
		rootX=300.0,rootY=600.0; 
		lArea=0.7,sArea=0.5; 
		LPX=rootX,LPY=rootY,ANGLE=90; 
		m_bFirstTree=true; 
	} 
	int i=0; 
	if(n>=N) 
		return; 
	if(n==1) 
	{ 
		double x=LPX+Length*cos(ANGLE*PI/180); 
		double y=LPY-Length*sin(ANGLE*PI/180); 
		pDC->MoveTo((int)LPX,(int)LPY); 
		pDC->LineTo((int)x,(int)y); 
		LPX=x,LPY=y; 
		DrawTree(pDC,Length,n+1); 
	} 
	else 
		while(branch[i][0]!=END) 
		{ 
			srand( (unsigned)time( NULL ) ); 
			int nAlpha,nTemp=0; 
			nAlpha=(int)(branch[i][0]*(rand()/RAND_MAX*sArea+(nTemp-sArea))); 
			ANGLE+=nAlpha; 
			ANGLE=ANGLE-(int)ANGLE+(int)ANGLE%360; 
			nTemp=(int)(branch[i][1]*Length*((double)rand()/(double)RAND_MAX*lArea+(nTemp-lArea))); 
			nTemp=-nTemp; 
			double x=LPX+nTemp*cos(ANGLE*PI/180); 
			double y=LPY-nTemp*sin(ANGLE*PI/180); 
			pDC->MoveTo((int)LPX,(int)LPY); 
			pDC->LineTo((int)x,(int)y); 
			LPX=x,LPY=y; 
			DrawTree(pDC,branch[i][1]*Length,n+1); 
 
			ANGLE+=180; 
			ANGLE=ANGLE-(int)ANGLE+(int)ANGLE%360; 
 
			LPX+=nTemp*cos(ANGLE*PI/180); 
			LPY-=nTemp*sin(ANGLE*PI/180); 
 
			ANGLE+=180-nAlpha; 
			ANGLE=ANGLE-(int)ANGLE+(int)ANGLE%360; 
			i++; 
 
		} 
 
} 
 
void CLogView::OnViewTree()  
{ 
	RedrawWindow(); 
	CClientDC dc(this); 
 
	DrawTree(&dc,180.0,1); 
	m_bFirstTree=false; 
}