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.
// GLDouglasView.cpp : implementation of the CGLDouglasView class
//

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

#include "GLDouglasDoc.h"
#include "GLDouglasView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGLDouglasView

IMPLEMENT_DYNCREATE(CGLDouglasView, CView)

BEGIN_MESSAGE_MAP(CGLDouglasView, CView)
	//{{AFX_MSG_MAP(CGLDouglasView)
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGLDouglasView construction/destruction

CGLDouglasView::CGLDouglasView()
{
	// TODO: add construction code here

}

CGLDouglasView::~CGLDouglasView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGLDouglasView drawing

void CGLDouglasView::OnDraw(CDC* pDC)
{
	CGLDouglasDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	double dLimits[4];
	CString str;

	pDoc->GetLimits(dLimits);
	m_view.SetLimits(dLimits);
	m_view.PostReshape();

	m_wgl.Begin(pDC);
		// setting view
		m_view.Reshape();	

		// clearing buffer
		glClearColor(1.0, 1.0, 1.0, 0.0);
 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		// drawing line
		pDoc->Draw(m_wgl);

	m_wgl.End();

	// Swap buffers.
	SwapBuffers(pDC->m_hDC) ;
}

/////////////////////////////////////////////////////////////////////////////
// CGLDouglasView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGLDouglasView message handlers

BOOL CGLDouglasView::OnEraseBkgnd(CDC* pDC) 
{
	return TRUE;
}

void CGLDouglasView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);

	m_view.SetViewport(0,0,cx,cy);

	m_view.PostReshape();	
}

void CGLDouglasView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();

	m_view.GetState()->SetLineWidth(2);
	
	((CGLDouglasApp*)AfxGetApp())->SetView(this);
}

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 | Mobile
Web04 | 2.8.141022.2 | Last Updated 4 Mar 2003
Article Copyright 2002 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid