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

📄 painterfacade.cpp

📁 神经网络中的无监督学习中的SOM学习算法
💻 CPP
字号:
#include "stdafx.h"
#include "math.h"
#include "PainterFacade.h"

/* draw a line for the linear formula ax + by + c = 0 */
int PainterFacade::DrawLine(CDC* pDC, double a, double b, double c)
{
    int cx, cy;

    cx = m_logicalRect.left + (m_logicalRect.right - m_logicalRect.left) / 2;
    cy = m_logicalRect.top + (m_logicalRect.bottom - m_logicalRect.top) / 2;

    if (fabs(b - 0.0) < 0.0001)
    {
        if (fabs(a - 0.0) < 0.0001)
            return 1;
        
        CPointf pTmp1(-c / a, 0);
        CPoint pTmp2;
        MPtoLP(&pTmp1, &pTmp2);
        pDC->MoveTo(pTmp2.x, m_logicalRect.top);
        pDC->LineTo(pTmp2.x, m_logicalRect.bottom);
    }
    else
    {
        CPointf pf1, pf2;
        CPoint pc1, pc2;
        pf1.x = -(cx - m_logicalRect.left) / m_dbBaseUnit;
        pf1.y = -(a * pf1.x + c) / b;
        pf2.x = -pf1.x;
        pf2.y = -(a * pf2.x + c) / b;

        MPtoLP(&pf1, &pc1);
        MPtoLP(&pf2, &pc2);
        
        /* adjust the beginning and ending of the line to be inner the rectangle of view */        
        if (!(pc1.y <= m_logicalRect.top && pc2.y <= m_logicalRect.top) 
            && !(pc1.y >= m_logicalRect.bottom && pc2.y >= m_logicalRect.bottom))
        {
            if (pc2.y != pc1.y)
            {
                int tmpY;
                tmpY = pc1.y < m_logicalRect.top ? m_logicalRect.top : 
                       pc1.y > m_logicalRect.bottom ? m_logicalRect.bottom : pc1.y;
                pc1.x = (pc2.x - pc1.x) * (tmpY - pc1.y) / (pc2.y - pc1.y) + pc1.x;
                pc1.y = tmpY;

                tmpY = pc2.y < m_logicalRect.top ? m_logicalRect.top : 
                       pc2.y > m_logicalRect.bottom ? m_logicalRect.bottom : pc2.y;
                pc2.x = (pc2.x - pc1.x) * (tmpY - pc1.y) / (pc2.y - pc1.y) + pc1.x;
                pc2.y = tmpY;
            }
            pDC->MoveTo(pc1);
            pDC->LineTo(pc2);
        }
    }

    return 0;
}

/* draw the math coordinate */
int PainterFacade::DrawCoordinate(CDC *pDC)
{
    int cx, cy;
    int offset;

    pDC->Rectangle(&m_logicalRect);

    cx = m_logicalRect.left + (m_logicalRect.right - m_logicalRect.left) / 2;
    cy = m_logicalRect.top + (m_logicalRect.bottom - m_logicalRect.top) / 2;
    
    /* x-coordinate */
    pDC->MoveTo(m_logicalRect.left, cy);
    pDC->LineTo(m_logicalRect.right, cy);
    offset = (int)m_dbBaseUnit;
    while (offset + cx < m_logicalRect.right)
    {
        pDC->MoveTo(cx + offset, cy);
        pDC->LineTo(cx + offset, cy - 5);

        pDC->MoveTo(cx - offset, cy);
        pDC->LineTo(cx - offset, cy - 5);
        offset += (int)m_dbBaseUnit;
    }

    /* y-coordinate */
    pDC->MoveTo(cx, m_logicalRect.top);
    pDC->LineTo(cx, m_logicalRect.bottom);
    offset = (int)m_dbBaseUnit;
    while (offset + cy < m_logicalRect.bottom)
    {
        pDC->MoveTo(cx, cy + offset);
        pDC->LineTo(cx + 5, cy + offset);

        pDC->MoveTo(cx, cy - offset);
        pDC->LineTo(cx + 5, cy - offset);
        offset += (int)m_dbBaseUnit;
    }

    return 0;
}

/* map the point of math coordinates to logical ones */
int PainterFacade::MPtoLP(const CPointf *pSrcPf, CPoint *pDstP)
{
    double ax, bx, ay, by;

    int cx = m_logicalRect.left + (m_logicalRect.right - m_logicalRect.left) / 2;
    int cy = m_logicalRect.top + (m_logicalRect.bottom - m_logicalRect.top) / 2;

	ax = 1.0 / m_dbBaseUnit;
    bx = -cx / m_dbBaseUnit;
    ay = -1.0 / m_dbBaseUnit;
    by = cy / m_dbBaseUnit;

    pDstP->x = (long)((pSrcPf->x - bx) / ax);
    pDstP->y = (long)((pSrcPf->y - by) / ay);

    return 0;
}

/* the inverse function of MPtoLP */
int PainterFacade::LPtoMP(const CPoint *pSrcP, CPointf *pDstPf)
{
    double ax, bx, ay, by;

    int cx = m_logicalRect.left + (m_logicalRect.right - m_logicalRect.left) / 2;
    int cy = m_logicalRect.top + (m_logicalRect.bottom - m_logicalRect.top) / 2;

	ax = 1 / m_dbBaseUnit;
    bx = -cx / m_dbBaseUnit;
    ay = -1 / m_dbBaseUnit;
    by = cy / m_dbBaseUnit;

    pDstPf->x = pSrcP->x * ax + bx;
    pDstPf->y = pSrcP->y * ay + by;
    
    return 0;
}

/* draw point use method of draw ellipse */
int PainterFacade::DrawPoint(CDC *pDC, const CPointf *pf, int nPointUnit)
{
    CPoint pL;
    MPtoLP(pf, &pL);
    if (m_logicalRect.PtInRect(pL))
    {
        pDC->Ellipse(pL.x - nPointUnit, pL.y - nPointUnit, 
                      pL.x + nPointUnit, pL.y + nPointUnit);
        return 0;
    }
    
    return 1;
}

/* draw point use method of draw line */
int PainterFacade::DrawPoint(CDC *pDC, const CPointf *pf)
{
    CPoint pL;
    MPtoLP(pf, &pL);
    if (m_logicalRect.PtInRect(pL))
    {
        pDC->MoveTo(pL);
        pDC->LineTo(pL);
        return 0;
    }
    
    return 1;    
}

int PainterFacade::DrawEllipse(CDC* pDC, const CPointf *pf, int x, int y)
{
    CPoint pL;
    MPtoLP(pf, &pL);
    if (m_logicalRect.PtInRect(pL))
    {
        pDC->Arc(pL.x - x, pL.y - y, pL.x + x, pL.y + y,
                 pL.x - x, pL.y - y, pL.x - x, pL.y - y);
        return 0;
    }
    
    return 1;    
    
}

⌨️ 快捷键说明

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