📄 test1view.cpp
字号:
// test1View.cpp : implementation of the CTest1View class
//
#include "stdafx.h"
#include "test1.h"
#include "test1Doc.h"
#include "test1View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTest1View
double m_nPpdistance;
struct CXData CltCenter[]={50, 10,
234, 92,
150, 240 };
struct CXData PpCent[]={40,40,
70,110,
200,270};
struct CXData InitData[]=
{
30, 50, 34, 52, 24, 40, 34, 42, 26, 50,
42, 40, 46, 38, 40, 50, 30, 50, 38, 38,
100, 124, 110, 120, 114, 124, 98, 108, 106, 126,
98, 120, 116, 120, 110, 118, 104, 122, 110, 122,
202, 210, 212, 230, 210, 218, 244, 246, 210, 230,
220, 280, 244, 244, 204, 218, 238, 254, 258, 240
};
IMPLEMENT_DYNCREATE(CTest1View, CView)
BEGIN_MESSAGE_MAP(CTest1View, CView)
//{{AFX_MSG_MAP(CTest1View)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_CREATE()
ON_COMMAND(IDR_FCM, OnFcm)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest1View construction/destruction
CTest1View::CTest1View()
{
// TODO: add construction code here
}
CTest1View::~CTest1View()
{
}
BOOL CTest1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTest1View drawing
void CTest1View::OnDraw(CDC* pDC)
{
CTest1Doc* pDoc = GetDocument();
m_pTOrigin=0;
m_bDraw=FALSE;
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CTest1View diagnostics
#ifdef _DEBUG
void CTest1View::AssertValid() const
{
CView::AssertValid();
}
void CTest1View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTest1Doc* CTest1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTest1Doc)));
return (CTest1Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTest1View message handlers
void CTest1View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// MessageBox(_T("Help, Something went wrong."), NULL, MB_OK);
m_pTOrigin= m_pOld=point;
m_bDraw=TRUE;
CView::OnLButtonDown(nFlags, point);
}
void CTest1View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
/* HDC hdc;
hdc=::GetDC(m_hWnd);
MoveToEx(hdc,m_pTOrigin.x,m_pTOrigin.y,NULL);
LineTo(hdc,point.x,point.y);
::ReleaseDC(m_hWnd,hdc);*/
/* CDC *pDC=GetDC();
pDC->MoveTo(m_pTOrigin);
pDC->LineTo(point);
ReleaseDC(pDC); */
/* CClientDC dc(this);
dc.MoveTo(m_pTOrigin);
dc.LineTo(point); */
m_bDraw=FALSE;
CView::OnLButtonUp(nFlags, point);
}
void CTest1View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
/* CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(255,0,0));
CPen *pOldpen=dc.SelectObject(&pen);
if(m_bDraw==TRUE)
{
dc.MoveTo(m_pTOrigin);
// dc.LineTo(point);
dc.LineTo(m_pOld);
// dc.MoveTo(m_pTOrigin);
dc.MoveTo(m_pOld);
dc.LineTo(point);
m_pOld=point;
}
dc.SelectObject(pOldpen);
*/
CView::OnMouseMove(nFlags, point);
}
int CTest1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
// follow is take a insert key
/*
CClientDC dc(this);
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);
ShowCaret();
*/
return 0;
}
/// 模糊c均值....................................................
void CTest1View::OnFcm()
{
// TODO: Add your command handler code here
CDC* pDC=GetDC();
DispDate();
CreateClusterCenter(); ////creat init cluster center
Distance(InitData,CltCenter); ///caculate the distance between initdata and cluster center;
m_nDataNumber=sizeof(InitData)/sizeof(CXData);
m_nCenterNumber=sizeof(CltCenter)/sizeof(CXData);
PpDistance();
while(m_nPpdistance>0.00001)
{
Distance(InitData,CltCenter);
for(int i=0;i<3;i++)
{
PpCent[i].x=CltCenter[i].x;
PpCent[i].y=CltCenter[i].y;
}
for(i=0;i<m_nCenterNumber;i++)
{
for(int k=0;k<m_nDataNumber;k++)
{
if(m_nDistance[i][k]>0)
{
double sum=0.0000000001;
for(int j=0;j<m_nCenterNumber;j++)
{
sum=sum+(m_nDistance[i][k]/(m_nDistance[j][k]+0.0000000001))*
(m_nDistance[i][k]/(m_nDistance[j][k]+0.0000000001));
}
uIndex[i][k]=1/sum;
}
else
if(m_nDistance[i][k]==0)
uIndex[i][k]=1; //%%%%%%%% need change
}
}
for(i=0;i<m_nCenterNumber;i++)
{
double sum_u_X[2]={0.00000000001,0.00000000001};
double sum_u=0.000000000001;
for(int k=0;k<m_nDataNumber;k++)
{
sum_u_X[0]=sum_u_X[0]+(uIndex[i][k])*(uIndex[i][k])*InitData[k].x;
sum_u_X[1]=sum_u_X[1]+(uIndex[i][k])*(uIndex[i][k])*InitData[k].y;
sum_u=sum_u+(uIndex[i][k])*(uIndex[i][k]);
}
CltCenter[i].x=sum_u_X[0]/sum_u;
CltCenter[i].y=sum_u_X[1]/sum_u;
// DispClusterCenter();
}
DispPpCentoCenter(pDC,m_nCenterNumber);
PpDistance();
Distance(InitData,CltCenter);
}
DispClusterCenter(pDC,m_nCenterNumber);
ReleaseDC(pDC);
}
//DEL void CTest1View::CreateClusterCenter()
//DEL {
//DEL
//DEL }
//DEL void CTest1View::CreateClusterCenter(CDC *pDC, int ClCNum)
//DEL {
//DEL for(int n=0;n<ClCNum;n++)
//DEL {
//DEL for(int k=0;k<2;k++)
//DEL DrawLump(pDC,CltCenter[n].x,CltCenter[n].y,CltCenter[n].x+3,CltCenter[n].y+3,255,0,0);
//DEL };
//DEL }
void CTest1View::Distance(CXData *cID, CXData *cClC)
{
m_nDataNumber=30;//sizeof(cID);//sizeof(CXData);
m_nCenterNumber=3;//sizeof(cClC);//sizeof(CXData);
for(int i=0;i<m_nCenterNumber;i++)
{
for(int j=0;j<m_nDataNumber;j++)
{
m_nDistance[i][j]=sqrt((cID[j].x-cClC[i].x)*(cID[j].x-cClC[i].x)+
(cID[j].y-cClC[i].y)*(cID[j].y-cClC[i].y));
};
}
}
void CTest1View::PpDistance()
{
m_nCenterNumber=sizeof(CltCenter)/sizeof(CXData);
int Ppdist_temp[3];
int max_temp;
for(int i=0;i<m_nCenterNumber;i++)
Ppdist_temp[i]=(CltCenter[i].x-PpCent[i].x)*(CltCenter[i].x-PpCent[i].x)+
(CltCenter[i].y-PpCent[i].y)*(CltCenter[i].y-PpCent[i].y);
if(Ppdist_temp[0]>Ppdist_temp[1])
{
if(Ppdist_temp[0]>Ppdist_temp[2])
max_temp=Ppdist_temp[0];
else
max_temp=Ppdist_temp[2];
}
else if(Ppdist_temp[1]>Ppdist_temp[2])
max_temp=Ppdist_temp[1];
else
max_temp=Ppdist_temp[2];
m_nPpdistance=sqrt(max_temp);
}
void CTest1View::DispPpCentoCenter(CDC *pDC, int CLCNum)
{
CPen pen;
pen.CreatePen(PS_SOLID,1,RGB(0,255,255));
pDC->SelectObject(pen);
for(int n=0;n<CLCNum;n++)
{
pDC->MoveTo(PpCent[n].x,PpCent[n].y);
pDC->LineTo(CltCenter[n].x,CltCenter[n].y);
}
DeleteObject(pen);
}
void CTest1View::DrawLump(CDC *pDC, int x1, int y1, int x2, int y2, int red, int green, int blue)
{
int i,j;
for(i=x1;i<x2;i++)
for(j=y1;j<y2;j++)
pDC->SetPixel(i,j,RGB(red,green,blue)); //给指定的范围着色
}
void CTest1View::CreateClusterCenter()
{
CDC *pDC=GetDC();
for(int n=0;n<3;n++)
{
for(int k=0;k<2;k++)
DrawLump(pDC,CltCenter[n].x,CltCenter[n].y,CltCenter[n].x+3,CltCenter[n].y+3,0,255,0);
};
ReleaseDC(pDC);
}
void CTest1View::DispClusterCenter(CDC *pDC, int ClCNum)
{
for(int n=0;n<ClCNum;n++)
{
for(int k=0;k<2;k++)
DrawLump(pDC,CltCenter[n].x,CltCenter[n].y,CltCenter[n].x+3,CltCenter[n].y+3,255,0,0);
};
}
void CTest1View::DispDate()
{
CDC *pDC=GetDC();//获得窗口设备环境
m_nDataNumber=30;
// TODO: Add your command handler code here
for(int j=0;j<m_nDataNumber;j++)
{
for(int k=0;k<2;k++)
DrawLump(pDC,InitData[j].x,InitData[j].y,InitData[j].x+3,InitData[j].y+3,0,0,255);
};
ReleaseDC(pDC);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -