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

📄 exampleview.cpp

📁 一个非常有帮助的绘制贝塞尔曲线的应用程序
💻 CPP
字号:
// ExampleView.cpp : implementation of the CExampleView class
//
#include <math.h>
#include "stdafx.h"
#include "Example.h"

#include "ExampleDoc.h"
#include "ExampleView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CExampleView
static int	 m;
IMPLEMENT_DYNCREATE(CExampleView, CView)

BEGIN_MESSAGE_MAP(CExampleView, CView)
	//{{AFX_MSG_MAP(CExampleView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_ASCEND, OnAscend)
	ON_UPDATE_COMMAND_UI(ID_ASCEND, OnUpdateAscend)
	ON_COMMAND(ID_DECEND, OnDecend)
	ON_UPDATE_COMMAND_UI(ID_DECEND, OnUpdateDecend)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_REDRAW, OnRedraw)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CExampleView construction/destruction

CExampleView::CExampleView()
{
	// TODO: add construction code here
	ascend=false;
	decend=false;

	for(int i=0;i<=100;i++)
	{
		pt[i].x=0;
		pt[i].y=0;
		pt1[i].x=0;
		pt1[i].y=0;
		pt2[i].x=0;
		pt2[i].y=0;
	
	}
}

CExampleView::~CExampleView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CExampleView drawing

void CExampleView::OnDraw(CDC* pDC)
{
	CExampleDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	double t=0.0,start_x=0.0,start_y=0.0,end_x=0.0,end_y=0.0;
	double s_x=0.0,s_y=0.0;
	int n=m-1;
	int j=0;
	if(m!=0)
	{

		pDC->SetPixel(pt[1].x, pt[1].y, RGB(255,0,0));
		pDC->Rectangle(pt[1].x-2,pt[1].y-2,pt[1].x+2,pt[1].y+2);
		for(int k=1;k<m;k++)
		{
			pDC->Rectangle(pt[k].x-2,pt[k].y-2,pt[k].x+2,pt[k].y+2);
			pDC->MoveTo(pt[k].x,pt[k].y);
			pDC->LineTo(pt[k+1].x,pt[k+1].y);

		}
		if(k==m)
		{
		     pDC->Rectangle(pt[k].x-2,pt[k].y-2,pt[k].x+2,pt[k].y+2);
		}
		start_x=pt[1].x;
		start_y=pt[1].y;
		for(int i=0;i<=1000;i++)
		{
			t=i*0.001;
			end_x=0.0;
			end_y=0.0;
			for(int j=0;j<=n;j++)
			{
				s_x=0.0;
				s_y=0.0;
				s_x=factorial(n)/(factorial(j)*factorial(n-j))*multiple(t,j)*multiple(1-t,n-j)*pt[j+1].x;
				s_y=factorial(n)/(factorial(j)*factorial(n-j))*multiple(t,j)*multiple(1-t,n-j)*pt[j+1].y;
				end_x+=s_x;
				end_y+=s_y;
				
			}	
			pDC->MoveTo(start_x,start_y);
			pDC->LineTo(end_x,end_y);
			start_x=end_x;
			start_y=end_y;	
		}
		
	}
}

/////////////////////////////////////////////////////////////////////////////
// CExampleView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CExampleView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CExampleView message handlers

void CExampleView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m++;
	pt[m].x=point.x;
	pt[m].y=point.y;
	CView::OnLButtonDown(nFlags, point);
}


void CExampleView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	Invalidate();
	CView::OnLButtonUp(nFlags, point);
}

double CExampleView::factorial(int n)
{
	double t=0;
	if(n==0) 
		t=1;
	else
		t=n*factorial(n-1);
	return t;
}
double CExampleView::multiple(double t,int j)
{
	double s=1;
	if(j==0)
		s=1;
	else
	{
		for(int k=1;k<=j;k++)
			s=s*t;
	}
	return s;
}
		


void CExampleView::OnAscend() 
{
	// TODO: Add your command handler code here
	ascend=true;
	decend=false;
	
}

void CExampleView::OnUpdateAscend(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(ascend);
}

void CExampleView::OnDecend() 
{
	// TODO: Add your command handler code here
	decend=true;
	ascend=false;
}

void CExampleView::OnUpdateDecend(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(decend);
}

void CExampleView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	double t=0.0,start_x=0.0,start_y=0.0,end_x=0.0,end_y=0.0;
	double s_x=0.0,s_y=0.0;
	int n=m-1;
	int j=0;
    if(m!=0)
	{
		//升阶
		if(ascend==true)
		{
			pt[0].x=0.0;
			pt[0].y=0.0;
			pt[m+1].x=0.0;
			pt[m+1].y=0.0;
			for(j=0;j<=n+1;j++)
			{
				pt1[j+1].x=(1-(double(j)/double(n+1)))*pt[j+1].x+(double(j)/double(n+1))*pt[j].x;
				pt1[j+1].y=(1-(double(j)/double(n+1)))*pt[j+1].y+(double(j)/double(n+1))*pt[j].y;
			}
			m++;
			for(j=1;j<=m;j++)
			{
				pt[j].x=pt1[j].x;
				pt[j].y=pt1[j].y;
			}
		}
		
		
		//降阶)
		if(decend==true)
		{
			if(m>2)
			{
				for(j=0;j<=n-1;j++)
				{
					pt1[j+1].x=double(n*pt[j+1].x-j*pt1[j].x)/double(n-j);
					pt1[j+1].y=double(n*pt[j+1].y-j*pt1[j].y)/double(n-j);
				}				
				for(j=n;j>=1;j--)
				{
					pt2[j].x=double(n*pt[j+1].x-(n-j)*pt2[j+1].x)/double(j);
					pt2[j].y=double(n*pt[j+1].y-(n-j)*pt2[j+1].y)/double(j);
				}
				for(j=0;j<=n-1;j++)
				{
					pt[j+1].x=(1-double(j)/double(n-1))*pt1[j+1].x+double(j)/double(n-1)*pt2[j+1].x;
					pt[j+1].y=(1-double(j)/double(n-1))*pt1[j+1].y+double(j)/double(n-1)*pt2[j+1].y;
				}
				m--;
				if(m<=2)	
				{
					MessageBox("降阶最多降阶到3次");
					return;
				}	
			}	
		}
	}
	RedrawWindow();	
	CView::OnRButtonDown(nFlags, point);
}

void CExampleView::OnRedraw() 
{
	// TODO: Add your command handler code here
	m=0;
	ascend=false;
	decend=false;
	for(int i=0;i<=100;i++)
	{
		pt[i].x=0;
		pt[i].y=0;
		pt1[i].x=0;
		pt1[i].y=0;
		pt2[i].x=0;
		pt2[i].y=0;

	}
	RedrawWindow();
}

⌨️ 快捷键说明

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