📄 shape.cpp
字号:
// Shape.cpp: implementation of the shape classes.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <math.h>
#include "Shape.h"
#define PI 3.14159
#define RADIAN 57.28578
//////////////////////////////////////////////////////////////////////
// CShape Class
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CShape::CShape(HWND hWnd, HDC hdc)
{
m_hWnd = hWnd;
m_hDC = hdc;
}
CShape::~CShape()
{
}
void CShape::Show()
{
HPEN hPen, hOldPen;
hPen = CreatePen (PS_SOLID, 2, RGB(0x0, 0x0, 0x0));
hOldPen = (HPEN) SelectObject (m_hDC, hPen);
Draw ();
SelectObject (m_hDC, hOldPen);
DeleteObject (hPen);
}
void CShape::Draw()
{
RECT rc;
SIZE size;
GetClientRect (m_hWnd, &rc);
size.cx = rc.right / 3;
size.cy = rc.bottom / 3;
Rectangle (m_hDC, rc.right / 2 - size.cy, rc.bottom / 2 - size.cy,
rc.right / 2 + size.cy, rc.bottom / 2 + size.cy);
Ellipse (m_hDC, rc.right / 2 - size.cy, rc.bottom / 2 - size.cy,
rc.right / 2 + size.cy, rc.bottom / 2 + size.cy);
}
//////////////////////////////////////////////////////////////////////
// CCircle Class
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCircle::CCircle(HWND hWnd, HDC hdc, POINT & ptCenter, int nRadius)
: CShape (hWnd, hdc)
{
m_ptCenter = ptCenter;
m_nRadius = nRadius;
}
CCircle::~CCircle()
{
}
void CCircle::Draw()
{
CArc arc (m_hWnd, m_hDC, m_ptCenter, m_nRadius, 900, 900);
for (int i = 0; i < 4; ++i)
{
arc.Draw ();
arc.Rotate (900);
}
}
//////////////////////////////////////////////////////////////////////
// CLine Class
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CLine::CLine(HWND hWnd, HDC hdc, POINT & ptStart, POINT ptEnd) : CShape (hWnd, hdc)
{
m_ptStart = ptStart;
m_ptEnd = ptEnd;
//
// Calculate the radius and current angle of the line
int cx = m_ptEnd.x - m_ptStart.x;
int cy = m_ptEnd.y - m_ptStart.y;
m_fRadius = sqrt (abs(cx * cx + cy * cy));
m_fCurAngle = asin ((m_ptStart.y - m_ptEnd.y) / m_fRadius);
}
CLine::~CLine()
{
}
void CLine::Draw()
{
MoveToEx (m_hDC, m_ptStart.x, m_ptStart.y, NULL);
LineTo (m_hDC, m_ptEnd.x, m_ptEnd.y);
}
void CLine::Rotate(int nDegrees, int nWhichEnd)
{
double fAngle;
POINT ptTemp;
GetCurrentPositionEx (m_hDC, &ptTemp);
if (!m_fRadius)
return;
fAngle = (double) nDegrees / (10.0 * RADIAN);
fAngle += m_fCurAngle;
if (!nWhichEnd) // Rotate the begin point
{
m_ptEnd.x = m_ptStart.x + (int) ((cos (fAngle) * m_fRadius));
m_ptEnd.y = m_ptStart.y - (int) ((sin (fAngle) * m_fRadius));
}
else // Rotate the end point
{
//
// New start point is the old end point.
// Calculate the new end point. Rotate the angle
// 180 degrees to indicate that we are going from
// end to start points.
fAngle += PI;
m_ptStart = m_ptEnd;
m_ptEnd.x = m_ptEnd.x - (int) ((cos (fAngle) * m_fRadius));
m_ptEnd.y = m_ptEnd.y + (int) ((sin (fAngle) * m_fRadius));
}
m_fCurAngle = fAngle;
}
//////////////////////////////////////////////////////////////////////
// CSquare Class
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSquare::CSquare(HWND hWnd, HDC hdc, POINT & ptStart, POINT & ptEnd, int nSquash)
: CShape (hWnd, hdc)
{
m_ptStart = ptStart;
m_ptEnd = ptEnd;
m_nSquash = nSquash;
}
CSquare::~CSquare()
{
}
void CSquare::Draw()
{
CLine line (m_hWnd, m_hDC, m_ptStart, m_ptEnd);
line.Show ();
line.Rotate (-900 + m_nSquash, 1);
line.Show ();
line.Rotate (900 - m_nSquash, 1);
line.Show ();
line.Rotate (900 + m_nSquash, 1);
line.Show ();
}
//////////////////////////////////////////////////////////////////////
// CArc Class
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CArc::CArc(HWND hWnd, HDC hdc, POINT & ptCenter, int nRadius,
int nStart, int nSubtend)
: CShape (hWnd, hdc)
{
m_fRadius = (double) nRadius;
m_ptCenter = ptCenter;
m_fStart = ((double) nStart / 10.0) / RADIAN;
m_fEnd = m_fStart + ((double) nSubtend / 10.0) / RADIAN;
Rotate (0);
}
CArc::~CArc()
{
}
void CArc::Draw()
{
Arc (m_hDC, m_ptCenter.x - (int) m_fRadius,
m_ptCenter.y - (int) m_fRadius,
m_ptCenter.x + (int) m_fRadius,
m_ptCenter.y + (int) m_fRadius,
m_ptStart.x, m_ptStart.y,
m_ptEnd.x, m_ptEnd.y);
}
void CArc::Rotate(int nDegrees)
{
m_fStart += ((double) nDegrees / 10.0) / RADIAN;
m_fEnd += ((double) nDegrees / 10.0) / RADIAN;
m_ptStart.x = m_ptCenter.x + (int) ((cos (m_fStart) * m_fRadius));
m_ptStart.y = m_ptCenter.y - (int) ((sin (m_fStart) * m_fRadius));
m_ptEnd.x = m_ptCenter.x - (int) ((cos (m_fEnd) * m_fRadius));
m_ptEnd.y = m_ptCenter.y + (int) ((sin (m_fEnd) * m_fRadius));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -