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

📄 linesmoothview.cpp

📁 使用插值算法,实现折线的平滑.这是一个比较简单的算法.
💻 CPP
字号:
// LineSmoothView.cpp : implementation of the CLineSmoothView class
//

#include "stdafx.h"
#include "LineSmooth.h"
#include "math.h"

#include "LineSmoothDoc.h"
#include "LineSmoothView.h"

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

const double PI = 3.1415927;

/////////////////////////////////////////////////////////////////////////////
// CLineSmoothView

IMPLEMENT_DYNCREATE(CLineSmoothView, CView)

BEGIN_MESSAGE_MAP(CLineSmoothView, CView)
//{{AFX_MSG_MAP(CLineSmoothView)
// NOTE - the ClassWizard will add and remove mapping macros here.
//    DO NOT EDIT what you see in these blocks of generated code!
//}}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()

/////////////////////////////////////////////////////////////////////////////
// CLineSmoothView construction/destruction

CLineSmoothView::CLineSmoothView()
{
	// TODO: add construction code here
	float  n = 10.0;
	CPoint pt;

	for(int i = 0; i <= n; i++)
	{
		pt.x = 200 + int(100 * cos(i / n * 2 * PI));
		pt.y = 200 + int(100 * sin(i / n * 2 * PI));
		m_aryPt.Add(pt);
	}

}

CLineSmoothView::~CLineSmoothView()
{
	m_aryPt.RemoveAll();
}

BOOL CLineSmoothView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CLineSmoothView drawing

void CLineSmoothView::OnDraw(CDC* pDC)
{
	CLineSmoothDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	
	CPoint pt;
	for(int i = 0; i < m_aryPt.GetSize(); i++)
	{
		if(i == 0)
			pDC->MoveTo(m_aryPt.GetAt(i));
		else
			pDC->LineTo(m_aryPt.GetAt(i));
	}

	LineMooth(m_aryPt,TRUE);

	for(i = 0; i < m_aryPt.GetSize(); i++)
	{
		pt = m_aryPt.GetAt(i);
		pt.Offset(250,0);
		if(i == 0)
			pDC->MoveTo(pt);
		else
			pDC->LineTo(pt);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CLineSmoothView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CLineSmoothView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CLineSmoothView message handlers


void LineMooth(CArray<CPoint,CPoint&> &aryPt,BOOL bClose)
{
	int         i = 0,num = 1;
	CPoint      pt[4];
	CArray<CPoint,CPoint&> p;
	
	if(aryPt.GetSize() < 3)
		return;

	p.Copy(aryPt);
	
	if(p.GetSize() == 3)
	{
		CPoint tmp;
		
		if(abs(p.GetAt(1).x - p.GetAt(2).x) < abs(p.GetAt(1).y - p.GetAt(2).y))
		{
			tmp.x = p.GetAt(1).x;
			tmp.y = int((p.GetAt(2).y + p.GetAt(1).y) * 0.5);
		}
		else
		{
			tmp.x = int((p.GetAt(2).x + p.GetAt(1).x) * 0.5);
			tmp.y = p.GetAt(1).y;
		}
		
		p.Add(tmp);
	}
	
	aryPt.RemoveAll();
	if(!bClose) //如果开
	{
		i = 0;
		aryPt.Add(p.GetAt(0));
		while (i < p.GetSize()-4) {
			pt[0] = p[i];
			pt[1] = p[i+1];
			pt[2] = p[i+2];
			pt[3] = p[i+3];
			InsertPoint(pt,aryPt,num);
			i++;
		}
		aryPt.Add(p.GetAt(p.GetSize()-1));
	}
	else
	{
		i = 0;
		while (i < p.GetSize()-1) {
			if(i == p.GetSize()-2)
			{
				pt[0] = p[i];
				pt[1] = p[0];
				pt[2] = p[1];
				pt[3] = p[2];
				InsertPoint(pt,aryPt,num);
			}
			else if(i == p.GetSize()-3)
			{
				pt[0] = p[i];
				pt[1] = p[i+1];
				pt[2] = p[0];
				pt[3] = p[1];
				InsertPoint(pt,aryPt,num);
			}
			else if(i == p.GetSize()-4)
			{
				pt[0] = p[i];
				pt[1] = p[i+1];
				pt[2] = p[i+2];
				pt[3] = p[0];
				InsertPoint(pt,aryPt,num);
			}
			else
			{
				pt[0] = p[i];
				pt[1] = p[i+1];
				pt[2] = p[i+2];
				pt[3] = p[i+3];
				InsertPoint(pt,aryPt,num);
			}
			i++;
		}
	}
}

void InsertPoint(CPoint pt[4], CArray<CPoint,CPoint&> &aryPt,int num)
{
	int      N;
	CPoint   temp;
	double   A[4],B[4],t,t2,t3,d;
	
	d = sqrt(pow(pt[1].x-pt[0].y, 2) + pow(pt[1].x-pt[0].x, 2));
	N = int(d * 3 + 1) * num;
	
	A[0] =  (pt[0].x+4.0*pt[1].x+pt[2].x)/6.0;              
	A[1] = -(pt[0].x-pt[2].x)/2.0;
	A[2] =  (pt[0].x-2.0*pt[1].x+pt[2].x)/2.0;
	A[3] = -(pt[0].x-3.0*pt[1].x+3.0*pt[2].x-pt[3].x)/6.0;
	B[0] =  (pt[0].y+4.0*pt[1].y+pt[2].y)/6.0;
	B[1] = -(pt[0].y-pt[2].y)/2.0;
	B[2] =  (pt[0].y-2.0*pt[1].y+pt[2].y)/2.0;
	B[3] = -(pt[0].y-3.0*pt[1].y+3.0*pt[2].y-pt[3].y)/6.0;  

	for(int i = 0; i <= N; i++)
	{
		t  = float(i/(float)(N+1));
		t2 = t*t;
		t3 = t2*t;	
		temp.x = long(A[0]+A[1]*t+A[2]*t2+A[3]*t3);
		temp.y = long(B[0]+B[1]*t+B[2]*t2+B[3]*t3);
		aryPt.Add(temp);
	} 
} 

⌨️ 快捷键说明

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