code for isoline(C)
Well, Thanks to all you folks who replied, I now have a contour plotting
routine, that does its job fairly well. I've posted this code below. It
is not nearly runnable as other Unix programs go, but you could use it
if you need C code to start off with to do contour plotting.
It doesn't handle stuff like labelling etc. which is somewhat tricky.
Basically almost all of the respondents suggested dividing up the set of
discrete points into a set of triangles and then computing the intersection
of these rectangles with planes representing the contour levels. This way
we can process all the levels at once instead of the "crawler" type algorithms
which is what I had originally been trying to get to work.
To summarize their ideas: if P1=P[j],P2=P[i+1][j],P3=P[i+1][j+1]
and P4=P[j+1] are four points, compute an intermediate point using
an interpolation scheme (in the code below, I just use averaging, but
one could obviously use fancier schemes). If this point is P, then the
foll. four triangles are used to compute the intersections with
contour levels l_0, .. l_n: P1 P2 P, P2 P3 P, P3 P4 P, P4 P1 P.
The intersection computation is quite trivial. Once these line segments have been
computed you could smooth them in 2d (the code below doesn't).
(see Michael Aramini's earlier posting for a variation on this that uses
rectangles directly -- He also mentions that you could use the diagonal edges
between two vertices instead of computing another intermediate point, but this
method has problems).
-Sundar
-----Sample Code Follows----------
/*
* If you want try using this, you probably would need to provide
* graph_set_point(), and graph_draw_line(), besides do things like
* initialize window systems and so on.
*
* Please see the code for a note about data format
*/
/* Definitions */
struct _array {
float *a_elements;
int a_rows;
int a_cols;
float *a_filler;
};
#define ELTS(a) (a->a_elements)
#define NOROWS(a) (a->a_rows)
#define NOCOLS(a) (a->a_cols)
#define NOELTS(a) (NOROWS(a) * NOCOLS(a))
#define COLUMN_VECTOR(a) (NOCOLS((a)) == 1)
#define ROW_VECTOR(a) (NOROWS((a)) == 1)
typedef struct _array *array;
typedef struct _graph *graph;
typedef struct _graph_port *graph_port;