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

📄 modelview.cpp

📁 一个模式识别的小程序
💻 CPP
字号:
// modelView.cpp : implementation of the CModelView class
//

#include "stdafx.h"
#include "model.h"

#include "modelDoc.h"
#include "modelView.h"

#define WSIZE 2000

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

/////////////////////////////////////////////////////////////////////////////
// CModelView

IMPLEMENT_DYNCREATE(CModelView, CView)

BEGIN_MESSAGE_MAP(CModelView, CView)
	//{{AFX_MSG_MAP(CModelView)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CModelView construction/destruction

CModelView::CModelView()
{
	// TODO: add construction code here

}

CModelView::~CModelView()
{
}

BOOL CModelView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CModelView drawing

void CModelView::OnDraw(CDC* pDC)
{
	CModelDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
//	int i;
	
	if (!pDoc)
		return;

	draw2(pDoc);
//	for(i=0;i<pDoc->mid.GetSize();i++)
//		drawpoint1(pDoc->mid.GetAt(i));
	draw1(pDC,pDoc);
}

/////////////////////////////////////////////////////////////////////////////
// CModelView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CModelView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CModelView message handlers

void CModelView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	
}

void CModelView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CModelDoc* pDoc = GetDocument();	
	pDoc->lpoints.Add(point);
	drawpoint1(point);
	
	CView::OnLButtonDown(nFlags, point);
}

void CModelView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CModelDoc* pDoc = GetDocument();	
	pDoc->rpoints.Add(point);
	drawpoint2(point);
	
	CView::OnRButtonDown(nFlags, point);
}

void CModelView::drawpoint1(CPoint point)
{
	CDC *dc=GetDC();
	CPen newpen(PS_SOLID,1,RGB(0,0,255)), * oldpen;
	oldpen=dc->SelectObject(&newpen);
	dc->MoveTo(point.x-2,point.y-2);
	dc->LineTo(point.x+3,point.y+3);
	dc->MoveTo(point.x-2,point.y+2);
	dc->LineTo(point.x+3,point.y-3);
	dc->SelectObject(oldpen);
	ReleaseDC(dc);

}

void CModelView::drawpoint2(CPoint point)
{	
	CDC *dc=GetDC();
	CPen newpen(PS_SOLID,1,RGB(255,0,0)), * oldpen;
	oldpen=dc->SelectObject(&newpen);
	dc->MoveTo(point.x-2,point.y-2);
	dc->LineTo(point.x+3,point.y+3);
	dc->MoveTo(point.x-2,point.y+2);
	dc->LineTo(point.x+3,point.y-3);
	dc->SelectObject(oldpen);
	ReleaseDC(dc);
}

void CModelView::draw1(CDC *pDC, CModelDoc *pDoc) //点数多易出错
{
	CPoint start,end;
	int j,tag;

	//第一条线
	if(pDoc->aa.GetSize())
	{
		start.x = pDoc->mid.GetAt(0).x - 2000;
		end.x = pDoc->mid.GetAt(0).x + 2000;
		start.y = (int)(pDoc->mid.GetAt(0).y - pDoc->aa.GetAt(0)*2000);
		end.y = (int)(pDoc->mid.GetAt(0).y + pDoc->aa.GetAt(0)*2000);

		pDC->MoveTo(start);
		pDC->LineTo(end);
		Sleep(1000);
	}

	for(int i=1;i<pDoc->aa.GetSize();i++)
	{
		if(pDoc->mid.GetAt(i).x !=0 && pDoc->mid.GetAt(i).y !=0)
		{
			FindFather(pDoc->BT,i,j,tag);
///////////////////////////////////////////////////////////////////////////////////////////
			if(pDoc->aa.GetAt(j) == INT_MAX)		//x = b1
			{
				if(pDoc->aa.GetAt(j) == pDoc->aa.GetAt(i))
				{
					start.x = (int)pDoc->bb.GetAt(i);
					end.x = (int)pDoc->bb.GetAt(i);
					start.y = 2000;
					end.y = -2000;
				}
				else if(tag == 1)
				{
					start.x = (int)pDoc->bb.GetAt(j);		//start.x = b1
					end.x = pDoc->mid.GetAt(i).x + 2000;
					start.y = (int)(start.x * pDoc->aa.GetAt(i) + pDoc->bb.GetAt(i));	//y=a2*x+b2 
					end.y = (int)(end.x * pDoc->aa.GetAt(i) + pDoc->bb.GetAt(i));
				}
				else
				{
					start.x = (int)pDoc->bb.GetAt(j);
					end.x = pDoc->mid.GetAt(i).x - 2000;
					start.y = (int)(start.x * pDoc->aa.GetAt(i) + pDoc->bb.GetAt(i));
					end.y = (int)(end.x * pDoc->aa.GetAt(i) + pDoc->bb.GetAt(i));
				}

				pDC->MoveTo(start);
				pDC->LineTo(end);
				Sleep(1000);
			}
///////////////////////////////////////////////////////////////////////////////////////////////
			else if(pDoc->aa.GetAt(j) == 0)		//y = b1
			{
				if(pDoc->aa.GetAt(j) == pDoc->aa.GetAt(i))
				{
					start.y = (int)pDoc->bb.GetAt(i);
					end.y = (int)pDoc->bb.GetAt(i);
					start.x = 2000;
					end.x = -2000;
				}
				else if(tag == 1)
				{
					start.y = (int)pDoc->bb.GetAt(j);		//start.y = b1
					end.y = pDoc->mid.GetAt(i).y + 2000;
					if(pDoc->aa.GetAt(i)!=INT_MAX)
					{
						start.x = (int)((start.y - pDoc->bb.GetAt(i)) / pDoc->aa.GetAt(i));	//x=(y-b2)/a2
						end.x = (int)((end.y - pDoc->bb.GetAt(i)) / pDoc->aa.GetAt(i));
					}
					else
					{
						start.x = (int)pDoc->bb.GetAt(i);
						end.x = (int)pDoc->bb.GetAt(i);
					}
				}
				else
				{
					start.y = (int)pDoc->bb.GetAt(j);		//start.y = b1
					end.y = pDoc->mid.GetAt(i).y - 2000;
					if(pDoc->aa.GetAt(i)!=INT_MAX)
					{
						start.x = (int)((start.y - pDoc->bb.GetAt(i)) / pDoc->aa.GetAt(i));	//x=(y-b2)/a2
						end.x = (int)((end.y - pDoc->bb.GetAt(i)) / pDoc->aa.GetAt(i));
					}
					else
					{
						start.x = (int)pDoc->bb.GetAt(i);
						end.x = (int)pDoc->bb.GetAt(i);
					}
				}

				pDC->MoveTo(start);
				pDC->LineTo(end);
				Sleep(1000);
			}
///////////////////////////////////////////////////////////////////////////////////////////////
			else				
			{
				if(pDoc->aa.GetAt(j) == pDoc->aa.GetAt(i))
				{
					start.x = pDoc->mid.GetAt(i).x - 2000;
					end.x = pDoc->mid.GetAt(i).x + 2000;
					start.y = (int)(pDoc->mid.GetAt(i).y - pDoc->aa.GetAt(i)*2000);
					end.y = (int)(pDoc->mid.GetAt(i).y + pDoc->aa.GetAt(i)*2000);
				}
				else if(tag == 1)	//x2*a1+b1<y2
				{
					if(pDoc->aa.GetAt(i) == 0)
					{
						start.y = (int)pDoc->bb.GetAt(i);
						start.x = (int)((start.y - pDoc->bb.GetAt(j)) / pDoc->aa.GetAt(j));	//x=(y-b1)/a1
						end.y = start.y;
						end.x = start.x + 2000;
					}
					else if(pDoc->aa.GetAt(i) == INT_MAX)
					{
						start.x = (int)pDoc->bb.GetAt(i);
						start.y = (int)(start.x * pDoc->aa.GetAt(j) + pDoc->bb.GetAt(j));
						end.x = start.x;
						end.y = start.y + 2000;
					}
					else	//最普通的情形 
					{
						start.x = (int)((pDoc->bb.GetAt(i) - pDoc->bb.GetAt(j))/(pDoc->aa.GetAt(j) - pDoc->aa.GetAt(i)));	
						//start.x=(b2-b1)/(a1-a2)
						start.y = (int)(start.x * pDoc->aa.GetAt(j) + pDoc->bb.GetAt(j));	//y=a1*x+b1
						end.y = pDoc->mid.GetAt(i).y + 2000;
						end.x = (int)((end.y - pDoc->bb.GetAt(i)) / pDoc->aa.GetAt(i));	//x=(y-b2)/a2
					}
	
				}
				else
				{

					if(pDoc->aa.GetAt(i) == 0)
					{
						start.y = (int)pDoc->bb.GetAt(i);
						start.x = (int)((start.y - pDoc->bb.GetAt(j)) / pDoc->aa.GetAt(j));	//x=(y-b1)/a1
						end.y = start.y;
						end.x = start.x - 2000;
					}
					else if(pDoc->aa.GetAt(i) == INT_MAX)
					{
						start.x = (int)pDoc->bb.GetAt(i);
						start.y = (int)(start.x * pDoc->aa.GetAt(j) + pDoc->bb.GetAt(j));
						end.x = start.x;
						end.y = start.y - 2000;
					}
					else	//最普通的情形 
					{
						start.x = (int)((pDoc->bb.GetAt(i) - pDoc->bb.GetAt(j))/(pDoc->aa.GetAt(j) - pDoc->aa.GetAt(i)));	
						//start.x=(b2-b1)/(a1-a2)
						start.y = (int)(start.x * pDoc->aa.GetAt(j) + pDoc->bb.GetAt(j));	//y=a1*x+b1
						end.y = pDoc->mid.GetAt(i).y - 2000;
						end.x = (int)((end.y - pDoc->bb.GetAt(i)) / pDoc->aa.GetAt(i));	//x=(y-b2)/a2
					}
				
				}

				pDC->MoveTo(start);
				pDC->LineTo(end);
				Sleep(1000);
			}
//////////////////////////////////////////////////////////////////////////////////////////////
		}	//end for
	}	//end if
}

void CModelView::draw2(CModelDoc *pDoc)
{
	for(int i=0;i<pDoc->lpoints.GetSize();i++)
		drawpoint1(pDoc->lpoints[i]);
	for(i=0;i<pDoc->rpoints.GetSize();i++)
		drawpoint2(pDoc->rpoints[i]);
}



void CModelView::FindFather(BiTree T,int x,int &j,int &tag)
{
	if(!T)
		return;

	else
	{
		if(T->lchild) 
		{
			if(T->lchild->data == x)	{j = T->data;tag = 1;return;}
			else FindFather(T->lchild,x,j,tag);
		}
		if(T->rchild)
		{
			if(T->rchild->data == x)	{j = T->data;tag = 0;return;}
			else FindFather(T->rchild,x,j,tag);
		}
	}
}

⌨️ 快捷键说明

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