Click here to Skip to main content
12,500,761 members (47,063 online)
Click here to Skip to main content

Stats

335.4K views
12K downloads
122 bookmarked
Posted

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.
							 /////////////////////////////////////////////////////////////////////////////
// CustomizeDialog.cpp: implementation of the CCustomizeDialog class.
//
/////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2001 by Nikolay Denisov. All rights reserved.
//
// This code is free for personal and commercial use, providing this 
// notice remains intact in the source files and all eventual changes are
// clearly marked with comments.
//
// You must obtain the author's consent before you can include this code
// in a software library.
//
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Please email bug reports, bug fixes, enhancements, requests and
// comments to: nick@actor.ru
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CustomizeDialog.h"
#include "ToolBarEx.h"

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



static const int cxPadding = 3;
static const int cyPadding = 3;

IMPLEMENT_DYNAMIC( CCustomizeDialog, CWnd )

CCustomizeDialog::CCustomizeDialog( CToolBarEx* pToolBar,
                                    TextOptions TextOptions,
                                    IconOptions IconOptions )
{
	ASSERT_VALID(pToolBar);
    m_pToolBar = pToolBar;

	m_eTextOptions=TextOptions;
	m_eIconOptions=IconOptions;

}

/////////////////////////////////////////////////////////////////////////////
// Operations

void CCustomizeDialog::SetTextOptions( TextOptions eTextOptions)
{
    m_eTextOptions = eTextOptions;
	if (IsWindow(m_wndTextOptions.m_hWnd))
		  m_wndTextOptions.SetCurSel(eTextOptions);
    
}

void CCustomizeDialog::SetIconOptions( IconOptions eIconOptions)
{
    m_eIconOptions = eIconOptions;

	if (IsWindow(m_wndIconOptions.m_hWnd))
	{
		  m_wndIconOptions.SetCurSel(eIconOptions);	  
	}
}


/////////////////////////////////////////////////////////////////////////////
// CCustomizeDialog message handlers

BEGIN_MESSAGE_MAP(CCustomizeDialog, CWnd)
    //{{AFX_MSG_MAP(CCustomizeDialog)
	//}}AFX_MSG_MAP
    ON_MESSAGE( WM_INITDIALOG, OnInitDialog )
    ON_CBN_SELENDOK(IDC_CB_TEXTOPTIONS, OnTextOptions)
    ON_CBN_SELENDOK(IDC_CB_ICONOPTIONS, OnIconOptions)
END_MESSAGE_MAP()

LRESULT CCustomizeDialog::OnInitDialog( WPARAM /*wParam*/, LPARAM /*lParam*/ )
{
    CRect rcClient;
    GetClientRect( rcClient );
    CRect rcWindow;
    GetWindowRect( rcWindow );

	// create  the windows	// sizes are hardcoded //ugly way


	CRect  rc;
	CFont *pFont =GetFont();
	ASSERT_VALID(pFont);

	
  
	//static wnd
	rc=CRect(4,2,49,8);
	MapDialogRect(m_hWnd,&rc);
	rc.left+=rcClient.left;
	rc.top+=rcClient.bottom;
	VERIFY(m_wndTextOptionsText.Create(_T("Te&xt options:"),WS_CHILD|WS_VISIBLE,rc,this));
 	m_wndTextOptionsText.SetFont(pFont);
	m_wndTextOptionsText.SetWindowPos(NULL,rc.left,rc.top,rc.right,rc.bottom,SWP_NOZORDER|SWP_NOACTIVATE);

	
	//Text options
	rc=CRect(52,0,123,50);
	MapDialogRect(m_hWnd,&rc);
	rc.left+=rcClient.left;
	rc.top+=rcClient.bottom;
	VERIFY(m_wndTextOptions.Create(WS_CHILD |WS_VISIBLE |CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP,rc,this, IDC_CB_TEXTOPTIONS));
 	m_wndTextOptions.SetFont(pFont);

	m_wndTextOptions.AddString(_T("Show Text Labels"));
	m_wndTextOptions.AddString(_T("Selective Text on Right"));
	m_wndTextOptions.AddString(_T("No Text Labels"));

	SetTextOptions(m_eTextOptions);
	m_wndTextOptions.SetWindowPos(NULL,rc.left,rc.top,rc.right,rc.bottom,SWP_NOZORDER|SWP_NOACTIVATE);

 
	//static wnd
	rc=CRect(4,20,49,8);
	MapDialogRect(m_hWnd,&rc);
	rc.left+=rcClient.left;
	rc.top+=rcClient.bottom;
	VERIFY(m_wndIconOptionsText.Create(_T("Ico&n options:"),WS_CHILD|WS_VISIBLE,rc,this));
	m_wndIconOptionsText.SetFont(pFont);
	m_wndIconOptionsText.SetWindowPos(NULL,rc.left,rc.top,rc.right,rc.bottom,SWP_NOZORDER|SWP_NOACTIVATE);


	//icon combo
	rc=CRect(52,18,123,50);
	MapDialogRect(m_hWnd,&rc);
	rc.left+=rcClient.left;
	rc.top+=rcClient.bottom;
	VERIFY(m_wndIconOptions.Create(WS_CHILD |WS_VISIBLE |CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP,rc,this, IDC_CB_ICONOPTIONS));
 	m_wndIconOptions.SetFont(pFont);

	m_wndIconOptions.AddString(_T("Small Icons"));
	m_wndIconOptions.AddString(_T("Large Icons"));
	
	SetIconOptions(m_eIconOptions);
	m_wndIconOptions.SetWindowPos(NULL,rc.left,rc.top,rc.right,rc.bottom,SWP_NOZORDER|SWP_NOACTIVATE);



	// final rect
	rc=CRect(0,0,179,34);
	MapDialogRect(m_hWnd,&rc);
	rc.left+=rcClient.left;
	rc.top+=rcClient.bottom;
	
 
    // Increase height of Customize Toolbar dialog accordingly
    rcWindow.bottom += (rc.bottom+cyPadding);
    VERIFY( SetWindowPos( 0, 0, 0, rcWindow.Width(), rcWindow.Height(),
        SWP_NOZORDER | SWP_NOMOVE ) );

	//Set Title
	ASSERT(m_pToolBar);
	CString strName,strOrgName;
	m_pToolBar->GetWindowText(strName);

	if (!strName.IsEmpty())
	{
		GetWindowText(strOrgName);
		SetWindowText(strOrgName + _T(" - ")+ strName);
	}

    return Default();

}




void CCustomizeDialog::OnTextOptions()
{
	m_eTextOptions=(TextOptions)m_wndTextOptions.GetCurSel();
	m_pToolBar->SetTextOptions(( TextOptions )m_eTextOptions,TRUE);
}

void CCustomizeDialog::OnIconOptions()
{
	m_eIconOptions=(IconOptions)m_wndIconOptions.GetCurSel();
}




LRESULT CCustomizeDialog::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	if (message==WM_HELP)
	{
	  LPHELPINFO lphi = (LPHELPINFO) lParam; 
	  if ( (lphi->iCtrlId != IDC_CB_TEXTOPTIONS) && (lphi->iCtrlId != IDC_CB_ICONOPTIONS) )
		return DefWindowProc(message,wParam,lParam);
	}
	return CWnd::WindowProc(message, wParam, lParam);
}



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).

You may also be interested in...

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