📄 modeldoc.cpp
字号:
// modelDoc.cpp : implementation of the CModelDoc class
//
#include "stdafx.h"
#include "model.h"
#include "stdio.h"
#include "modelDoc.h"
#include "modelView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CModelDoc
IMPLEMENT_DYNCREATE(CModelDoc, CDocument)
BEGIN_MESSAGE_MAP(CModelDoc, CDocument)
//{{AFX_MSG_MAP(CModelDoc)
ON_COMMAND(ID_PROCESS, OnProcess)
ON_UPDATE_COMMAND_UI(ID_PROCESS, OnUpdateProcess)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CModelDoc construction/destruction
CModelDoc::CModelDoc()
{
// TODO: add one-time construction code here
}
CModelDoc::~CModelDoc()
{
}
BOOL CModelDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
static POSITION pos = this->GetFirstViewPosition();
CModelView* pView = (CModelView*)this->GetNextView(pos);
if(pView != NULL)
pView->GetParent()->ShowWindow(SW_SHOWMAXIMIZED);
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CModelDoc serialization
void CModelDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CModelDoc diagnostics
#ifdef _DEBUG
void CModelDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CModelDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CModelDoc commands
void CModelDoc::OnProcess()
{
// TODO: Add your command handler code here
double a, b;
// int i,x,y;
GetLine(&lpoints, &rpoints, &a, &b);
// FILE *fp;
// fp = fopen("E:\test.txt","w+");
// if (!fp)
// return;
// for(i=0;i<mid.GetSize();i++)
// {
// x = mid.GetAt(i).x;
// y = mid.GetAt(i).y;
// fprintf(fp,"%d,%d",&x,&y);
// }
// fclose(fp);
count = 0;
BuildT(BT);
static POSITION pos = this->GetFirstViewPosition();
CModelView* pView = (CModelView*)this->GetNextView(pos);
if(pView != NULL)
{
CRect rc;
pView->GetParent()->GetWindowRect(&rc);
pView->GetParent()->InvalidateRect(rc,TRUE);
}
}
void CModelDoc::OnUpdateProcess(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable( lpoints.GetUpperBound() > -1 && rpoints.GetUpperBound() > -1);
}
CPoint CModelDoc::GetMiddlePoint(PTArray *x)
{
CPoint m;
int i;
m.x = 0;
m.y = 0;
for (i=0;i<x->GetSize();i++)
{
m.x += x->GetAt(i).x;
m.y += x->GetAt(i).y;
}
// if (x->GetSize() == 0)
// return false;
m.x /= x->GetSize();
m.y /= x->GetSize();
return m;
}
void CModelDoc::GetLine(PTArray *l, PTArray *r, double *a, double *b)
{
PTArray ll, lr, rl, rr;
CPoint lm,rm,m;
lm = GetMiddlePoint(l);
rm = GetMiddlePoint(r);
if(rm.y != lm.y)
{ //y=ax+b
*a = (lm.x - rm.x) / (lm.y - rm.y); //分割线斜率
*b = (lm.y+rm.y)/2 - (lm.x+rm.x)*(*a)/2; //分割线在Y轴上的截距
}
else
{
*a = INT_MAX;
*b = (lm.x+rm.x)/2; //分割线在X轴上的截距
}
m.x = (lm.x+rm.x)/2;
m.y = (lm.y+rm.y)/2;
aa.Add(*a);
bb.Add(*b);
mid.Add(m);
for(int i = 0; i < l->GetSize(); i++)
{
if(*a == INT_MAX)
{
if(l->GetAt(i).x > m.x)
ll.Add(l->GetAt(i));
else
lr.Add(l->GetAt(i));
}
else
{
if(*a * l->GetAt(i).x + *b - l->GetAt(i).y < 0)
ll.Add(l->GetAt(i));
else
lr.Add(l->GetAt(i));
}
}
for(i = 0; i < r->GetSize(); i++)
{
if(*a == INT_MAX)
{
if(r->GetAt(i).x > m.x)
rl.Add(r->GetAt(i));
else
rr.Add(r->GetAt(i));
}
else
{
if(*a * r->GetAt(i).x + *b -r->GetAt(i).y < 0)
rl.Add(r->GetAt(i));
else
rr.Add(r->GetAt(i));
}
}
if(ll.GetSize() > 0 && rl.GetSize() > 0) //二叉树先序遍历
GetLine(&ll, &rl, a, b);
else
{
m.x = 0;
m.y = 0;
*a = 0;
*b = 0;
aa.Add(*a);
bb.Add(*b);
mid.Add(m);
}
if(lr.GetSize() > 0 && rr.GetSize() > 0)
GetLine(&lr, &rr, a, b);
else
{
m.x = 0;
m.y = 0;
*a = 0;
*b = 0;
aa.Add(*a);
bb.Add(*b);
mid.Add(m);
}
}
void CModelDoc::BuildT(BiTree &T)
{
if(mid.GetAt(count).x == 0 && mid.GetAt(count).y == 0) {T=0;count++;return;}
T=new BiTNode;
T->data = count;
count ++;
BuildT(T->lchild);
BuildT(T->rchild);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -