📄 computerview.cpp
字号:
// computerView.cpp : implementation of the CComputerView class
//
#include "stdafx.h"
#include "computer.h"
#include "computerDoc.h"
#include "computerView.h"
#include <math.h>
#include <stdlib.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
typedef struct two_dim
{
double x;
double y;
}TWO;
typedef struct three_dim
{
double x;
double y;
double z;
}THREE;
bool exp1=false,exp2=false,exp3=false,flag=false,flag1=false;
int co_x,co_y,x=0,y=0;
int radius=100,margin=200,i;
CDC* pdc1;
CPoint givepoint;
/////////////////////////////////////////////////////////////////////////////
// CComputerView
IMPLEMENT_DYNCREATE(CComputerView, CView)
BEGIN_MESSAGE_MAP(CComputerView, CView)
//{{AFX_MSG_MAP(CComputerView)
ON_COMMAND(ID_Experiment1, OnExperiment1)
ON_COMMAND(ID_Experiment2, OnExperiment2)
ON_COMMAND(ID_Experiment3, OnExperiment3)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CComputerView construction/destruction
CComputerView::CComputerView()
{
// TODO: add construction code here
}
CComputerView::~CComputerView()
{
}
BOOL CComputerView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CComputerView drawing
void CComputerView::OnDraw(CDC* pDC)
{
CComputerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pdc1=pDC;
co_x=350,co_y=200;
pDC->SetViewportOrg(co_x,co_y);
if(exp1||flag1)
{
Classify(x,y);
}
if(exp2)
{
double x1,y1;
double Pi=4*atan(1);
pDC->MoveTo(radius,0);
CPen pen(PS_SOLID,1,RGB(205,38,38));
CPen *oldPen=pDC->SelectObject(&pen);
for(i=0;i<=360;i=i+1)
{
x1=radius*cos(2*Pi*i/360);
y1=radius*sin(2*Pi*i/360);
pDC->LineTo(int(x1),int(y1));
}
CPen Pen1(PS_SOLID, 1, RGB(0,0,255));
pDC->SelectObject(&Pen1);
pDC->MoveTo(0,0);
pDC->LineTo(-margin,0);
pDC->LineTo(-margin,margin);
pDC->LineTo(0,margin);
pDC->LineTo(0,0);
}
}
/////////////////////////////////////////////////////////////////////////////
// CComputerView printing
BOOL CComputerView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CComputerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CComputerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CComputerView diagnostics
#ifdef _DEBUG
void CComputerView::AssertValid() const
{
CView::AssertValid();
}
void CComputerView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CComputerDoc* CComputerView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CComputerDoc)));
return (CComputerDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
void CComputerView::OnExperiment1()
{
// TODO: Add your command handler code here
exp1=true;
exp2=false;
exp3=false;
Invalidate();
}
void CComputerView::OnExperiment2()
{
// TODO: Add your command handler code here
exp1=false;
exp2=true;
exp3=false;
flag1=false;
Invalidate();
}
void CComputerView::OnExperiment3()
{
// TODO: Add your command handler code here
exp1=false;
exp2=true;
exp3=true;
flag1=false;
Invalidate();
}
void CComputerView::OnLButtonDown(UINT nFlags, CPoint point)
{
//TODO: Add your message handler code here and/or call default
//COLORREF crColor=0x00ff00;
CPoint givepoint=point;
flag=true;
flag1=false;
//pDC->SetPixel(givepoint.x,givepoint.y,crColor);
int p1,q1,dist1,p2,q2,dist2;
p1=givepoint.x-co_x;
q1=givepoint.y-co_y;
dist1=int(sqrt(p1*p1+q1*q1));
p2=int(abs(givepoint.x+margin/2-co_x));
q2=int(abs(givepoint.y-margin/2-co_y));
dist2=Max(p2,q2);
if(exp1)
{
}
else if(exp3)
{
if(dist1<100)
{
if(dist2<100)
AfxMessageBox( "属于圆与正方形交集");
else
AfxMessageBox( "只属于圆");
}
else
{
if(dist2<100)
AfxMessageBox( "只属于正方形");
else
AfxMessageBox( "属于背景");
}
}
CView::OnLButtonDown(nFlags, point);
}
void CComputerView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
flag=false;
if(exp1)
{
flag1=true;
srand( (unsigned)time( NULL ));
x=rand()%15;
y=rand()%15;
Invalidate();
}
CView::OnLButtonUp(nFlags, point);
}
int CComputerView::Max(int a, int b)
{
if(abs(a)>abs(b))
return a;
else
return b;
}
void CComputerView::Classify(int cent_x, int cent_y)
{
int length=20,len=length/2,cent_z=34,z1=cent_z-len,z2=cent_z+len;
int f_x1=cent_x+len,f_x2=cent_x-len,
f_y1=cent_y+len,f_y2=cent_y-len;
THREE three[8]={{f_x1,f_y2,z1},{f_x2,f_y2,z1},{f_x2,f_y1,z1},{f_x1,f_y1,z1},
{f_x1,f_y2,z2},{f_x2,f_y2,z2},{f_x2,f_y1,z2},{f_x1,f_y1,z2}};
TWO two[8];
int f=8,i;
for(i=0;i<8;++i)
{
two[i].x=15*f*three[i].x/(f-three[i].z);
two[i].y=15*f*three[i].y/(f-three[i].z);
}
CPen pen(PS_SOLID,1,RGB(205,38,38));
CPen *oldPen=pdc1->SelectObject(&pen);
pdc1->MoveTo(int(two[0].x),int(two[0].y));
for(i=1;i<4;++i)
pdc1->LineTo(int(two[i].x),int(two[i].y));
pdc1->LineTo(int(two[0].x),int(two[0].y));
pdc1->MoveTo(int(two[4].x),int(two[4].y));
CPen pen1(PS_DASH,1,RGB(0,0,255));
pdc1->SelectObject(&pen1);
for(i=4;i<8;++i)
pdc1->LineTo(int(two[i].x),int(two[i].y));
pdc1->LineTo(int(two[4].x),int(two[4].y));
CPen pen2(PS_DASH,1,RGB(224,102,255));
pdc1->SelectObject(&pen2);
for(i=0;i<4;++i)
{
pdc1->MoveTo(int(two[0+i].x),int(two[0+i].y));
pdc1->LineTo(int(two[4+i].x),int(two[4+i].y));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -