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

📄 k均值dlg.cpp

📁 本程序通过k均值算法对两类进行分类。通过任意选择初始点
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		BITMAP bm1;
        mybitmap.GetObject(sizeof(BITMAP),&bm1);
		memdc.CreateCompatibleDC(pDC);
        CBitmap* oldbitmap=memdc.SelectObject(&mybitmap);
		int wide=rect.Width()/4;
		int mheight=rect.Height()/4;
	for (int j=0;j<4;j++)
	{
		for(int i=0;i<4;i++)
	{
      pDC->StretchBlt(rect.left,rect.top,wide,mheight,&memdc,0,0,bm1.bmWidth,bm1.bmHeight,SRCCOPY);
        rect.left+= wide;	
	}
	     rect.left-=wide*4;
		 rect.top+=mheight;
	}
CRect rects;GetClientRect(rect);
	GetDlgItem(IDC_STATIC1)->GetClientRect(rects);
	 int i;   
	for( i=0;i<18;i++)
		{
          rects.top=rect.top+i*3;
		  rects.bottom=rects.top+3;
          pDC->FillSolidRect(rects,RGB(188+i,212+i,249));
		}
CRect rectp,rectp1; 
	CPen *pOldPen;
	GetDlgItem(IDC_STATIC2)->GetWindowRect(rectp);ScreenToClient(rectp);
	pDC->FillSolidRect(rectp,RGB(255,255,255));
 CPen mypen1(PS_SOLID,1,RGB(0,0,0));
   pOldPen=pDC->SelectObject(&mypen1);
   pDC->MoveTo(rectp.left+20, rectp.bottom-20);
   pDC->LineTo(rectp.left+20, rectp.bottom-20-450);
  pDC->LineTo(rectp.left+15, rectp.bottom-20-435);
  pDC->MoveTo(rectp.left+20, rectp.bottom-20-450);
   pDC->LineTo(rectp.left+25, rectp.bottom-20-435);
   pDC->TextOut(rectp.left+20,rectp.bottom-20-470,"Y");
pDC->MoveTo(rectp.left+20, rectp.bottom-20);
pDC->LineTo(rectp.left+20+450, rectp.bottom-20);
pDC->LineTo(rectp.left+20+435, rectp.bottom-25);
pDC->MoveTo(rectp.left+20+450, rectp.bottom-20);
pDC->LineTo(rectp.left+20+435, rectp.bottom-15);
pDC->TextOut(rectp.left+20+465,rectp.bottom-30,"X");
CPen mypen2(PS_DASH,1,RGB(0,0,0));
pDC->SelectObject(&mypen2);
for(i=0;i<10;i++)
{
pDC->MoveTo(rectp.left+20, rectp.bottom-20-40*(i+1));
pDC->LineTo(rectp.left+20+400, rectp.bottom-20-40*(i+1));
CString temp;
temp.Format("%d",i+1);
pDC->TextOut(rectp.left+4,rectp.bottom-20-40*(i+1)-5,temp);
}
for(i=0;i<10;i++)
{
  pDC->MoveTo(rectp.left+20+40*(i+1), rectp.bottom-20);
  pDC->LineTo(rectp.left+20+40*(i+1),rectp.bottom-20-400);
CString temp;
temp.Format("%d",i+1);
pDC->TextOut(rectp.left+20+40*(i+1)-4,rectp.bottom-15,temp);
}
//CBrush* pOldbrush;
CBrush mypen3(RGB(255,0,0));CBrush mypen6(RGB(187,255,21));
//pOldbrush=pDC->SelectObject(&mypen3);
for(i=0;i<18;i++)
{
CRgn rect;
rect.CreateEllipticRgn(rectp.left+20+int(*(point+2*i)/0.025)-4,rectp.bottom-20-int(*(point+2*i+1)/0.025)-4,rectp.left+20+int(*(point+2*i)/0.025)+4,rectp.bottom-20-int(*(point+2*i+1)/0.025)+4);
if(i<8)
pDC->FillRgn(&rect,&mypen3);
else
pDC->FillRgn(&rect,&mypen6);
}
CBrush mypen4(RGB(0,255,0));CBrush mypen5(RGB(0,0,255));
if(dot1)
{
CRgn rect;
rect.CreateEllipticRgn(rectp.left+20+int((pointc1[0])/0.025)-4,rectp.bottom-20-int(pointc1[1]/0.025)-4,rectp.left+20+int(pointc1[0]/0.025)+4,rectp.bottom-20-int(pointc1[1]/0.025)+4);
pDC->FillRgn(&rect,&mypen4);dot=2;
pDC->TextOut(rectp.left+20+int((pointc1[0])/0.025)-3,rectp.bottom-20-int(pointc1[1]/0.025)-20,"0");
}
if(dot2)
{
CRgn rect;
rect.CreateEllipticRgn(rectp.left+20+int((pointc2[0])/0.025)-4,rectp.bottom-20-int(pointc2[1]/0.025)-4,rectp.left+20+int(pointc2[0]/0.025)+4,rectp.bottom-20-int(pointc2[1]/0.025)+4);
pDC->FillRgn(&rect,&mypen5);dot=false;dot2=false;dot1=false;pDC->TextOut(rectp.left+20+int((pointc2[0])/0.025)-3,rectp.bottom-20-int(pointc2[1]/0.025)-20,"0");

}
if(flag)
{
   CRgn rect;
//rect.CreateEllipticRgn(rectp.left+20+int((pointc1.x)/0.025)-4,rectp.bottom-20-int(pointc1.y/0.025)-4,rectp.left+20+int(pointc1.x/0.025)+4,rectp.bottom-20-int(pointc1.y/0.025)+4);
//pDC->FillRgn(&rect,&mypen4);  
// rect.CreateEllipticRgn(rectp.left+20+int((pointc1.x)/0.025)-4,rectp.bottom-20-int(pointc1.y/0.025)-4,rectp.left+20+int(pointc1.x/0.025)+4,rectp.bottom-20-int(pointc1.y/0.025)+4);
//pDC->FillRgn(&rect,&mypen4); 
}
	return true;
}

void CKDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(dot)
	{
	CRect rectp; 
	
	GetDlgItem(IDC_STATIC2)->GetWindowRect(rectp);ScreenToClient(rectp);
	CDC* dc=GetWindowDC();
	if(!dot1)
	{
	  dc->DPtoLP(&point);dot1=true;
	  pointc1[0]=((point.x-rectp.left-20)*0.025);
      pointc1[1]=((rectp.bottom-20-point.y)*0.025);
	  CString temp,temp1;
	  temp="x1=";temp1.Format("%3.4f",pointc1[0]);temp=temp+temp1;
	  strcpy(m_t2.mychar,temp);
        temp="y1=";temp1.Format("%3.4f",pointc1[1]);temp=temp+temp1;
	  strcpy(m_t3.mychar,temp);
      InvalidateRect(rectp,true);
GetDlgItem(IDC_STATIC3)->GetWindowRect(rectp);ScreenToClient(rectp);InvalidateRect(rectp,true);
	}
	else
	{
		
		dot2=true;
		dc->DPtoLP(&point);
	  pointc2[0]=((point.x-rectp.left-20)*0.025);
      pointc2[1]=((rectp.bottom-20-point.y)*0.025);
	  m_b1.down=false;
	  m_b1.Invalidate();
      CString temp,temp1;
	  temp="x2=";temp1.Format("%3.4f",pointc2[0]);temp=temp+temp1;
	  strcpy(m_t4.mychar,temp);
        temp="y2=";temp1.Format("%3.4f",pointc2[1]);temp=temp+temp1;
	  strcpy(m_t5.mychar,temp);
      InvalidateRect(rectp,true);
GetDlgItem(IDC_STATIC3)->GetWindowRect(rectp);ScreenToClient(rectp);InvalidateRect(rectp,true);
	}
	}
	CDialog::OnLButtonDown(nFlags, point);
}

void CKDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	dot=true;
}

void CKDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	z=0;
	SetTimer(1,600,NULL);
	/*	dot1=true;dot2=true;
    CRect rectp; 
	GetDlgItem(IDC_STATIC2)->GetWindowRect(rectp);ScreenToClient(rectp);
	CString temp,temp1;
	  temp="x1=";temp1.Format("%3.4f",pointc1[0]);temp=temp+temp1;
	  strcpy(m_t2.mychar,temp);
        temp="y1=";temp1.Format("%3.4f",pointc1[1]);temp=temp+temp1;
	  strcpy(m_t3.mychar,temp);
      InvalidateRect(rectp,true);
     temp="x2=";temp1.Format("%3.4f",pointc2[0]);temp=temp+temp1;
	  strcpy(m_t4.mychar,temp);
        temp="y2=";temp1.Format("%3.4f",pointc2[1]);temp=temp+temp1;
	  strcpy(m_t5.mychar,temp);
GetDlgItem(IDC_STATIC3)->GetWindowRect(rectp);ScreenToClient(rectp);InvalidateRect(rectp,true);*/
}

void CKDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	KillTimer(1);
	double x1=0,x2=0,y1=0,y2=0,c1=0,c2=0;

	for(int i=0;i<18;i++)
	{
      double w1,w2;
	  w1=(*(point+2*i)-pointc1[0])*(*(point+2*i)-pointc1[0])+(*(point+2*i+1)-pointc1[1])*(*(point+2*i+1)-pointc1[1]);
	   w2=(*(point+2*i)-pointc2[0])*(*(point+2*i)-pointc2[0])+(*(point+2*i+1)-pointc2[1])*(*(point+2*i+1)-pointc2[1]);
	   if(w1>w2)
	   {
		   x2=x2+*(point+2*i);y2=y2+*(point+2*i+1);c2++;
	   }
	   else
	   {
            x1=x1+*(point+2*i);y1=y1+*(point+2*i+1);c1++;
	   }
	}
    double e1,e2;
	BOOL fag1=false,fag2=false;
	e1=(x1/c1-pointc1[0])*(x1/c1-pointc1[0])+(y1/c1-pointc1[1])*(y1/c1-pointc1[1]);
	if(e1>=0.0001)
	{
      pointc1[0]=x1/c1;pointc1[1]=y1/c1;fag1=true;
	}
	e2=(x2/c2-pointc2[0])*(x2/c2-pointc2[0])+(y2/c2-pointc2[1])*(y2/c2-pointc2[1]);
    
	if(e2>=0.0001)
	{
		pointc2[0]=x2/c2; pointc2[1]=y2/c2;fag2=true;
	}
	if(!fag1&&!fag2)
	{
        dot1=true;dot2=true;
    CRect rectp; 
	//GetDlgItem(IDC_STATIC2)->GetWindowRect(rectp);ScreenToClient(rectp);
	CString temp,temp1;
	  temp="x1=";temp1.Format("%3.4f",pointc1[0]);temp=temp+temp1;
	  strcpy(m_t2.mychar,temp);
        temp="y1=";temp1.Format("%3.4f",pointc1[1]);temp=temp+temp1;
	  strcpy(m_t3.mychar,temp);
      //InvalidateRect(rectp,true);
     temp="x2=";temp1.Format("%3.4f",pointc2[0]);temp=temp+temp1;
	  strcpy(m_t4.mychar,temp);
        temp="y2=";temp1.Format("%3.4f",pointc2[1]);temp=temp+temp1;
	  strcpy(m_t5.mychar,temp);m_b2.down=false;m_b2.Invalidate();
GetDlgItem(IDC_STATIC3)->GetWindowRect(rectp);ScreenToClient(rectp);InvalidateRect(rectp,true);
	}
	else
	{
//	dot1=true;dot2=true;
    CBrush mypen4(RGB(0,255,0));CBrush mypen5(RGB(0,0,255));
		CRect rectp; 
	GetDlgItem(IDC_STATIC2)->GetWindowRect(rectp);ScreenToClient(rectp);
	CDC* pdc=GetDC( );
	CRgn rect;
	z++;
	CString tt;
tt.Format("%d",z);
rect.CreateEllipticRgn(rectp.left+20+int((pointc1[0])/0.025)-4,rectp.bottom-20-int(pointc1[1]/0.025)-4,rectp.left+20+int(pointc1[0]/0.025)+4,rectp.bottom-20-int(pointc1[1]/0.025)+4);
pdc->FillRgn(&rect,&mypen4);
pdc->TextOut(rectp.left+20+int((pointc1[0])/0.025)-3,rectp.bottom-20-int(pointc1[1]/0.025)-20,tt);
CRgn rect1;
rect1.CreateEllipticRgn(rectp.left+20+int((pointc2[0])/0.025)-4,rectp.bottom-20-int(pointc2[1]/0.025)-4,rectp.left+20+int(pointc2[0]/0.025)+4,rectp.bottom-20-int(pointc2[1]/0.025)+4);
pdc->FillRgn(&rect1,&mypen5);
pdc->TextOut(rectp.left+20+int((pointc2[0])/0.025)-3,rectp.bottom-20-int(pointc2[1]/0.025)-20,tt);	
CString temp,temp1;
	  temp="x1=";temp1.Format("%3.4f",pointc1[0]);temp=temp+temp1;
	  strcpy(m_t2.mychar,temp);
        temp="y1=";temp1.Format("%3.4f",pointc1[1]);temp=temp+temp1;
	  strcpy(m_t3.mychar,temp);
      //InvalidateRect(rectp,true);
     temp="x2=";temp1.Format("%3.4f",pointc2[0]);temp=temp+temp1;
	  strcpy(m_t4.mychar,temp);
        temp="y2=";temp1.Format("%3.4f",pointc2[1]);temp=temp+temp1;
	  strcpy(m_t5.mychar,temp);
GetDlgItem(IDC_STATIC3)->GetWindowRect(rectp);ScreenToClient(rectp);InvalidateRect(rectp,true);
		
SetTimer(1,600,NULL);
	}
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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