Click here to Skip to main content
13,864,513 members
Click here to Skip to main content
Add your own
alternative version


68 bookmarked
Posted 9 Jan 2002

A C++ implementation of an improved contour plotting algorithm

, 30 Jul 2002
Rate this:
Please Sign up or sign in to vote.
This class generates isocurves of a user defined function. Curves are drawn to OpenGL dc or stored in line strips.

Sample Image - contour.jpg


This article presents  a contour plot class. It is designed to draw iso-contour of a user-defined function f(x,y). I wrote it to integrate it in a graphic library : Plot Graphic Library

The class is based on the algorithm presented in [1] (Check References section). It is basically an improved version of the Level Curve Tracing Algorithm.

Understanding the algorithm

The algorithm uses several tuning parameter that the user must choose in order to have the best quality/performance ratio of the algorithm :

  • Domain of x,y:
     // Setting domain x=[0,1], y=[2,3]
    double pLimits[4]={0,1,2,3,4};
    CContour contour;
  • Size of the primary grid : The grid on which evaluate the function f(x,y). See SetFirGrid, GetColFir, GetRowFir. The parameter influence greatly the quality of the contour.
  • Size of the secondary grid : The grid where the function is going to be evaluated. This grid can be much finer that the first grid. See SetSecGrid, GetColSec, GetRowSec.



Main contour class. This class cannot be directly but has to be inherited. The inherited class must implement the ExportLine function. To generate contours, use

void Generate()

Make to have set the field function (f(x,y)) before calling this function. The function will call ExportLine for each new segment.


Use this class to draw contours to an OpenGL device context.


Use this class to generate contour and store them as line strip. The user can retrieve each contour and use it as he wills. This function uses to sub-classes :

  • CLineStrip, a list < int > containing the index of the points.
  • CLineStripList, a list <CLineStrip*>.

The line strip can be accessed by

CLineStripList* GetList(iPane);

where iPane is the index of the contour.

How to...

Set up a contour object

Suppose that we have inherited a class from CContour and overriden ExportLine function.

class CMyContour : CContour
    void ExportLine(...);

Now, first set the function f(x,y):

double  myF(double x, double y)
{ [...] return ... };

CMyContour contour;
// Setting f(x,y)=myF

Then set the iso-contour values, i.e.

int n;
CMyContour contour;
vector<double> vIso(n);
for (int i=0;i<n;i++)
{ ... }
// setting iso-lines

The contour is ready to be used.

Draw contours using OpenGL

<p.>Use CGLContour as inherited function of CContour.

CGLContour contour;
// Setting up contour : setting f, domain of x,
// isocurve values
// generating contour

Retrieve contours in line strip

Use CListContour as inherited function of CContour. Only the index of the points with respect to second grid are stored in the list. You can access their real value by using GetXi() and GetYi() functions.

CGLContour contour;
// Setting up contour : setting f,
// domain of x, isocurve values
// generating contour
// Retreiving info
CLineStripList* pStripList;
// getting 0-th iso-curve
// iterating liststrip vertices
CLineStrip::iterator pos;
for (pos=pStripList->begin();
    pos != pStripList->end() ; pos++)
    if (pStrip->empty())
    // using info of strip list
    // pStrip contains the succesive index of the points
    // See CContourGLDoc.OnDraw for further details


  • August, 31, 2002 Added contribution from Chenggang Zhou: better strip compression, threshold merging, area of strip, boundary detection, Also some minor changes I don't remember...
  • March, 4, 2002 All the code is now using STL. :)



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


About the Author

Jonathan de Halleux
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 (

You may also be interested in...

Comments and Discussions

GeneralWon't run in VC 2005 or VC 2008 [modified] Pin
Michael B Pliam2-Mar-10 7:16
memberMichael B Pliam2-Mar-10 7:16 
GeneralRe: Won't run in VC 2005 or VC 2008 [modified] Pin
michael_asher10-May-10 22:45
membermichael_asher10-May-10 22:45 
GeneralRe: Won't run in VC 2005 or VC 2008 [modified] Pin
Matthew_Z13-Aug-14 9:33
memberMatthew_Z13-Aug-14 9:33 
GeneralRe: Won't run in VC 2005 or VC 2008 [modified] Pin
Member 429892922-Oct-14 17:58
memberMember 429892922-Oct-14 17:58 
QuestionWhy I can not download the demo? Pin
bigsun690910-Jan-08 17:21
memberbigsun690910-Jan-08 17:21 
GeneralHi,I have a contour ocx here.Would you want to try it?Thank you. Pin
snowedforest3-Feb-07 20:49
membersnowedforest3-Feb-07 20:49 
GeneralBitmap Pin
Sangeetha_J25-Jan-07 21:04
memberSangeetha_J25-Jan-07 21:04 
GeneralRe: Bitmap Pin
Sangeetha_J25-Jan-07 21:09
memberSangeetha_J25-Jan-07 21:09 
GeneralImprovement Pin
8-Feb-05 0:37
suss8-Feb-05 0:37 
GeneralDemo program crashes on redraw Pin
bdiscoe18-Aug-04 10:56
memberbdiscoe18-Aug-04 10:56 
Generalplotting Contour In an image Pin
Jobaida Begum6-Apr-04 8:45
memberJobaida Begum6-Apr-04 8:45 
Generalfeature line extraction from contours Pin
sanjayrana10-Jun-03 6:21
membersanjayrana10-Jun-03 6:21 
GeneralImage Processing Pin
prit13-Apr-03 9:50
memberprit13-Apr-03 9:50 
GeneralRe: Image Processing Pin
Chris Losinger13-Apr-03 10:11
professionalChris Losinger13-Apr-03 10:11 
GeneralRe: Image Processing Pin
Jonathan de Halleux13-Apr-03 22:12
memberJonathan de Halleux13-Apr-03 22:12 
GeneralHelp! Pin
Wang_lei5-Sep-02 23:10
memberWang_lei5-Sep-02 23:10 
GeneralNope Pin
Jonathan de Halleux15-Oct-02 22:15
memberJonathan de Halleux15-Oct-02 22:15 
QuestionRe: Nope Pin
bigsun690910-Jan-08 17:19
memberbigsun690910-Jan-08 17:19 
GeneralRe: Help! Pin
Tigga_123-Feb-07 2:12
memberTigga_123-Feb-07 2:12 
GeneralImprovement Pin
22-Jul-02 21:24
suss22-Jul-02 21:24 
GeneralDid you do it ? Pin
peliikhan23-Jul-02 4:20
susspeliikhan23-Jul-02 4:20 
GeneralRe: Did you do it ? Pin
C.G. Zhou25-Jul-02 9:29
memberC.G. Zhou25-Jul-02 9:29 
GeneralUpdated Pin
Jonathan de Halleux31-Jul-02 2:10
memberJonathan de Halleux31-Jul-02 2:10 
QuestionHow to fill the contour with color? Pin
wangchi17-Jul-02 21:04
memberwangchi17-Jul-02 21:04 
AnswerRe: How to fill the contour with color? Pin
Jonathan de Halleux17-Jul-02 23:13
memberJonathan de Halleux17-Jul-02 23:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 31 Jul 2002
Article Copyright 2002 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid