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

📄 textoutview.cpp

📁 这是用来进行模式识别的参考程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// textoutView.cpp : implementation of the CTextoutView class
//

#include "stdafx.h"
#include "textout.h"
#include "math.h"

#include "textoutDoc.h"
#include "textoutView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTextoutView

IMPLEMENT_DYNCREATE(CTextoutView, CView)

BEGIN_MESSAGE_MAP(CTextoutView, CView)
	//{{AFX_MSG_MAP(CTextoutView)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_START, OnStart)
	ON_COMMAND(ID_RESTART, OnRestart)
	ON_COMMAND(ID_UNDOSORT, OnUndosort)
	ON_COMMAND(ID_ONE, OnOne)
	ON_COMMAND(ID_TWO, OnTwo)
	ON_COMMAND(ID_THREE, OnThree)
	ON_WM_CHAR()
	ON_WM_MOUSEMOVE()
	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()

/////////////////////////////////////////////////////////////////////////////
// CTextoutView construction/destruction
myPoint::myPoint()
{	
}
double line::Getk1()
{
	double k;
	if(point1[0].x==point1[1].x)
		return 10000;
 	k=(double)(point1[0].y-point1[1].y)/(double)(point1[0].x-point1[1].x);
	return k;
}

CPoint line::GetMid1()
{
	CPoint p;
	p.x=(int)((point1[0].x+point1[1].x)/2);
	p.y=(int)((point1[0].y+point1[1].y)/2);
	return p;
}

BOOL line::IsFrame1()//判断该线在不在边框上
{
	if(Getk1()==0)
	{
		if(point1[0].y==0||point1[0].y==1000)
			return 1;
		else return 0;
	}
	else if(Getk1()==10000)
	{
		if(point1[0].x==0||point1[0].x==1000)
			return 1;
		else return 0;
	}
	else return 0;
}

area1::area1()
{
	Cnt1=0;
}

void area1::DelLine1(int iLine)
{
	if(iLine==Cnt1-1)
	{
		Cnt1--;
	}
	else
	{
		myLine1[iLine]=myLine1[Cnt1-1];
		Cnt1--;
	}//iLine要保证比Cnt小
}
double line::Getk()
{
	double k;
	if(point[0].x==point[1].x)
		return 10000;
 	k=(double)(point[0].y-point[1].y)/(double)(point[0].x-point[1].x);
	return k;
}

CPoint line::GetMid()
{
	CPoint p;
	p.x=(int)((point[0].x+point[1].x)/2);
	p.y=(int)((point[0].y+point[1].y)/2);
	return p;
}

BOOL line::IsFrame()//判断该线在不在边框上
{
	if(Getk()==0)
	{
		if(point[0].y==0||point[0].y==1000)
			return 1;
		else return 0;
	}
	else if(Getk()==10000)
	{
		if(point[0].x==0||point[0].x==1000)
			return 1;
		else return 0;
	}
	else return 0;
}

area::area()
{
	Cnt=0;
}

void area::DelLine(int iLine)
{
	if(iLine==Cnt-1)
	{
		Cnt--;
	}
	else
	{
		myLine[iLine]=myLine[Cnt-1];
		Cnt--;
	}//iLine要保证比Cnt小
}

CTextoutView::CTextoutView()
{
	oCnt=0;
	xCnt=0;
	iCnt=0;
	sCnt=0;
	flag=0;
    wCnt=0;
	MyArea[0].myLine[0].point[0].x=0;
    MyArea[0].myLine[0].point[0].y=0;
	MyArea[0].myLine[0].point[1].x=1000;
	MyArea[0].myLine[0].point[1].y=0;
	MyArea[0].myLine[0].LineSide=1;

    MyArea[0].myLine[1].point[0].x=0;
    MyArea[0].myLine[1].point[0].y=0;
	MyArea[0].myLine[1].point[1].x=0;
	MyArea[0].myLine[1].point[1].y=1000;
	MyArea[0].myLine[1].LineSide=1;

	MyArea[0].myLine[2].point[0].x=1000;
    MyArea[0].myLine[2].point[0].y=0;
	MyArea[0].myLine[2].point[1].x=1000;
	MyArea[0].myLine[2].point[1].y=1000;
	MyArea[0].myLine[2].LineSide=0;

	MyArea[0].myLine[3].point[0].x=0;
    MyArea[0].myLine[3].point[0].y=1000;
	MyArea[0].myLine[3].point[1].x=1000;
	MyArea[0].myLine[3].point[1].y=1000;
	MyArea[0].myLine[3].LineSide=0;

	MyArea[0].Cnt=4;
	
	AreaCnt=1;
	iCnt1=0;
    
	MyArea1[0].myLine1[0].point1[0].x=0;
    MyArea1[0].myLine1[0].point1[0].y=0;
	MyArea1[0].myLine1[0].point1[1].x=1000;
	MyArea1[0].myLine1[0].point1[1].y=0;
	MyArea1[0].myLine1[0].LineSide1=1;

    MyArea1[0].myLine1[1].point1[0].x=0;
    MyArea1[0].myLine1[1].point1[0].y=0;
	MyArea1[0].myLine1[1].point1[1].x=0;
	MyArea1[0].myLine1[1].point1[1].y=1000;
	MyArea1[0].myLine1[1].LineSide1=1;

	MyArea1[0].myLine1[2].point1[0].x=1000;
    MyArea1[0].myLine1[2].point1[0].y=0;
	MyArea1[0].myLine1[2].point1[1].x=1000;
	MyArea1[0].myLine1[2].point1[1].y=1000;
	MyArea1[0].myLine1[2].LineSide1=0;

	MyArea1[0].myLine1[3].point1[0].x=0;
    MyArea1[0].myLine1[3].point1[0].y=1000;
	MyArea1[0].myLine1[3].point1[1].x=1000;
	MyArea1[0].myLine1[3].point1[1].y=1000;
	MyArea1[0].myLine1[3].LineSide1=0;

	MyArea1[0].Cnt1=4;
	
	AreaCnt1=1;
}

CTextoutView::~CTextoutView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTextoutView drawing

void CTextoutView::OnDraw(CDC* pDC)
{
	CTextoutDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	GetParentFrame()->SetWindowText(_T("分段逼近解决两类问题 "));

	CBrush lBrush,rBrush,rlBrush;//rlbrush代表第三类
	
	int Temp;
	lBrush.CreateSolidBrush(RGB(255,0,0));
	rBrush.CreateSolidBrush(RGB(0,0,255));
	rlBrush.CreateSolidBrush(RGB(0,255,0));
    
	pDC->SelectObject(&lBrush);
	for(Temp=0;Temp<oCnt;Temp++)
    pDC->Ellipse(mpO[Temp].x-5,mpO[Temp].y-5,mpO[Temp].x+5,mpO[Temp].y+5);
	pDC->SelectObject(&rBrush);
	for(Temp=0;Temp<wCnt;Temp++)
	pDC->Rectangle(mpW[Temp].x-4,mpW[Temp].y-4,mpW[Temp].x+4,mpW[Temp].y+4);
	pDC->SelectObject(&rlBrush);
	for(Temp=0;Temp<sCnt;Temp++)
    pDC->Ellipse(mpS[Temp].x-5,mpS[Temp].y-5,mpS[Temp].x+5,mpS[Temp].y+5);


	for(Temp=0;Temp<iCnt;Temp++)
	{
		pDC->MoveTo(ResultLine[Temp].point[0]);
		pDC->LineTo(ResultLine[Temp].point[1]);
	}//输出结果折线!!!!!!!!!

	for(Temp=0;Temp<iCnt1;Temp++)
	{
		pDC->MoveTo(ResultLine1[Temp].point1[0]);
		pDC->LineTo(ResultLine1[Temp].point1[1]);
	}//输出结果折线!!!!!!!!!
/*	for(Temp=AreaCnt-1;Temp>=0;Temp--)
	{
		for(int temp=MyArea[Temp].Cnt-1;temp>=0;temp--)
		{
			pDC->MoveTo(MyArea[Temp].myLine[temp].point[0]);
			pDC->LineTo(MyArea[Temp].myLine[temp].point[1]);
		}
	}*/
	//保留以前的输出形式
}

/////////////////////////////////////////////////////////////////////////////
// CTextoutView printing

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

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


/////////////////////////////////////////////////////////////////////////////
// CTextoutView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTextoutView message handler
void CTextoutView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if(flag==1){
	  if(oCnt<100)
	  {
		mpO[oCnt].x=point.x;
		mpO[oCnt].y=point.y;
		mpO[oCnt].iBelong=0;
		oCnt++;
	  }
	   else AfxMessageBox("You can not input more than 100 points each kind!");
	}
	if(flag==2){
      if(wCnt<100)
	  {
		mpW[wCnt].x=point.x;
		mpW[wCnt].y=point.y;
    	mpW[wCnt].iBelong=0;
		wCnt++;
	  }
	   else AfxMessageBox("You can not input more than 100 points each kind!");

	}
	if(flag==3){
      if(sCnt<100)
	  {
		mpS[sCnt].x=point.x;
		mpS[sCnt].y=point.y;
		mpS[sCnt].iBelong=0;
		sCnt++;
	  }
	  else AfxMessageBox("You can not input more than 100 points each kind!");
	}
	
	CView::OnLButtonDown(nFlags, point);
	CTextoutView::Invalidate(1);
}

 

void CTextoutView::FindMin(int iArea)
{
    long Temp1,Temp2;
	long MinDis=1000000;
	long CurDis;
	for(Temp1=0;Temp1<oCnt;Temp1++)
	{
        for(Temp2=0;Temp2<xCnt;Temp2++)
		{
			if(mpO[Temp1].iBelong==iArea&&mpX[Temp2].iBelong==iArea)
			{
				CurDis=Distance(mpX[Temp2],mpO[Temp1]);
				if(CurDis<MinDis)
				{
					MinDis=CurDis;
					minline.point[0].x=mpX[Temp2].x;//最近距离连线的第一个点是x
					minline.point[0].y=mpX[Temp2].y;
					minline.point[1].x=mpO[Temp1].x;//第二个点是o
					minline.point[1].y=mpO[Temp1].y;
				}
			}
		}
	}
}

long CTextoutView::Distance(CPoint p1,CPoint p2)
{
	long temp;
	temp=(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
    return(temp);
}

void CTextoutView::MidLine()
{ 
   double k1,k2;
   CPoint p1,p2;
   double x,y;
   x=(minline.point[0].x+minline.point[1].x)/2;
   y=(minline.point[0].y+minline.point[1].y)/2;
   k1=minline.Getk();
   if(k1==0)
	   k2=10000;
   else k2=-1/k1;
   if(k2>1||k2<-1)
   {
	   p1.y=0;
	   p1.x=(int)(x-y/k2);
	   p2.y=1000;
	   p2.x=(int)((1000-y)/k2+x);
   }
   else
   {
	   p1.x=0;
	   p1.y=(int)(y-k2*x);
	   p2.x=1000;
	   p2.y=(int)(y+k2*(1000-x));
   }
   midline.point[0].x=p1.x;
   midline.point[0].y=p1.y;
   midline.point[1].x=p2.x;
   midline.point[1].y=p2.y;
}

void CTextoutView::SplitArea(int iArea)
{
   CPoint p1;
   CPoint p2;
   int flag=0;
   int Temp;
   double k1,k2;
   int x,y,x1,y1,x2,y2;
   k1=midline.Getk();
   x1=midline.point[0].x;
   y1=midline.point[0].y;

⌨️ 快捷键说明

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