📄 inclusiontestdlg.cpp
字号:
// InclusionTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "InclusionTest.h"
#include "InclusionTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
inline int SignedArea( CPoint *P1, CPoint *P2, CPoint *Pt) //内联函数
{
return ( ( P2->x - P1->x) * ( Pt->y - P1->y)
- (Pt->x - P1->x) * (P2->y - P1->y));
}
bool CInclusionTestDlg::SingedAreaAlgrth( CPoint *pt) //判断点是否在多边形以内
{
int k = 0;
vector<CPoint *>::iterator it;
for (it = vPolygon.begin(); it < vPolygon.end() - 1; it++)
{
if ( (*(it))->y <= pt->y)
{
if ( (*(it + 1))->y > pt->y)
{
if (SignedArea( *it, *(it + 1), pt) > 0)
++k;
}
}
else
{
if ( (*(it + 1))->y <= pt->y)
{
if (SignedArea( *it, *(it + 1), pt) < 0)
--k;
}
}
if (SignedArea( *it, *(it + 1), pt) == 0) //点在多边形上
flag = true;
}
if ( k == 0)
{
return false;
}
return true;
}
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CInclusionTestDlg dialog
CInclusionTestDlg::CInclusionTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CInclusionTestDlg::IDD, pParent),
m_state( setpoints ),
flag( false )
{
//{{AFX_DATA_INIT(CInclusionTestDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CInclusionTestDlg::~CInclusionTestDlg()
{
if( m_state == testpoint)
vPolygon.erase(vPolygon.end() - 1);
vector<CPoint *>::iterator it;
for (it = vPolygon.begin(); it < vPolygon.end(); ++it)
{
delete *it;
}
}
void CInclusionTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CInclusionTestDlg)
DDX_Control(pDX, IDC_DRAWPOLYGON, m_btnDraw);
DDX_Control(pDX, IDC_POINTTEST, m_btnTest);
DDX_Control(pDX, IDOK, m_btnOK);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CInclusionTestDlg, CDialog)
//{{AFX_MSG_MAP(CInclusionTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDC_DRAWPOLYGON, OnDrawpolygon)
ON_BN_CLICKED(IDC_POINTTEST, OnPointtest)
ON_WM_SETCURSOR()
ON_WM_CANCELMODE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CInclusionTestDlg message handlers
BOOL CInclusionTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_bmpBackGround.LoadBitmap( IDB_BACKGROUND );
m_btnDraw.LoadBitmaps( IDB_DRAWUP, IDB_DRAWDOWN);
m_btnDraw.SizeToContent();
m_btnTest.LoadBitmaps( IDB_TESTUP, IDB_TESTDOWN);
m_btnTest.SizeToContent();
m_btnOK.LoadBitmaps( IDB_EXITUP, IDB_EXITDOWN);
m_btnOK.SizeToContent();
return TRUE; // return TRUE unless you set the focus to a control
}
void CInclusionTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CInclusionTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this);
//change background
CDC memDC;
memDC.CreateCompatibleDC( &dc );
BITMAP bitmap;
m_bmpBackGround.GetBitmap( &bitmap);
CRect rect;
GetClientRect( rect );
CBitmap *pOldBitmap;
pOldBitmap = memDC.SelectObject( &m_bmpBackGround);
dc.StretchBlt( 0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
memDC.SetBkMode(TRANSPARENT);
memDC.SelectObject( pOldBitmap );
// CBrush m_brush;
// m_brush.CreateSolidBrush( RGB(0, 0, 0) );
CWnd *pwnd;
pwnd = GetDlgItem( IDC_DISPLAY );
pwnd->GetClientRect( m_rect );
// dc.FillRect(m_rect, &m_brush);
dc.FillSolidRect(0+11, 0+11, m_rect.Width(), m_rect.Height(), RGB(0, 0, 0));
//
//CBrush *pOldBrush = dc->SelectObject( m_brush );
//memDC.FillRect( m_rect, &m_brush );
// draw the polygon
CPen m_pen;
m_pen.CreatePen( PS_SOLID, 1, RGB(0, 255, 0));
CPen *pOldPen = dc.SelectObject( &m_pen );
vector<CPoint *>::iterator it;
for (it = vPolygon.begin(); it<vPolygon.end();)
{
dc.MoveTo(**it);
++it;
if (it < vPolygon.end())
dc.LineTo(**it);
else
dc.LineTo(**vPolygon.begin());
}
dc.SelectObject( &pOldPen );
}
// CDialog::OnPaint();
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CInclusionTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CInclusionTestDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
CWnd *pwnd;
pwnd = GetDlgItem( IDC_DISPLAY );
pwnd -> GetClientRect( m_rect );
if (m_rect.PtInRect( point ))
{
if ( m_state == setpoints)
{
CPoint *p = new CPoint;
p->x = point.x;
p->y = point.y;
vPolygon.push_back(p); //将一个值为P的单元插入到控制序列的尾部
Invalidate(); //相当于把鼠标点位置的值放到vPolygon中
}
else
{
if (SingedAreaAlgrth( &point ))
MessageBox( "点在多边形内", "测试结果", MB_ICONEXCLAMATION );
else if (flag)
MessageBox( "点在多边形上", "测试结果", MB_ICONEXCLAMATION );
else
MessageBox( "点在多边形外", "测试结果", MB_ICONINFORMATION );
}
}
// vPolygon.push_back(p);
CDialog::OnLButtonDown(nFlags, point);
}
void CInclusionTestDlg::OnDrawpolygon()
{
if ( m_state == testpoint )
vPolygon.erase( vPolygon.end() - 1 );
vector<CPoint *>::iterator it;
for ( it = vPolygon.begin(); it < vPolygon.end(); ++it)
delete *it;
vPolygon.clear(); //calls erase( begin(), end())
m_state = setpoints;
InvalidateRect( m_rect );
}
void CInclusionTestDlg::OnPointtest()
{
if (vPolygon.size() < 3)
MessageBox("边数小于3请画一个多边形后再点击此按钮", "输入错误" ,MB_ICONERROR);
else
{
vPolygon.push_back( vPolygon.at(0));
m_state = testpoint;
}
}
BOOL CInclusionTestDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
CString sClassName;
::GetClassName(pWnd->GetSafeHwnd(),sClassName.GetBuffer(80),80);
if (m_hBtnCursor)
{
m_hBtnCursor = AfxGetApp()->LoadCursor(IDC_CURSOR);
::SetCursor(m_hBtnCursor);
return TRUE;
}
/* else
{
m_hBtnCursor = AfxGetApp() -> LoadCursor( IDC_PENCIL);
::SetCursor( m_hBtnCursor );
return TRUE;
}*/
/* if (sClassName == "Picture" && m_hBtnCursor)
{
m_hBtnCursor = AfxGetApp() -> LoadCursor( IDC_PENCIL);
::SetCursor( m_hBtnCursor );
return TRUE;
}*/
/*else if (pWnd->GetDlgCtrlID() == IDC_DISPLAY)
{
SetCursor( AfxGetApp() -> LoadCursor (IDC_PENCIL) );
}return true;
*/
return CDialog::OnSetCursor(pWnd, nHitTest, message);
}
void CInclusionTestDlg::OnCancelMode()
{
CDialog::OnCancelMode();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -