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

📄 curveview.cpp

📁 这是一个贝塞尔曲线生成的程序
💻 CPP
字号:
// curveView.cpp : implementation of the CCurveView class
//

#include "stdafx.h"
#include "curve.h"

#include "curveDoc.h"
#include "curveView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCurveView

IMPLEMENT_DYNCREATE(CCurveView, CView)

BEGIN_MESSAGE_MAP(CCurveView, CView)
	//{{AFX_MSG_MAP(CCurveView)
	ON_COMMAND(ID_DRAW_BEZIER2, OnDrawBezier2)
	ON_COMMAND(ID_DRAW_BEZIER3, OnDrawBezier3)
	ON_COMMAND(ID_CLRSCR, OnClrscr)
	ON_COMMAND(ID_DRAW_B2, OnDrawB2)
	ON_COMMAND(ID_DRAW_B3, OnDrawB3)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CCurveView construction/destruction

CCurveView::CCurveView()
{
	// TODO: add construction code here

}

CCurveView::~CCurveView()
{
}

BOOL CCurveView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CCurveView drawing

void CCurveView::OnDraw(CDC* pDC)
{
	CCurveDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CCurveView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CCurveView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CCurveView message handlers

void CCurveView::bezier2(CDC *pDC, int points[][2])
{
	int n=100;
	double t,dt=1.0/n;
	int i,j;
	int x,y;
	
	pDC->MoveTo(points[0][0],points[0][1]);
	for(i=1;i<=n;i++)
	{
		t = i*dt;
		x = (1-t)*(1-t)*points[0][0] + 2*t*(1-t)*points[1][0] + t*t*points[2][0];
		y = (1-t)*(1-t)*points[0][1] + 2*t*(1-t)*points[1][1] + t*t*points[2][1];
		//pDC->LineTo(x,y);
		pDC->SetPixel(x,y,RGB(255,0,0));
	}
}

void CCurveView::OnDrawBezier2() 
{
	// TODO: Add your command handler code here
	int points[][2]={{100,200},{200,100},{300,300}};
	CDC *pDC = GetDC();
	//画控制多边形
	pDC->MoveTo(points[0][0],points[0][1]);
	for(int i=1;i<3;i++);
	//	pDC->LineTo(points[i][0],points[i][1]);

	//画二次Bezier曲线;
	bezier2(pDC,points);
}

void CCurveView::bezier3(CDC *pDC, int points[][2])
{
	int n=100;
	double t,dt=1.0/n;
	int i,j;
	int p[2];
	
	pDC->MoveTo(points[0][0],points[0][1]);
	for(i=1;i<=n;i++)
	{
		t = i*dt;
		for(j=0;j<2;j++)
			p[j] = (1-t)*(1-t)*(1-t)*points[0][j] + 3*t*(1-t)*(1-t)*points[1][j] + 3*t*t*(1-t)*points[2][j] + t*t*t*points[3][j];
		pDC->LineTo(p[0],p[1]);
	}
}

void CCurveView::OnDrawBezier3() 
{
	// TODO: Add your command handler code here
	int points[][2]={{100,200},{200,100},{300,150},{400,300}};
	CDC *pDC = GetDC();
	//画控制多边形
	pDC->MoveTo(points[0][0],points[0][1]);
	for(int i=1;i<4;i++)
		pDC->LineTo(points[i][0],points[i][1]);

	//画二次Bezier曲线
	bezier3(pDC,points);
}

void CCurveView::B2(CDC *pDC, int points[][2])
{
	int n=100;
	double t,dt=1.0/n;
	int i,j;
	int p[2];

	//移到起点
	for(j=0;j<2;j++)
		p[j]=(points[0][j]+points[1][j])/2;
	pDC->MoveTo(p[0],p[1]);

	for(i=1;i<=n;i++)
	{
		t = i*dt;
		for(j=0;j<2;j++)
			p[j] = 0.5*((1-t)*(1-t)*points[0][j] + (1+2*t-2*t*t)*points[1][j] + t*t*points[2][j]);
		pDC->LineTo(p[0],p[1]);
	}
}

void CCurveView::OnDrawB2() 
{
	// TODO: Add your command handler code here
	int points[][2]={{100,200},{200,100},{300,300}};
	CDC *pDC = GetDC();
	//画控制多边形
	pDC->MoveTo(points[0][0],points[0][1]);
	for(int i=1;i<3;i++)
		pDC->LineTo(points[i][0],points[i][1]);

	//画二次Bezier曲线;
	B2(pDC,points);
}

void CCurveView::B3(CDC *pDC, int points[][2])
{
	int n=100;
	double t,dt=1.0/n;
	int i,j;
	int p[2];
	

	//移到起点
	for(j=0;j<2;j++)
		p[j]=(points[0][j]+4*points[1][j]+points[2][j])/6;
	pDC->MoveTo(p[0],p[1]);
	
	for(i=0;i<=n;i++)
	{
		t = i*dt;
		for(j=0;j<2;j++)
			p[j] =((1-t)*(1-t)*(1-t)*points[0][j] + (3*t*t*(t-2)+4)*points[1][j] 
			+ (3*t*t*(1-t)+3*t+1)*points[2][j] + t*t*t*points[3][j])/6;
		pDC->LineTo(p[0],p[1]);
	}
	//移到起点
	for(j=0;j<2;j++)
		p[j]=(points[0][j]+4*points[1][j]+points[2][j])/6;
	pDC->MoveTo(p[0],p[1]-1);
	
	for(i=0;i<=n;i++)
	{
		t = i*dt;
		for(j=0;j<2;j++)
			p[j] =((1-t)*(1-t)*(1-t)*points[0][j] + (3*t*t*(t-2)+4)*points[1][j] 
			+ (3*t*t*(1-t)+3*t+1)*points[2][j] + t*t*t*points[3][j])/6;
		pDC->LineTo(p[0],p[1]-1);
	}
}

void CCurveView::OnDrawB3() 
{
	// TODO: Add your command handler code here
	int points[][2]={{100,200},{200,100},{300,150},{400,300}};
	CDC *pDC = GetDC();
	//画控制多边形
	pDC->MoveTo(points[0][0],points[0][1]);
	for(int i=1;i<4;i++)
		pDC->LineTo(points[i][0],points[i][1]);

	pDC->MoveTo(points[0][0],points[0][1]-1);
	for(int ii=1;ii<4;ii++)
		pDC->LineTo(points[ii][0],points[ii][1]-1);
	//画二次Bezier曲线
	B3(pDC,points);
}

void CCurveView::OnClrscr() 
{
	// TODO: Add your command handler code here	
	RedrawWindow();
}

⌨️ 快捷键说明

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