📄 samview.cpp
字号:
#include "header.h"
#include "matlvq.h"
void DrawBlock(CPaintDC *dc, int xcenter, int ycenter, COLORREF color);
#define SPACE_TOP 20;
COLORREF COLOR_TABLE[] =
{
RGB(255, 0, 0),
RGB(0, 255, 0),
RGB(0, 0, 255),
RGB(127, 0, 0),
RGB(0, 127, 0),
RGB(0, 0, 127),
RGB(255, 127, 0),
RGB(255, 0, 127),
RGB(127, 255, 0),
RGB(127, 0, 255),
RGB(0, 255, 127),
RGB(0, 127, 255),
RGB(127, 127, 0),
RGB(127, 0, 127),
RGB(0, 127, 127),
RGB(255, 255, 0),
RGB(255, 0, 255),
RGB(0, 255, 255),
RGB(127, 127, 127)
};
BEGIN_MESSAGE_MAP(CViewSammon, CFrameWnd)
//{{AFX_MSG_MAP(CViewSammon)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_CREATE()
ON_WM_HSCROLL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CViewSammon::PreCreateWindow(CREATESTRUCT& cs)
{
/*cs.style |= WS_CHILD;*/
if (CFrameWnd::PreCreateWindow(cs) == 0)
return 0;
cs.style |= WS_HSCROLL;
return 1;
}
CViewSammon::CViewSammon()
{
pSammon = NULL;
}
CViewSammon::CViewSammon(CSammon *psam)
{
pSammon = psam;
int n_max_pict = pSammon->GetSize();
CMatrix *pmat;
PSAMMON_PICT pict;
int n_mat;
float x, y;
long l_col_max, l_col;
char name[MAX_NAME];
n_pict = 0;
pClass = new CStringList();
/*compute the dimension of the map*/
for (n_mat = 0; n_mat < n_max_pict; n_mat++)
{
pict = pSammon->GetPict(n_mat);
pmat = pict->pmat;
if (pmat == NULL)
break;
l_col_max = pmat->GetCol();
pmat->Lock();
if (n_mat == 0)
{
xmax = xmin = pmat->GetAt(1, 1);
ymax = ymin = pmat->GetAt(2, 1);
}
for (l_col = 1; l_col <= l_col_max; l_col++)
{
x = pmat->GetAt(1, l_col);
y = pmat->GetAt(2, l_col);
if (xmax < x)
xmax = x;
if (xmin > x)
xmin = x;
if (ymax < y)
ymax = y;
if (ymin > y)
ymin = y;
pmat->GetColName(l_col, name, sizeof(name));
if (pClass->Find(name) == NULL)
pClass->AddTail(name);
}
pmat->Unlock();
}
/*if (xmin > 0.0)
xmin = 0.1;
if (ymin > 0.0)
ymin = 0.1;*/
xmax *= 1.10;
xmin *= 1.10;
ymax *= 1.10;
ymin *= 1.10;
}
CViewSammon::~CViewSammon()
{}
void CViewSammon::OnPaint()
{
CPaintDC dc(this); // device context for painting
RECT area;
CMatrix *pmat;
PSAMMON_PICT pict;
float xscale, yscale, xshift, yshift;
float x1, x2, y1, y2;
long lcol, lmaxcol;
CPen pen(PS_SOLID, 0, RGB(0, 0, 0));
CPen *oldpen;
int color_index;
char name[MAX_NAME];
CString str;
POSITION pos;
char buf[150];
if (pSammon == NULL)
return;
GetClientRect(&area);
area.top += SPACE_TOP;
xscale = (float)(area.right - area.left)/(xmax - xmin);
yscale = (float)(area.bottom - area.top)/(ymax - ymin);
xshift = - xmin;
yshift = - ymin;
/*Set the scroll bar*/
SetScrollPos(SB_HORZ, n_pict);
/*Draw the text : name etc.*/
pict = pSammon->GetPict(n_pict);
sprintf(buf, "learning it. num : %.0f, %.0f total sammon views of the %s, mapping error = %f", (double)(pict->lnum),
(double)(pict->total), pict->name, pict->error);
dc.TextOut(10, 3, buf, strlen(buf));
/*Draw the axes*/
oldpen = dc.SelectObject(&pen);
/*dc.MoveTo(yshift*xscale, area.top);
dc.LineTo(yshift*xscale, area.bottom);
dc.MoveTo(area.left, xshift*yscale);
dc.LineTo(area.right, xshift*yscale);*/
x1 = 0.15*(float)(area.right - area.left);
x2 = 0.9*(float)(area.right - area.left);
y1 = 0.1*(float)(area.bottom - area.top) + area.top;
y2 = 0.9*(float)(area.bottom - area.top) + area.top;
dc.MoveTo(area.left, area.top);
dc.LineTo(area.right, area.top);
dc.MoveTo(x1, area.top);
dc.LineTo(x1, area.bottom);
dc.MoveTo(x2, area.top);
dc.LineTo(x2, area.bottom);
dc.MoveTo(area.left, y1);
dc.LineTo(area.right, y1);
dc.MoveTo(area.left, y2);
dc.LineTo(area.right, y2);
sprintf(buf, "%f", (float)x1/xscale-xshift);
dc.TextOut(x1+3, y2+3, buf, strlen(buf));
sprintf(buf, "%f", (float)x2/xscale-xshift);
dc.TextOut(x2+3, y2+3, buf, strlen(buf));
sprintf(buf, "%f", (float)y2/yscale-yshift);
dc.TextOut(3, y1+3, buf, strlen(buf));
sprintf(buf, "%f", (float)y1/yscale-yshift);
dc.TextOut(3, y2+3, buf, strlen(buf));
dc.SelectObject(oldpen);
pmat = pict->pmat;
lmaxcol = pmat->GetCol();
pmat->Lock();
for (lcol = 1; lcol <= lmaxcol; lcol++)
{
pmat->GetColName(lcol, name, sizeof(name));
color_index = 0;
str = pClass->GetHead();
pos = pClass->GetHeadPosition();
while (str != name)
{
color_index++;
str = pClass->GetNext(pos);
}
if (color_index >= sizeof(COLOR_TABLE)/sizeof(COLORREF))
color_index = color_index % (sizeof(COLOR_TABLE)/sizeof(COLORREF)-1);
DrawBlock(&dc, (xshift+pmat->GetAt(1, lcol))*xscale,
area.bottom-(area.top+(yshift+pmat->GetAt(2, lcol))*yscale),
COLOR_TABLE[color_index]);
}
pmat->Unlock();
}
void CViewSammon::Play()
{
n_pict = 0;
SetTimer(1, 300, NULL);
}
void CViewSammon::OnTimer(UINT nIDEvent)
{
n_pict++;
if (n_pict >= pSammon->GetSize())
{
KillTimer(1);
n_pict = pSammon->GetSize()-1;
return;
}
RECT rect;
GetClientRect(&rect);
InvalidateRect(&rect);
UpdateWindow();
}
void DrawBlock(CPaintDC *dc, int xcenter, int ycenter, COLORREF color)
{
int i, j;
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
dc->SetPixel(xcenter+i, ycenter+j, color);
}
}
}
void CViewSammon::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
KillTimer(1);
switch (nSBCode)
{
case SB_LINERIGHT:
n_pict++;
break;
case SB_LINELEFT:
n_pict--;
break;
case SB_PAGERIGHT:
n_pict++;
break;
case SB_PAGEUP:
n_pict--;
break;
case SB_THUMBPOSITION:
case SB_THUMBTRACK:
n_pict = nPos;
break;
}
if (n_pict < 0)
n_pict = 0;
if (n_pict >= pSammon->GetSize())
n_pict = pSammon->GetSize()-1;
Invalidate();
}
int CViewSammon::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
SetScrollRange(SB_HORZ, 0, pSammon->GetSize()-1);
/* Initialise the horizontal scroll range*/
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -