Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A C++ implementation of Douglas-Peucker Line Approximation Algorithm

, 3 Mar 2003
DP Line approximation algorithm is a well-known method to approximate 2D lines. It is quite fast, O(nlog_2(n)) for a n-points line and can drastically compress a data curve. Here, a fully OOP implementation is given.
// GLDouglasDoc.cpp : implementation of the CGLDouglasDoc class
//

#include "stdafx.h"
#include "GLDouglas.h"

#include "GLDouglasDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGLDouglasDoc

IMPLEMENT_DYNCREATE(CGLDouglasDoc, CDocument)

BEGIN_MESSAGE_MAP(CGLDouglasDoc, CDocument)
	//{{AFX_MSG_MAP(CGLDouglasDoc)
		// 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
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGLDouglasDoc construction/destruction

CGLDouglasDoc::CGLDouglasDoc()
{
}

CGLDouglasDoc::~CGLDouglasDoc()
{
}

BOOL CGLDouglasDoc::OnNewDocument()
{
	using namespace hull;
	UINT n=500;

	if (!CDocument::OnNewDocument())
		return FALSE;

	srand( (unsigned)time( NULL ) );

	m_dShrink=0.1;
	m_dp.ResizePoints(n);

	// generate datas
	m_lCount=0;
	Animate();

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CGLDouglasDoc serialization

void CGLDouglasDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CGLDouglasDoc diagnostics

#ifdef _DEBUG
void CGLDouglasDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CGLDouglasDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGLDouglasDoc commands

void CGLDouglasDoc::Draw(CWGL& wgl)
{
	UINT nIter;
	CString str;
	CGLDouglasApp* pApp=(CGLDouglasApp*)AfxGetApp();

	if (pApp->GetView() != NULL)
		Animate();

	try
	{
		nIter=m_dp.ShrinkNorm(m_dShrink, 0);
	}
	catch(TCHAR* str)
	{
		AfxMessageBox(str);
		pApp->SetView(NULL);
	}


	glColor3f(0,0,0);
	glLineWidth(2);
	m_dp.PlotPoints();

	glColor3f(1,0,0);
	m_dp.PlotKeys(wgl);
	
	glColor3f(0,0,0);
	str.Format(_T("Douglas-Peucker Demo.... Shrink: %g%%, points: %d, keys: %d, tol = %g (Achieved in %d iterations)"),
			m_dShrink*100,
			m_dp.GetPointSize(),
			m_dp.GetKeySize(),
			m_dp.GetTol(),
			nIter);

	AfxGetMainWnd()->SetWindowText(str);
}

void CGLDouglasDoc::Animate()
{
	using namespace hull;

	size_t i;
	m_lCount%=10000;

	size_t n=m_dp.GetPointSize();

	for (i = 0; i < n; i++)
	{
		m_dp.SetPoint(i,i,
			n / 2.0 * (
			sin((2*3.14 * i) / n + m_lCount/100.0)
			*cos((3.14 * i) / n + m_lCount/1000.0)*cos((3.14 * i) / n+ m_lCount/1000.0)
			+sin((8*3.14 * i) / n + m_lCount/5.0)/3.0) 
			);

	}	
	m_lCount++;
}

void CGLDouglasDoc::GetLimits(double pExt[])
{
	CDPHullGL::Hull::SLimits l=m_dp.GetHull().GetBoundingBox();

	pExt[0]=l.dMinX;
	pExt[1]=l.dMaxX;
	pExt[2]=l.dMinY;
	pExt[3]=l.dMaxY;
}

CDPHullGL& CGLDouglasDoc::GetHull()
{
	return m_dp;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jonathan de Halleux
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 4 Mar 2003
Article Copyright 2002 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid