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

📄 线性分类器dlg.cpp

📁 该程序是一个对于一个对于以某一直线为边界的两类进行分类
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 线性分类器Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "线性分类器.h"
#include "线性分类器Dlg.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include"Dialogg.h"
#include"DialogT.h"
#include"DialogA.h"
#include"DialogP.h"
#include"DialogS.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_STATICz, m_z);
	DDX_Control(pDX, IDC_BUTTON6, m_b7);
	DDX_Control(pDX, IDC_BUTTON5, m_b6);
	DDX_Control(pDX, IDC_BUTTON4, m_b5);
	DDX_Control(pDX, IDC_BUTTON3, m_b4);
	DDX_Control(pDX, IDC_BUTTON2, m_b3);
	DDX_Control(pDX, IDC_BUTTON1, m_b2);
	DDX_Control(pDX, IDOK, m_b1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_ERASEBKGND()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::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
	t=0;
	m_b2.flagv=true;GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
	strcpy(m_b5.strText1,"显示检测点");
	m_b3.flagv=true;GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
	m_b4.flagv=true;GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE);
	m_b5.flagv=true;GetDlgItem(IDC_BUTTON4)->EnableWindow(FALSE);
	m_b6.flagv=true;GetDlgItem(IDC_BUTTON5)->EnableWindow(FALSE);
	m_b7.flagv=true;GetDlgItem(IDC_BUTTON6)->EnableWindow(FALSE);
	CRect rect,rects,rectp;
	GetClientRect(rect);
    GetDlgItem(IDC_STATIC1)->GetClientRect(rects);
    GetDlgItem(IDC_STATIC2)->GetWindowRect(rectp);ScreenToClient(rectp);
	int w=rectp.Width( );
	rectp.top=rects.bottom+20;
	rectp.bottom=rectp.top+rectp.Height( ) ;
	rectp.left=rect.left+rect.Width( )/2-rectp.Width( )/2;
    rectp.right=rectp.left+w;
	GetDlgItem(IDC_STATIC2)->MoveWindow(rectp,true);
     mybitmap.LoadBitmap(IDB_BITMAP1);
	 flag1=false;flag2=false;flagd=false;
	 m=0;
	 flagl=false;fa1=false;fa2=false;fa3=false;
	 x1=0;x2=0;y1=0;y2=0;	fg=false;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::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 CMyDlg::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
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyDlg::OnOK() 
{
	// TODO: Add extra validation here
	if(flag1)
		return;
	int i1=0,i2=0;
	int x,y;
	/* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
   srand( (unsigned)time( NULL ) );
	while((i1<50))
	{
            x=(int)(rand()%350);
			y=(int)(rand()%350);
			if((y>x+10)&&(x>2))
			{
				*(onep+2*i1)=x;
                 *(onep+2*i1+1)=y;
				 i1++;
			}
           
	}
	while((i2<50))
	{
            x=(int)(rand()%350);
			y=(int)(rand()%350);
			
           if((y<x-10)&&(y>2))
			{
				*(towp+2*i2)=x;
                 *(towp+2*i2+1)=y;
				 i2++;
			}
	}
    CRect rectldj;   
	GetDlgItem(IDC_STATIC2)->GetWindowRect(rectldj);ScreenToClient(rectldj);
	flag1=true;
	m_b2.flagv=false;
GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
m_b3.flagv=false;
GetDlgItem(IDC_BUTTON2)->EnableWindow(true);
m_b4.flagv=false;
GetDlgItem(IDC_BUTTON3)->EnableWindow(true);

    Invalidate();
    UpdateWindow();

	//InvalidateRect(rectldj,true);

}

BOOL CMyDlg::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	    CRect rect,rects;
		GetClientRect(rect);
        GetDlgItem(IDC_STATIC1)->GetClientRect(rects);
	    for(int i=0;i<15;i++)
		{
          rects.top=rect.top+i*3;
		  rects.bottom=rects.top+3;
          pDC->FillSolidRect(rects,RGB(188+i,212+i,249));
		}
        rect.top=rects.bottom;
//////////////////////////////////////////////////////
        CDC memdc;
		BITMAP bm1;
        mybitmap.GetObject(sizeof(BITMAP),&bm1);
		memdc.CreateCompatibleDC(pDC);
        CBitmap* oldbitmap=memdc.SelectObject(&mybitmap);
		int wide=rect.Width()/3;
		int mheight=rect.Height()/3;
	for (int j=0;j<3;j++)
	{
		for(int i=0;i<3;i++)
	{
      pDC->StretchBlt(rect.left,rect.top,wide,mheight,&memdc,0,0,bm1.bmWidth,bm1.bmHeight,SRCCOPY);
        rect.left+= wide;	
	}
	     rect.left-=wide*3;
		 rect.top+=mheight;
	}
	CRect rectp; 
	CPen *pOldPen;
	GetDlgItem(IDC_STATIC2)->GetWindowRect(rectp);ScreenToClient(rectp);
	pDC->FillSolidRect(rectp,RGB(255,255,255));
    CPen mypen1(PS_SOLID,0,RGB(0,0,255));
	CPen mypen2(PS_SOLID,0,RGB(0,255,0));
	CPen mypen3(PS_SOLID,1,RGB(255,23,125));
    CPen mypen4(PS_SOLID,1,RGB(0,0,0));
    pOldPen=pDC->SelectObject(&mypen1);
        pDC->MoveTo(rectp.left+40, rectp.bottom-30);
        pDC->LineTo(rectp.left+440, rectp.bottom-30);
        pDC->LineTo(rectp.left+430, rectp.bottom-25);
        pDC->MoveTo(rectp.left+440, rectp.bottom-30);
        pDC->LineTo(rectp.left+430, rectp.bottom-35);
        pDC->MoveTo(rectp.left+40, rectp.bottom-30);
        pDC->LineTo(rectp.left+40, rectp.bottom-400);
        pDC->LineTo(rectp.left+45, rectp.bottom-390);
	    pDC->MoveTo(rectp.left+40, rectp.bottom-400);
        pDC->LineTo(rectp.left+35, rectp.bottom-390);
        pDC->SelectObject(&mypen2);
        pDC->MoveTo(rectp.left+40, rectp.bottom-30);
        pDC->LineTo(rectp.left+390, rectp.bottom-380);
        CBrush* polidbrush;
	pDC->SelectObject(&mypen3);
		if(flag1)
		{
			pDC->SelectObject(&mypen3);
			for(int i1=0;i1<50;i1++)
			{
               
				

				CRect rect(rectp.left+40+*(onep+2*i1)-1,rectp.bottom-30-*(onep+2*i1+1)-1,rectp.left+40+*(onep+2*i1)+1,rectp.bottom-30-*(onep+2*i1+1)+1);
				
                pDC->Ellipse(rect);
			

			}
          pDC->SelectObject(&mypen4);
			for(int i2=0;i2<50;i2++)
			{
               
				

				CRect rect(rectp.left+40+*(towp+2*i2)-1,rectp.bottom-30-*(towp+2*i2+1)-1,rectp.left+40+*(towp+2*i2)+1,rectp.bottom-30-*(towp+2*i2+1)+1);
				
                pDC->Ellipse(rect);
			

			}


		}
         if(flag2)
		{
			pDC->SelectObject(&mypen3);
			for(int i1=0;i1<100;i1++)
			{
               
				

				CRect rect(rectp.left+40+*(onetp+2*i1)-1,rectp.bottom-30-*(onetp+2*i1+1)-1,rectp.left+40+*(onetp+2*i1)+1,rectp.bottom-30-*(onetp+2*i1+1)+1);
				
                pDC->Ellipse(rect);
			

			}
          pDC->SelectObject(&mypen4);
			for(int i2=0;i2<100;i2++)
			{
               
				

				CRect rect(rectp.left+40+*(towtp+2*i2)-1,rectp.bottom-30-*(towtp+2*i2+1)-1,rectp.left+40+*(towtp+2*i2)+1,rectp.bottom-30-*(towtp+2*i2+1)+1);
				
                pDC->Ellipse(rect);
			

			}


		}
		if(flagl)
		{
			CPen mlp1(PS_SOLID,0,RGB(255,255,255));
			CPen mlp2(PS_SOLID,0,RGB(0,0,255));
            CPen mlp3(PS_DASHDOT,1,RGB(18,101,138));
	
            pDC->SelectObject(&mlp3);
		    if(t>=100)
			{
				int yyyu;
				yyyu=0;
			}
			double tex,tey;
				if(w1==0)
			{
                 x1=0;tey=(-((double)w4))/w2;y1=int(tey*10+0.5);x2=350;y2=y1;
				pDC->MoveTo(rectp.left+40+x1, rectp.bottom-30-y1);
                pDC->LineTo(rectp.left+40+x2, rectp.bottom-30-y2);
					
				
			}
			else
			{
				if(w2==0)
				{
                 y1=0;tex=(-((double)w4))/w1; x1=int(tex*10+0.5);y2=350;x2=x1;
                  pDC->MoveTo(rectp.left+40+x1, rectp.bottom-30-y1);
                  pDC->LineTo(rectp.left+40+x2, rectp.bottom-30-y2);	
				}
				else
				{
					double tep;
					double te=((double)(-w1))/w2;
					if(te>0)
					{
					    if(w2>0)
						tep=w4;
						else
							tep=-w4;
					 if(tep<=0)
					 {
                       x1=0;tey=((double)(-w4))/w2;y1=(int)(10*(tey+0.05));
					 }
					 else
					 {
						 y1=0;tex=((double)(-w4))/w1;x1=(int)(10*(tex+0.05));
					 }
                     if(w2>0)
						tep=35*w1+35*w2+w4;
						else
							tep=-35*w1-35*w2-w4;
					 if(tep>=0)
					 {
						 x2=350;tey=((double)(-w4-35*w1))/w2;y2=(int)(10*(tey+0.05));
					 }
					 else
					 {
                         y2=350;tex=((double)(-w4-35*w2))/w1;x2=(int)(10*(tex+0.05));
					 }
					}
					else
					{
                      if(w2>0)
						tep=w2*35+w4;
						else
						tep=-w2*35-w4;
						if(tep>=0)
						{
							x1=0;tey=((double)(-w4))/w2;y1=(int)(10*(tey+0.05));
						}
						else
						{
                            y1=0;tex=((double)(-w4))/w1;x1=(int)(10*(tex+0.05));
						}
                        if(w2>0)
						tep=w1*35+w4;
						else
						tep=-w1*35-w4;
                        if(tep>=0)
					 {
						 y2=0;tex=((double)(-w4))/w1;x2=(int)(10*(tex+0.05));
						
					 }
					 else
					 {
                         x2=350;tey=((double)(-w4-35*w1))/w2;y2=(int)(10*(tey+0.05));
					 }
					}
                  pDC->MoveTo(rectp.left+40+x1, rectp.bottom-30-y1);
                  pDC->LineTo(rectp.left+40+x2, rectp.bottom-30-y2);
				}
			}

		}
pDC->SelectObject(&pOldPen);
pDC->SelectObject(&polidbrush);
return true;
}

void CMyDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CDialogg dlg;
	dlg.m=m;
	fa1=true;fa2=false;fa3=false;
    if(dlg.DoModal()==IDOK)
	{
		bool f=true;
         m_b5.down=false;
	    m_b5.Invalidate();
        m_b6.flagv=true;GetDlgItem(IDC_BUTTON5)->EnableWindow(false);
	    m_b7.flagv=true;GetDlgItem(IDC_BUTTON6)->EnableWindow(FALSE);
		if(f)
		{
		
			m_b2.down=false;
	    m_b2.Invalidate();
    GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE);
    w1=(double)atoi(dlg.m_w1);
    w2=(double)atoi(dlg.m_w2);
    w4=(double)atoi(dlg.m_w4);
	a=dlg.m_a;
	if(w2<=0)
	{
		w1=-w1;w2=-w2;w4=-w4;
	}
    flag1=true;flag2=false;
	flagl=true;
    CRect rectldj,rectl;   
	GetDlgItem(IDC_STATIC2)->GetWindowRect(rectldj);ScreenToClient(rectldj);
	InvalidateRect(rectldj,true);
    GetDlgItem(IDC_STATICz)->GetWindowRect(rectl);ScreenToClient(rectl);
	strcpy(m_z.mychar,"正在学习中");
    InvalidateRect(rectl,true);
	SetTimer(1,1000,NULL);
		}
	}
}

void CMyDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	KillTimer(1);
	if(t==0)fg=false;
	double wt1,wt2,wt4;
	BOOL ft=true;
if(fa1)
{
	if(m==0)
	{
      while(ft)
	  {//w
		  if(t>=100)
		  {
			  t++;
			  ft=false;
		  }
		  if(t<50) 
		  {
             double xt,yt,z;
			 xt=((double)(*(onep+2*t)))/10;
             yt=((double)(*(onep+2*t+1)))/10;
             /*if(w2<=0)
			 {
				 w1=-w1;
				 w2=-w2;w4=-w4;
			 }*/
			 z=xt*w1+yt*w2+w4;
			 if(z<=0)
			 {
				 fg=true;
				 ft=false;
				 BOOL ft1=true;
				 w1=w1+a*xt;
				 w2=w2+a*yt;
				 w4=w4+a;
				
				 
			 }
			 t++;
		  }
		  
		  else
		  {
			  if(t>=50&&t<100)
			  {
                   double xt,yt,z;
			        xt=((double)(*(towp+2*(t-50))))/10;
                     yt=((double)(*(towp+2*(t-50)+1)))/10;
                     /*if(w2<=0)
					 {
						 w1=-w1;
						 w2=-w2;
						 w4=-w4;
					 }*/
					 z=xt*w1+yt*w2+w4;
			         if(z>=0)
					 {
				       fg=true;
						 ft=false;
				       BOOL ft1=true;
				       w1=w1-a*xt;
				       w2=w2-a*yt;
				       w4=w4-a;
				       
					 }
			     t++;
			  }
			}//
		  }//w
	  }//i
	
	if(m==1)
	{
		  while(ft)
		  {//w
		    if(t>=100)
			{
			  t++;
			  ft=false;
			}
		     if(t<50) 
			 {
              double xt,yt,z;
			  xt=((double)(*(towp+2*t)))/10;
              yt=((double)(*(towp+2*t+1)))/10;
             
			  z=xt*w1+yt*w2+w4;
			  if(z>=0)
			  {
				 fg=true;
				 ft=false;
				 BOOL ft1=true;
				 w1=w1-a*xt;
				 w2=w2-a*yt;
				 w4=w4-a;
				
				 
			  }
			  t++;
			 }
		  
		     else
			 {
			    if(t>=50&&t<100)
				{
                   double xt,yt,z;
			        xt=((double)(*(onep+2*(t-50))))/10;
                     yt=((double)(*(onep+2*(t-50)+1)))/10;
                     
					 z=xt*w1+yt*w2+w4;
			         if(z<=0)
					 {
				       fg=true;
						 ft=false;

⌨️ 快捷键说明

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