Click here to Skip to main content
15,891,942 members
Articles / Desktop Programming / MFC

A C++ Implementation of an Improved Contour Plotting Algorithm

Rate me:
Please Sign up or sign in to vote.
4.74/5 (13 votes)
30 Jul 20023 min read 327K   12.7K   69  
This class generates isocurves of a user defined function. Curves are drawn to OpenGL dc or stored in line strips.
In this article, you will learn about a contour plot class, which is an improved version of the Level Curve Tracing Algorithm. It was designed to draw iso-contour of a user-defined function f(x,y).
// ContourGLDoc.cpp : implementation of the CContourGLDoc class
//

#include "stdafx.h"
#include "Contour.h"
#include "ContourGL.h"
#include "ContourGLDoc.h"

#ifdef LINE
	#undef LINE
#endif

#define LINE(a,b,c,d) line2(a,b,c,d)

void line2(double x1, double y1, double x2, double y2)
{
	glColor3f(0,0,0);
	glBegin(GL_LINES);
		glVertex2d(x1,y1);
		glVertex2d(x2,y2);
	glEnd();
}

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

/////////////////////////////////////////////////////////////////////////////
// CContourGLDoc

IMPLEMENT_DYNCREATE(CContourGLDoc, CDocument)

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

/////////////////////////////////////////////////////////////////////////////
// CContourGLDoc construction/destruction

CContourGLDoc::CContourGLDoc()
{
	// TODO: add one-time construction code here

}

CContourGLDoc::~CContourGLDoc()
{
}

BOOL CContourGLDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CContourGLDoc serialization

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

/////////////////////////////////////////////////////////////////////////////
// CContourGLDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CContourGLDoc commands

void CContourGLDoc::Draw()
{
	// Plotting strip generated by CLineStripList
	// generating info
	m_listContour.Generate();

	// draw line strips
	CLineStripList* pStripList;
	CLineStrip* pStrip;
	UINT i,index;
	CLineStripList::iterator pos;
	CLineStrip::iterator pos2;
	double pLimits[4];
	double x,y;

	m_listContour.GetLimits(pLimits);
	for (i=0;i<m_listContour.GetNPlanes();i++)
	{
		pStripList=m_listContour.GetLines(i);
		ASSERT(pStripList);
		for (pos=pStripList->begin(); pos != pStripList->end() ; pos++)
		{
			pStrip=(*pos);
			ASSERT(pStrip);
			if (pStrip->empty())
				continue;


			// putting point at start and end of strip
			glBegin(GL_POINTS);
				glColor4f(1,0,0,.8f);
				// retreiving index
				index=pStrip->front();
				// drawing
				x=m_listContour.GetXi(index);
				y=m_listContour.GetYi(index);
				glVertex2f((GLfloat)(pLimits[0]+x),(GLfloat)(pLimits[2]+y));
				// retreiving index
				glColor4f(0,0,1,.8f);
				index=pStrip->back();
				// drawing
				x=m_listContour.GetXi(index);
				y=m_listContour.GetYi(index);
				glVertex2f((GLfloat)(pLimits[0]+x),(GLfloat)(pLimits[2]+y));
			glEnd();

			glColor3f(i/(float)m_listContour.GetNPlanes(),
				i/(float)m_listContour.GetNPlanes(),
				1.f-i/(float)m_listContour.GetNPlanes());

			// filling strip if closed...
			glBegin(GL_LINE_STRIP);
			for (pos2=pStrip->begin(); pos2 != pStrip->end() ; pos2++)
			{
				// retreiving index
				index=(*pos2);
				// drawing
				x=m_listContour.GetXi(index);
				y=m_listContour.GetYi(index);

				glVertex2f((GLfloat)(pLimits[0]+x),(GLfloat)(pLimits[2]+y));
			}
			glEnd();
		}
	}
}

void CContourGLDoc::GetGLLimits(double pLimits[])
{
	// drawing
	m_listContour.GetLimits(pLimits);
}

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.


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

Comments and Discussions