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

📄 b_quxianview.cpp

📁 绘制B样条曲线
💻 CPP
字号:
// B_QUXIANView.cpp : implementation of the CB_QUXIANView class
//

#include "stdafx.h"
#include "B_QUXIAN.h"

#include "B_QUXIANDoc.h"
#include "B_QUXIANView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CB_QUXIANView

IMPLEMENT_DYNCREATE(CB_QUXIANView, CView)

BEGIN_MESSAGE_MAP(CB_QUXIANView, CView)
	//{{AFX_MSG_MAP(CB_QUXIANView)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_WM_PAINT()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CB_QUXIANView construction/destruction

CB_QUXIANView::CB_QUXIANView()
{
	// TODO: add construction code here
	index=0;
	i=0;

}

CB_QUXIANView::~CB_QUXIANView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CB_QUXIANView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CB_QUXIANView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CB_QUXIANView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CB_QUXIANView message handlers

void CB_QUXIANView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	index++;
	if(index==1)
	{
		oldpoint=point;
		index=2;
		P[0]=point;
		i++;

	}
	else if(index>2)
	{
		dc.MoveTo(oldpoint);
		dc.LineTo(point);
		oldpoint=point;
		P[i]=point;
		i++;

	}

	CView::OnLButtonDown(nFlags, point);
}



void CB_QUXIANView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	CPoint p;
	double t;
	double a;
	int n;
	for(n=0;n+3<i;n++)
	{
	
		for(t=0;t<=1;t+=0.0001)
		{
	       a=P[n].x*1/6*(-t*t*t+3*t*t-3*t+1)+P[n+1].x*1/6*(3*t*t*t-6*t*t+4)+P[n+2].x*1/6*(-3*t*t*t+3*t*t+3*t+1)+P[n+3].x*1/6*t*t*t;
	       if(a-int(a)>=0.5)
		   {
		      a++;
		   }
	       p.x= a;
	       a=P[n].y*1/6*(-t*t*t+3*t*t-3*t+1)+P[n+1].y*1/6*(3*t*t*t-6*t*t+4)+P[n+2].y*1/6*(-3*t*t*t+3*t*t+3*t+1)+P[n+3].y*1/6*t*t*t;
	       if(a-int(a)>=0.5)
		   {
		      a++;
		   }
	       p.y= a;
	       dc.SetPixel(p.x,p.y,RGB(255,0,0));
		}
	}
	CView::OnRButtonDown(nFlags, point);
}

void CB_QUXIANView::OnFileNew() 
{
	// TODO: Add your command handler code here
	i=0;
	index=0;
	Invalidate();
	
}



void CB_QUXIANView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	dc.TextOut(20,20,"单击左键确定各顶点,单击右键画出曲线");
	
	// Do not call CView::OnPaint() for painting messages
}

⌨️ 快捷键说明

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