📄 三次样条曲线view.cpp
字号:
#include "stdafx.h"
#include "三次样条曲线.h"
#include "math.h"
#include "三次样条曲线Doc.h"
#include "三次样条曲线View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_Sanciyangtiao, OnSanciyangtiao)
ON_COMMAND(ID_222, On222)
ON_COMMAND(ID_333, On333)
//}}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)
ON_COMMAND(ID_Sanci, OnSanci)
END_MESSAGE_MAP()
#include "math.h"
#include "三次样条曲线view.h"
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
CMyView::CMyView()
{m_type=0;
dot_numbers=0;
// TODO: add construction code here
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMyView printing
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{ // TODO: Add your message handler code here and/or call default
if(m_type==3){
dot_numbers++;
switch(dot_numbers){
case 1:
x[0]=point.x;
y[0]=point.y;
break;
case 2:
x[1]=point.x;
y[1]=point.y;
break;
case 3:
x[2]=point.x;
y[2]=point.y;
break;
case 4:
x[3]=point.x;
y[3]=point.y;
byangtiao() ;
m_type=0;
dot_numbers=0;
break;
default:
;
}
}
if(m_type==2){
dot_numbers++;
switch(dot_numbers){
case 1:
x[0]=point.x;
y[0]=point.y;
break;
case 2:
x[1]=point.x;
y[1]=point.y;
break;
case 3:
x[2]=point.x;
y[2]=point.y;
break;
case 4:
x[3]=point.x;
y[3]=point.y;
besizer() ;
m_type=0;
dot_numbers=0;
break;
default:
;
}}
if(m_type==1){
dot_numbers++;
switch(dot_numbers){
case 1:
x[0]=point.x;
y[0]=point.y;
break;
case 2:
x[1]=point.x;
y[1]=point.y;
break;
case 3:
x[2]=point.x;
y[2]=point.y;
break;
case 4:
x[3]=point.x;
y[3]=point.y;
break;
case 5:
x[4]=point.x;
y[4]=point.y;
Sanciyangtiao() ;
m_type=0;
dot_numbers=0;
break;
default:
;
}}
CView::OnLButtonDown(nFlags, point);
}
void CMyView::Sanciyangtiao()
{CDC *pDC=GetDC();
float a[100],b[100],c[100];
float px[100],py[100],qx[100],qy[100],tt[100];
float dx[100],dy[100];
int i,n=4,t,es=3;
float bx3,bx4=0.0,by3=0.0,by4,cx,cy;
for(i=0;i<=n;i++)
{ a[i]=0.0; b[i]=0.0; c[i]=0.0; px[i]=0.0; py[i]=0.0;
dx[i]=0.0; dy[i]=0.0; tt[i]=0.0; qx[i]=0.0; qy[i]=0.0; }
px[0]=1.0; py[0]=1.0;
px[4]=1.0; py[4]=1.0;
pDC->MoveTo(x[0],y[0]);
for(i=0;i<=n;i++) pDC->SetPixel(x[i],y[i],15);
pDC->SetPixel(x[0],y[0],15);
for(i=1;i<=n;i++)
tt[i]=sqrt((x[i]-x[i-1])*(x[i]-x[i-1])+(y[i]-y[i-1])*(y[i]-y[i-1]));
if (n==1) goto p0;
for(i=1;i<=n-1;i++)
{
a[i]=2*(tt[i]+tt[i+1]);
b[i]=tt[i+1];
c[i]=tt[i];
dx[i]=3*(tt[i]*(x[i+1]-x[i])/tt[i+1]+tt[i+1]*(x[i]-x[i-1])/tt[i]);
dy[i]=3*(tt[i]*(y[i+1]-y[i])/tt[i+1]+tt[i+1]*(y[i]-y[i-1])/tt[i]);
}
dx[1]=dx[1]-tt[2]*px[0]; dx[n-1]=dx[n-1]-tt[n-1]*px[n];
dy[1]=dy[1]-tt[2]*py[0]; dy[n-1]=dy[n-1]-tt[n-1]*py[n];
if (n==2) {px[1]=dx[1]/a[1];py[1]=dy[1]/a[1]; goto p0;}
c[1]=c[1]/a[1];
for(i=2;i<=n-1;i++)
{ a[i]=a[i]-b[i]*c[i-1]; c[i]=c[i]/a[i];}
qx[1]=dx[1]/a[1]; qy[1]=dy[1]/a[1];
for(i=2;i<=n-1;i++)
{ qx[i]=(dx[i]-b[i]*qx[i-1])/a[i];
qy[i]=(dy[i]-b[i]*qy[i-1])/a[i];
}
px[n-1]=qx[n-1]; py[n-1]=qy[n-1];
for(i=n-2;i>=1;i--)
{ px[i]=qx[i]-c[i]*px[i+1];
py[i]=qy[i]-c[i]*py[i+1];
}
p0:
for(i=0;i<=n-1;i++)
{ bx3=(3*(x[i+1]-x[i])/tt[i+1]-2*px[i]-px[i+1])/tt[i+1];
bx4=((2*(x[i]-x[i+1])/tt[i+1]+px[i]+px[i+1])/tt[i+1])/tt[i+1];
by3=(3*(y[i+1]-y[i])/tt[i+1]-2*py[i]-py[i+1])/tt[i+1];
by4=((2*(y[i]-y[i+1])/tt[i+1]+py[i]+py[i+1])/tt[i+1])/tt[i+1];
t=0;
do {
t=t+es;
cx=x[i]+(px[i]+(bx3+bx4*t)*t)*t;
cy=y[i]+(py[i]+(by3+by4*t)*t)*t;
pDC->LineTo(cx,cy);
} while(t<tt[i+1]);
}
}
void CMyView::OnSanciyangtiao()
{
// TODO: Add your command handler code here
m_type=1;
}
void CMyView::OnSanci(void)
{
// TODO: Add your command handler code here
}
void CMyView::OnB(void)
{
// TODO: Add your command handler code here
}
void CMyView::On333()
{m_type=3;
dot_numbers=0;
}
void CMyView::besizer()
{CClientDC *pdc=new CClientDC(this);
CPen pen;
pen.CreatePen(PS_SOLID,1,RGB(0,0XFF,0));
CPen *oldpen=(CPen*)pdc->SelectObject(&pen);
int cx,cy,i,n;
n=9;
double t,t2,t3,a0,a1,a2,a3,b0,b1,b2,b3,dt,xa,ya;
a0=x[0];
a1=-3*(x[0]-x[1]);
a2=3*x[0]-6*x[1]+3*x[2];
a3=-x[0]+3*x[1]-3*x[2]+x[3];
b0=y[0];
b1=-3*(y[0]-y[1]);
b2=3*y[0]-6*y[1]+3*y[2];
b3=-y[0]+3*y[1]-3*y[2]+y[3];
dt=1.0/n;
for (i=0;i<=n;i++){
t=i*dt;
t2=t*t;
t3=t2*t;
xa=a0+a1*t+a2*t2+a3*t3;
ya=(b0+b1*t+b2*t2+b3*t3);
if(i==0) { pdc->MoveTo(xa,ya);}
else pdc->LineTo(xa,ya);
}
pdc->DeleteDC();
}
void CMyView::On222()
{m_type=2;
dot_numbers=0;
}
void CMyView::byangtiao()
{ CClientDC *pdc=new CClientDC(this);
CPen pen;
pen.CreatePen(PS_SOLID,1,RGB(0,0XFF,0));
CPen *oldpen=(CPen*)pdc->SelectObject(&pen);
int cx,cy,i,n;
n=9;
double t,t2,t3,a0,a1,a2,a3,b0,b1,b2,b3,dt,xa,ya;
a0=(x[0]+4*x[1]+x[2])/6;
a1=-(x[0]-x[2])/2;
a2=(x[2]-2*x[1]+x[0])/2;
a3=-(x[0]-3*x[1]+3*x[2]-x[3])/6;
b0=(y[0]+4*y[1]+y[2])/6;
b1=-(y[0]-y[2])/2;
b2=(y[2]-2*y[1]+y[0])/2;
b3=-(y[0]-3*y[1]+3*y[2]-y[3])/6;
dt=1.0/n;
for (i=0;i<=n;i++){
t=i*dt;
t2=t*t;
t3=t2*t;
xa=a0+a1*t+a2*t2+a3*t3;
ya=b0+b1*t+b2*t2+b3*t3;
if(i==0) pdc->MoveTo(xa,ya);
else pdc->LineTo(xa,ya);
}
pdc->DeleteDC();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -