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

📄 test2view.cpp

📁 心形线
💻 CPP
字号:
// test2View.cpp : implementation of the CTest2View class
//

#include "stdafx.h"
#include "test2.h"

#include "test2Doc.h"
#include "test2View.h"
#include <math.h>
#define PI 3.1415926

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
float a = 300;
float xCircle, yCircle, xPoint, yPoint, w;
POINT polypts[250];
int nCounts = 0;
bool pause = false;
// CTest2View

IMPLEMENT_DYNCREATE(CTest2View, CView)

BEGIN_MESSAGE_MAP(CTest2View, CView)
	//{{AFX_MSG_MAP(CTest2View)
	ON_WM_KEYDOWN()
	ON_WM_TIMER()
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest2View construction/destruction

CTest2View::CTest2View()
{
	// TODO: add construction code here
}

CTest2View::~CTest2View()
{
}

BOOL CTest2View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTest2View drawing

void CTest2View::OnDraw(CDC* pDC)
{
	CTest2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CRect  cRect;                                    //客户区矩形
    CPen  *cOldPen,
		cRedPen1(PS_SOLID, 5, RGB(255,0,0)),
		cRedPen2(PS_SOLID, 10, RGB(255,0,0)),
		cYellowPen(PS_SOLID, 5, RGB(255,255,0)),
		cGreenPen(PS_SOLID,5,RGB(0,255,0)),
		cBluePen(PS_SOLID, 5, 0x02ff0000);//笔声明
	CBrush *pOldBrush, cNewBrush;//旧画笔、新画笔声明

    int  OrgDC;

    OrgDC = pDC->SaveDC();     //保存原始DC,供以后恢复它时使用
    pDC->SetMapMode(MM_ISOTROPIC);   //设定"各向同性UCS"映射方式
	pDC->SetWindowExt(4000, 3000);   //设定窗口范围 
    AfxGetMainWnd()->GetClientRect(cRect);//获取主窗口的客户区矩形
	pDC->SetViewportExt(cRect.Width(), -cRect.Height());//设定视口范围
	pDC->DPtoLP(cRect);                   //将设备坐标转换成逻辑坐标
	pDC->SetWindowOrg(-(cRect.Width()/2), -(cRect.Height()/2));//设定窗口原点为中心点

	xCircle = 2*a*sin(w*PI);
	yCircle = 2*a*cos(w*PI);            //计算转动的圆的圆心坐标
	yPoint = a*(2*cos(w*PI) - cos(2*w*PI));
	xPoint = a*(2*sin(w*PI) - sin(2*w*PI));//计算固定点的当前位置
	polypts[nCounts].x = xPoint;
	polypts[nCounts].y = yPoint;   //将当前位置保存进数组
	

	pDC->SelectObject(&cGreenPen);
	pDC->Ellipse(xCircle-a, yCircle+a, xCircle+a, yCircle-a);    //画外面转动的绿色的圆

    pDC->SelectObject(&cRedPen1);
	cNewBrush.CreateSolidBrush(RGB(255,0,0));
	pOldBrush = pDC->SelectObject(&cNewBrush);
	pDC->Ellipse(xPoint-15, yPoint+15, xPoint+15, yPoint-15);
	pDC->SelectObject(pOldBrush);
	cNewBrush.DeleteObject();           //画绿色圆上的那个参考红点

	pDC->SelectObject(&cRedPen2);
	pDC->Polyline(polypts,nCounts);      //画心形线

	pDC->SelectObject(&cRedPen1);
    pDC->Ellipse(-a,a,a,-a);             //画坐标原点的固定不动的红圆

	pDC->SelectObject(&cYellowPen);
	pDC->MoveTo(0,0);
	pDC->LineTo(xCircle, yCircle);                   //画两个大圆之间的连心线

	pDC->SelectObject(&cBluePen);
	pDC->MoveTo(0,0);
	pDC->LineTo(0,(cRect.Height()/2));
	pDC->MoveTo(0,0);
	pDC->LineTo(0,-(cRect.Height()/2));
	pDC->MoveTo(0,0);
	pDC->LineTo((cRect.Width()/2),0);
	pDC->MoveTo(0,0);
	pDC->LineTo(-(cRect.Width()/2),0); //画坐标轴
	DrawText(pDC);

   pDC->RestoreDC(OrgDC);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTest2View printing

BOOL CTest2View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTest2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTest2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTest2View diagnostics

#ifdef _DEBUG
void CTest2View::AssertValid() const
{
	CView::AssertValid();
}

void CTest2View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTest2Doc* CTest2View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTest2Doc)));
	return (CTest2Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTest2View message handlers

void CTest2View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	int aOld = a;
	int i;
	switch( nChar )
	{
	case ' ':
		if(pause == false)
			pause = true;
		else
			pause = false;
		Invalidate();
		break;
	case VK_UP:
		if ( a >= 430 )
			a = 430;
		else
			a += 10;
		for(i=0; i<=nCounts; i++)
		{
			polypts[i].x = polypts[i].x*a/aOld;
			polypts[i].y = polypts[i].y*a/aOld;
		}
		Invalidate();
		break;
	case VK_DOWN:
		if ( a <= 150 )
			a = 150;
		else
			a -= 10;
		for(i=0; i<=nCounts; i++)
		{
			polypts[i].x = polypts[i].x*a/aOld;
			polypts[i].y = polypts[i].y*a/aOld;
		}
		Invalidate();
		break;
	default:break;
	}
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}


void CTest2View::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	int i;
	if(!pause)
	{
	     w += 0.01;
		 nCounts++;
	     if( w >= 2 )
		 {
		    w = 0;
			nCounts = 0;
			for( i=0; i<=250; i++)
			{
				polypts[i].x = 0;
				polypts[i].y = 0;
			}

		 }
	}
	Invalidate();
	CView::OnTimer(nIDEvent);
}

int CTest2View::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	SetTimer(1,100,NULL); //时间间隔为0.1秒
	return 0;
}

void CTest2View::DrawText(CDC *pDC)
{
    CFont *pOldFont,cNewFont;
	//cNewFont.CreateFont(20,15,450,450,FW_BOLD,FALSE,FALSE,FALSE,OEM_CHARSET,
	//OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,VARIABLE_PITCH|FF_ROMAN,"Roman");
	cNewFont.CreateStockObject(SYSTEM_FIXED_FONT);
	pOldFont=pDC->SelectObject(&cNewFont);
	pDC->SetTextColor(0x0000);
	pDC->TextOut(-2100, 1400,"周晓波  空间中心 200728007329058");
	pDC->TextOut(-350, 1400,"心形线动态生成演示");
	pDC->TextOut(-2100, 1200,"控制键:");
	pDC->TextOut(-2100, 1000,"UP:    放大");
	pDC->TextOut(-2100, 800,"DOWN:  缩小");
	pDC->TextOut(-2100, 600,"SPACE: 暂停/继续");
	if(a == 430)
	{
		pDC->SetTextColor(0x00ff);
		pDC->TextOut(-2100, 400,"尺寸已为最大,无法再增大!");
	}
	if(a == 150)
	{
		pDC->SetTextColor(0x00ff);
		pDC->TextOut(-2100, 400,"尺寸已为最小,无法再缩小!");
	}
}

⌨️ 快捷键说明

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