MFC-Simulator/TestSimulator/BlockAnd.cpp

226 lines
7.0 KiB
C++
Raw Normal View History

2023-02-03 03:07:52 +01:00
#include "stdafx.h"
#include "BlockAnd.h"
#include "math.h"
#define PIN_RADIUS 8 //Pin<69>b<EFBFBD>|
#define GOLD RGB (255, 215, 0) //Pin<69>}<7D>C<EFBFBD><43>
#define BLACK RGB (0, 0, 0) //<2F>s<EFBFBD>u<EFBFBD>C<EFBFBD><43>
#define NO_VALUE -10 //<2F><><EFBFBD>s<EFBFBD>b<EFBFBD><62><EFBFBD>J<EFBFBD>T<EFBFBD><54>
#define NO_OPER_FLAG -11 //<2F><><EFBFBD>s<EFBFBD>b<EFBFBD>B<EFBFBD>
#define COMPUTE_ERROR -13 //<2F><><EFBFBD><EFBFBD><EFBFBD>\<5C><><EFBFBD>B<EFBFBD><42>
#define NO_BLOCK_HEAD -14 //<2F><><EFBFBD>s<EFBFBD>b<EFBFBD>s<EFBFBD><73>Block
#define TOLERANCE 0.001 //<2F>e<EFBFBD>Ի~<7E>t<EFBFBD><74>
#define DIGITAL_VALUE 4 //<2F>Ʀ<EFBFBD><C6A6>T<EFBFBD><54>flag
#define ANALOG_VALUE 5 //<2F><><EFBFBD><EFBFBD><EFBFBD>T<EFBFBD><54>flag
#define BLK_AND 3 //Block<63><6B><EFBFBD><EFBFBD><EFBFBD>s<EFBFBD><73>
CBlockAnd::CBlockAnd ()
{
m_rcPinIn1 = CRect ();
m_rcPinIn2 = CRect ();
m_rcPinOut = CRect ();
m_pBlkHead1 = NULL;
m_pBlkHead2 = NULL;
m_iBlkHead1Num = NO_BLOCK_HEAD;
m_iBlkHead2Num = NO_BLOCK_HEAD;
}
CBlockAnd::~CBlockAnd ()
{
}
void CBlockAnd::SetBlkRect (CPoint ptCenter, int iWidth, int iHeight)
{
CBlockBasis::SetBlkRect (ptCenter, iWidth, iHeight);
m_rcPinIn1.left = m_rcBlk.CenterPoint ().x - int (m_rcBlk.Width () / 4) - PIN_RADIUS;
m_rcPinIn1.top = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinIn1.right = m_rcBlk.CenterPoint ().x - int (m_rcBlk.Width () / 4) + PIN_RADIUS;
m_rcPinIn1.bottom = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) + PIN_RADIUS;
m_rcPinIn2.left = m_rcBlk.CenterPoint ().x + int (m_rcBlk.Width () / 4) - PIN_RADIUS;
m_rcPinIn2.top = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinIn2.right = m_rcBlk.CenterPoint ().x + int (m_rcBlk.Width () / 4) + PIN_RADIUS;
m_rcPinIn2.bottom = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) + PIN_RADIUS;
m_rcPinOut.left = m_rcBlk.CenterPoint ().x - PIN_RADIUS;
m_rcPinOut.top = m_rcBlk.CenterPoint ().y + int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinOut.right = m_rcBlk.CenterPoint ().x + PIN_RADIUS;
m_rcPinOut.bottom = m_rcBlk.CenterPoint ().y + int (m_rcBlk.Height () / 2) + PIN_RADIUS;
}
CRect CBlockAnd::GetPinIn1Rect () const
{
return m_rcPinIn1;
}
CRect CBlockAnd::GetPinIn2Rect () const
{
return m_rcPinIn2;
}
CRect CBlockAnd::GetPinOutRect () const
{
return m_rcPinOut;
}
CBlockBasis* CBlockAnd::GetBlkHead1Ptr () const
{
return m_pBlkHead1;
}
CBlockBasis* CBlockAnd::GetBlkHead2Ptr () const
{
return m_pBlkHead2;
}
void CBlockAnd::SetBlkHead1Ptr (CBlockBasis* pBlkHead)
{
m_pBlkHead1 = pBlkHead;
}
void CBlockAnd::SetBlkHead2Ptr (CBlockBasis* pBlkHead)
{
m_pBlkHead2 = pBlkHead;
}
int CBlockAnd::GetBlkHead1Num () const
{
return m_iBlkHead1Num;
}
int CBlockAnd::GetBlkHead2Num () const
{
return m_iBlkHead2Num;
}
void CBlockAnd::SetBlkHead1Num (int iHeadBlkNum)
{
m_iBlkHead1Num = iHeadBlkNum;
}
void CBlockAnd::SetBlkHead2Num (int iHeadBlkNum)
{
m_iBlkHead2Num = iHeadBlkNum;
}
double CBlockAnd::GetBlkValue () const
{
//And<6E>޿<EFBFBD> (<28>Y<EFBFBD><59><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>Ҭ<EFBFBD>1<EFBFBD>A<EFBFBD>h<EFBFBD>^<5E><>1<EFBFBD>A<EFBFBD>_<EFBFBD>h<EFBFBD>^<5E><>0)
if (abs (m_pBlkHead1->GetBlkValue () - 1) < TOLERANCE && abs (m_pBlkHead2->GetBlkValue () - 1) < TOLERANCE)
return 1.;
else
return 0.;
}
int CBlockAnd::GetValueFlag () const
{
if (m_pBlkHead1 == NULL || m_pBlkHead2 == NULL) //<2F>Y<EFBFBD>s<EFBFBD>b<EFBFBD><62><EFBFBD>@<40><><EFBFBD>J<EFBFBD><4A><EFBFBD>I<EFBFBD><49><EFBFBD>s<EFBFBD><73><EFBFBD>A<EFBFBD>^<5E><>"<22>L<EFBFBD><4C>"
return NO_VALUE;
else if (m_pBlkHead1->GetValueFlag () == NO_VALUE || m_pBlkHead2->GetValueFlag () == NO_VALUE) //<2F>Y<EFBFBD>s<EFBFBD>b<EFBFBD><62><EFBFBD>@<40><><EFBFBD>J<EFBFBD><4A><EFBFBD>L<EFBFBD>ȡA<C8A1>^<5E><>"<22>L<EFBFBD><4C>"
return NO_VALUE;
else if (m_pBlkHead1->GetValueFlag () == COMPUTE_ERROR || m_pBlkHead2->GetValueFlag () == COMPUTE_ERROR) //<2F>Y<EFBFBD>s<EFBFBD>b<EFBFBD><62><EFBFBD>@<40><><EFBFBD>J<EFBFBD><4A><EFBFBD>p<EFBFBD><70><EFBFBD><EFBFBD><EFBFBD>~<7E>A<EFBFBD>^<5E><>"<22>p<EFBFBD><70><EFBFBD><EFBFBD><EFBFBD>~"
return COMPUTE_ERROR;
else if (m_pBlkHead1->GetValueFlag () == ANALOG_VALUE || m_pBlkHead2->GetValueFlag () == ANALOG_VALUE) //<2F><><EFBFBD><EFBFBD><EFBFBD>\<5C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>J
return COMPUTE_ERROR;
else if (m_pBlkHead1->GetValueFlag () == NO_OPER_FLAG || m_pBlkHead2->GetValueFlag () == NO_OPER_FLAG) //<2F><><EFBFBD>]<5D>w<EFBFBD>B<EFBFBD>
return NO_OPER_FLAG;
else if (m_pBlkHead1->GetValueFlag () == DIGITAL_VALUE && m_pBlkHead2->GetValueFlag () == DIGITAL_VALUE) //<2F>Y<EFBFBD><59><EFBFBD>J<EFBFBD>Ҭ<EFBFBD><D2AC>Ʀ<EFBFBD><C6A6>T<EFBFBD><54><EFBFBD>A<EFBFBD>h<EFBFBD>^<5E>ǼƦ<C7BC><C6A6>T<EFBFBD><54>
return DIGITAL_VALUE;
else
return NO_VALUE;
}
void CBlockAnd::Move (CPoint ptCursor)
{
CBlockBasis::Move (ptCursor);
m_rcPinIn1.left = m_rcBlk.CenterPoint ().x - int (m_rcBlk.Width () / 4) - PIN_RADIUS;
m_rcPinIn1.top = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinIn1.right = m_rcBlk.CenterPoint ().x - int (m_rcBlk.Width () / 4) + PIN_RADIUS;
m_rcPinIn1.bottom = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) + PIN_RADIUS;
m_rcPinIn2.left = m_rcBlk.CenterPoint ().x + int (m_rcBlk.Width () / 4) - PIN_RADIUS;
m_rcPinIn2.top = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinIn2.right = m_rcBlk.CenterPoint ().x + int (m_rcBlk.Width () / 4) + PIN_RADIUS;
m_rcPinIn2.bottom = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) + PIN_RADIUS;
m_rcPinOut.left = m_rcBlk.CenterPoint ().x - PIN_RADIUS;
m_rcPinOut.top = m_rcBlk.CenterPoint ().y + int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinOut.right = m_rcBlk.CenterPoint ().x + PIN_RADIUS;
m_rcPinOut.bottom = m_rcBlk.CenterPoint ().y + int (m_rcBlk.Height () / 2) + PIN_RADIUS;
}
void CBlockAnd::Draw (CDC* pDC)
{
CBrush brushPin (GOLD), * pbrushOld;
pbrushOld = pDC->SelectObject (&brushPin);
pDC->Ellipse (m_rcPinIn1);
pDC->Ellipse (m_rcPinIn2);
pDC->Ellipse (m_rcPinOut);
pDC->SelectObject (pbrushOld);
CBlockBasis::Draw (pDC);
pDC->DrawTextA (_T ("AND"), -1, &m_rcBlk, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
void CBlockAnd::DrawLine (CDC* pDC)
{
if (m_pBlkHead1 != NULL)
{
CPen penLine (PS_SOLID, 5, BLACK);
CPen* pOldPen;
pOldPen = pDC->SelectObject (&penLine);
CPoint ptStart = m_pBlkHead1->GetPinOutRect ().CenterPoint (); //<2F>s<EFBFBD>u<EFBFBD>_<EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>JBlock<63><6B><EFBFBD><EFBFBD><EFBFBD>XPin<69>}<7D><><EFBFBD><EFBFBD><EFBFBD>I
CPoint ptEnd = m_rcPinIn1.CenterPoint (); //<2F>s<EFBFBD>u<EFBFBD><75><EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD>Block<63><6B><EFBFBD><EFBFBD><EFBFBD>JPin<69>}<7D><><EFBFBD><EFBFBD><EFBFBD>I
int iGrid = (int) (m_rcBlk.Height () / 2. + 0.5); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>I<EFBFBD><49><EFBFBD>j
int iCornerGridY = (int) (((ptStart.y + ptEnd.y) / 2. - ptStart.y) / iGrid + 0.5); //<2F>p<EFBFBD><70><EFBFBD>s<EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD>Z<EFBFBD><5A><EFBFBD><EFBFBD><EFBFBD>JBlock<63>X<EFBFBD>ӹj<D3B9>I
int iCornerY = iCornerGridY * iGrid + ptStart.y; //<2F>s<EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD>BY<42>y<EFBFBD>Ц<EFBFBD><D0A6>m
pDC->MoveTo (ptStart);
pDC->LineTo (CPoint (ptStart.x, iCornerY));
pDC->MoveTo (CPoint (ptStart.x, iCornerY));
pDC->LineTo (CPoint (ptEnd.x, iCornerY));
pDC->MoveTo (CPoint (ptEnd.x, iCornerY));
pDC->LineTo (ptEnd);
pDC->SelectObject (pOldPen);
}
if (m_pBlkHead2 != NULL)
{
CPen penLine (PS_SOLID, 5, BLACK);
CPen* pOldPen;
pOldPen = pDC->SelectObject (&penLine);
CPoint ptStart = m_pBlkHead2->GetPinOutRect ().CenterPoint (); //<2F>s<EFBFBD>u<EFBFBD>_<EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>JBlock<63><6B><EFBFBD><EFBFBD><EFBFBD>XPin<69>}<7D><><EFBFBD><EFBFBD><EFBFBD>I
CPoint ptEnd = m_rcPinIn2.CenterPoint (); //<2F>s<EFBFBD>u<EFBFBD><75><EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD>Block<63><6B><EFBFBD><EFBFBD><EFBFBD>JPin<69>}<7D><><EFBFBD><EFBFBD><EFBFBD>I
int iGrid = (int) (m_rcBlk.Height () / 2. + 0.5); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>I<EFBFBD><49><EFBFBD>j
int iCornerGridY = (int) (((ptStart.y + ptEnd.y) / 2. - ptStart.y) / iGrid + 0.5); //<2F>p<EFBFBD><70><EFBFBD>s<EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD>Z<EFBFBD><5A><EFBFBD><EFBFBD><EFBFBD>JBlock<63>X<EFBFBD>ӹj<D3B9>I
int iCornerY = iCornerGridY * iGrid + ptStart.y; //<2F>s<EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD>BY<42>y<EFBFBD>Ц<EFBFBD><D0A6>m
pDC->MoveTo (ptStart);
pDC->LineTo (CPoint (ptStart.x, iCornerY));
pDC->MoveTo (CPoint (ptStart.x, iCornerY));
pDC->LineTo (CPoint (ptEnd.x, iCornerY));
pDC->MoveTo (CPoint (ptEnd.x, iCornerY));
pDC->LineTo (ptEnd);
pDC->SelectObject (pOldPen);
}
}
int CBlockAnd::BlkTypeIs () const
{
return BLK_AND;
}