📄 beziercurvesurfview.cpp
字号:
// BezierCurveSurfView.cpp : implementation of the CBezierCurveSurfView class
//
#include "stdafx.h"
#include "BezierCurveSurf.h"
#include "BezierCurveSurfView.h"
#include <math.h>
#include "MainFrm.h"
//#include "InputDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBezierCurveSurfView
IMPLEMENT_DYNCREATE(CBezierCurveSurfView, CView)
BEGIN_MESSAGE_MAP(CBezierCurveSurfView, CView)
//{{AFX_MSG_MAP(CBezierCurveSurfView)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_ERASEBKGND()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CBezierCurveSurfView construction/destruction
CBezierCurveSurfView::CBezierCurveSurfView()
{
// TODO: add construction code here
DotNo = 0;
m=0;n=0;
}
CBezierCurveSurfView::~CBezierCurveSurfView()
{
}
BOOL CBezierCurveSurfView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CBezierCurveSurfView drawing
void CBezierCurveSurfView::OnDraw(CDC* pDC)
{
CBezierCurveSurfDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if (DotNo==16)
{
CBezierPoint Bezier[2];
CPen* penBlue;
penBlue = new CPen(PS_SOLID, 1, RGB(0, 0, 255));
CPen* oldPen;
oldPen = pDC->SelectObject( penBlue );
for(int i=0; i<=25; i++){
for(int j=0; j<=50; j++){
Bezier[1]=BezierPoint( BezierPnt, 3, 3, 0.04*i, 0.02*j );
if ( j>=1 ){
pDC->MoveTo(Bezier[0].x, Bezier[0].y);
pDC->LineTo(Bezier[1].x, Bezier[1].y);
}
Bezier[0]=BezierPoint( BezierPnt, 3, 3, 0.04*i, 0.02*j );
}
}
for(int j=0; j<=25; j++){
for(int i=0; i<=50; i++){
Bezier[1]=BezierPoint( BezierPnt, 3, 3, 0.02*i, 0.04*j );
if ( i>=1 ){
pDC->MoveTo(Bezier[0].x, Bezier[0].y);
pDC->LineTo(Bezier[1].x, Bezier[1].y);
}
Bezier[0]=BezierPoint( BezierPnt, 3, 3, 0.02*i, 0.04*j );
}
}
delete penBlue;
pDC->SelectObject(oldPen);
}
}
/////////////////////////////////////////////////////////////////////////////
// CBezierCurveSurfView printing
BOOL CBezierCurveSurfView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CBezierCurveSurfView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CBezierCurveSurfView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CBezierCurveSurfView diagnostics
#ifdef _DEBUG
void CBezierCurveSurfView::AssertValid() const
{
CView::AssertValid();
}
void CBezierCurveSurfView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CBezierCurveSurfDoc* CBezierCurveSurfView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBezierCurveSurfDoc)));
return (CBezierCurveSurfDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CBezierCurveSurfView message handlers
void CBezierCurveSurfView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMainFrame *pWnd=(CMainFrame*)AfxGetMainWnd();
pWnd->m_wndStatusBar.SetPaneInfo(2, ID_SEPARATOR, SBPS_NORMAL, 150);
CString StrInfo;
StrInfo.Format("鼠标当前的位置:x=%002d: y=%002d", point.x, point.y);
pWnd->m_wndStatusBar.SetPaneText(2, StrInfo);
if (Index >= 0 && Index <= 15){
BezierPnt[Index].x = point.x;
BezierPnt[Index].y = point.y;
Invalidate();
}
CView::OnMouseMove(nFlags, point);
}
void CBezierCurveSurfView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC* pDC=new CClientDC(this);
//////////////////////////调试使用
CString a;
a.Format("%d,%d",m, n);
AfxMessageBox(a);
////////////////////////////////////////
if ( DotNo <m*n ){
BezierPnt[DotNo].x=point.x;
BezierPnt[DotNo].y=point.y;
if (DotNo>=1 && DotNo<=3){
pDC->MoveTo(BezierPnt[DotNo].x, BezierPnt[DotNo].y);
pDC->LineTo(BezierPnt[DotNo- 1].x, BezierPnt[DotNo- 1].y);
}
if (DotNo==4 || DotNo==8 || DotNo==12){
pDC->MoveTo(BezierPnt[DotNo-4].x, BezierPnt[DotNo-4].y);
pDC->LineTo(BezierPnt[DotNo].x, BezierPnt[DotNo].y);
}
if ((DotNo>4 && DotNo<8) || (DotNo>8 && DotNo<12) || (DotNo>12 && DotNo<16)){
pDC->MoveTo(BezierPnt[DotNo].x, BezierPnt[DotNo].y);
pDC->LineTo(BezierPnt[DotNo- 1].x, BezierPnt[DotNo- 1].y);
pDC->MoveTo(BezierPnt[DotNo-4].x, BezierPnt[DotNo-4].y);
pDC->LineTo(BezierPnt[DotNo].x, BezierPnt[DotNo].y);
}
DotNo++;
}
if (DotNo == (16))
Invalidate();
delete pDC;
CView::OnLButtonDown(nFlags, point);
}
CBezierPoint CBezierCurveSurfView::BezierPoint(POINT B[], int n, int m,
double u, double w)
{
POINT b[100][100];
CBezierPoint s;
s.x = 0;
s.y = 0;
for(int k=0;k<=n;k++){
for(int j=0;j<=m;j++){
b[k][j].x=B[4*k+j].x;
b[k][j].y=B[4*k+j].y;
}
}
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
s.x +=Bernstein(n, i, u)*Bernstein(m, j, w)*b[i][j].x;
s.y +=Bernstein(n, i, u)*Bernstein(m, j, w)*b[i][j].y;
}
}
return s;
}
int CBezierCurveSurfView::Factorial(int n)
{
if ( n==0 )
return 1;
else
return Factorial( n-1 )*n;
}
int CBezierCurveSurfView::Binomial(int n, int m)
{
if ( n>=0 && m>=0 && n>=m )
return Factorial( n )/(Factorial( m )*Factorial( n-m ));
else
exit (1);
}
double CBezierCurveSurfView::Bernstein(int n, int i, double t)
{
if ( n>=i && t>=0 && t<=1 )
return Binomial( n, i )*pow( t, i )*pow( (1-t), (n-i) );
else
exit(1);
}
void CBezierCurveSurfView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC* pDC=new CClientDC(this);
CPen* penRed;
penRed = new CPen(PS_SOLID, 1, RGB(255, 0, 0));
CPen* oldPen;
oldPen = pDC->SelectObject( penRed );
for(int i=0; i<16; i++){
pDC->Ellipse(BezierPnt[i].x- 2, BezierPnt[i].y- 2,
BezierPnt[i].x+2, BezierPnt[i].y+2);
if (i>=1 && i<=3){
pDC->MoveTo(BezierPnt[i- 1].x, BezierPnt[i- 1].y);
pDC->LineTo(BezierPnt[i].x, BezierPnt[i].y);
}
if (i==4 || i==8 || i==12){
pDC->MoveTo(BezierPnt[i].x, BezierPnt[i].y);
pDC->LineTo(BezierPnt[i-4].x, BezierPnt[i-4].y);
}
if ((i>4 && i<8) || (i>8 && i<12) || (i>12 && i<16)){
pDC->MoveTo(BezierPnt[i- 1].x, BezierPnt[i- 1].y);
pDC->LineTo(BezierPnt[i].x, BezierPnt[i].y);
pDC->MoveTo(BezierPnt[i].x, BezierPnt[i].y);
pDC->LineTo(BezierPnt[i-4].x, BezierPnt[i-4].y);
}
}
delete penRed;
pDC->SelectObject(oldPen);
DotNo=0;
CView::OnRButtonDown(nFlags, point);
}
BOOL CBezierCurveSurfView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CBrush MyBrush;
MyBrush.CreateSolidBrush(RGB(225,255,225));
CBrush *pOldBrush=pDC->SelectObject(&MyBrush);
CRect Rect;
GetClientRect(&Rect);
pDC->Rectangle(&Rect);
pDC->SelectObject(pOldBrush);
return TRUE;
return CView::OnEraseBkgnd(pDC);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -