next up previous contents index
Next: Printing out graphs Up: Plotting graphs Previous: Graph Decorations   Contents   Index


Drawing graphs line by line

While the high level CCATSL graphics routines make it easy to draw the graph of a function, or to display a dataset in a standard form, it is sometimes better to take control of the graph drawing process yourself and draw each line or point individually. A sequence of commands to draw a triangle might look like:

  
Set2DPlotCL();           /* Select a 2D plot                 */
XRangeCL(-1.0,1.0);      /* Establish the new x-scale        */
YRangeCL(-1.0,1.0);      /* Establish the new y-scale        */
GAxesCL();               /* Draw the axes                    */
XYMoveCL(-1.0,-1.0);     /* Move to the point (-1, -1)       */
XYDrawCL( 0.0, 1.0);     /* Draw a line to the point ( 0, 1) */
XYDrawCL( 1.0,-1.0);     /* Draw a line to the point ( 1,-1) */
XYDrawCL(-1.0,-1.0);     /* Draw a line to the point (-1,-1) */
To use this approach, before trying to draw anything, you must establish scales for your axes. This can be done with XRangeCL etc. (as in the example), but it also happens automatically when you call one of the high level CCATSL graphics routines with an AxisModeCT of AUTOAXES or RESCALE .

Once the scales have been established, if you have not just called a high-level graphics routine, you will probably first want to draw the axes and axis-labels with GAxesCL. After that you can add points using XYSymbolCL, lines with XYMoveCL and XYDrawCL, polygons with XYPolygonCL and filled polygons with XYPolygonFillCL. The functions XYZSymbolCL, XYZMoveCL, XYZPolygonCL and XYZPolygonFillCL provide similar functionality for three-dimensional graphs.


GAxesCL

This command asks CCATSL to draw and label the axes. You do not normally have to call these routines yourself since CCATSL will draw (or re-draw) and label the axes whenever you use one of the high level graphics routines. You should call either Set2DPlotCL or Set3DPlotCL prior to calling GAxesCL.

  
GAxesCL();   /* draw the axes */


Set2DPlotCL and Set3DPlotCL

These functions tell CCATSL the number of dimension in the next plot.

  
Set2DPlotCL();  /* prepare for a 2D plot */
/* .. */
Set3DPlotCL();  /* prepare for a 3D plot */


XYMoveCL, XYDrawCL, XYZMoveCL and XYZDrawCL

The routines either draw a line from the current graphics cursor to a specified point or move the graphics cursor to a new point. For two-dimensional graphs, the first two routines should be used. For three-dimensional graphs the final routines must be used. The syntax of these commands is straightforward:

  
XYMoveCL(1.0, 1.0)     /* move the graphics cursor to (1.0, 1.0) */
XYDrawCL(1.0,-1.0)     /* draw a line to (1.0, -1.0)             */
for a two-dimensional graph, and

  
XYZMoveCL(1.0, 1.0, 1.0)     /* move the graphics cursor to (1.0, 1.0, 1.0) */
XYZDrawCL(1.0,-1.0, 1.0)     /* draw to (1.0, -1.0, 1.0)                    */
for a three-dimensional graph. For example:


screenshot of drawabs.pas
  
/*   /examples/chapter3/drawabs.c   */
#include <catam.h>

int MainCL(void)
{
  double x,y;
  int change;
  double dx,dy;

  Set2DPlotCL();
  XRangeCL(-300,300);
  YRangeCL(-300,300);
  GAxesCL();
  x=0.0;
  y=0.0;
  XYMoveCL(0.0,0.0);
  while (x*x+y*y<90000) {
    change=RandomIntCL(4);
    dx=0.0;
    dy=0.0;
    switch (change) {
    case 1: dx= 1; break;
    case 2: dx=-1; break;
    case 3: dy= 1; break;
    case 4: dy=-1; break;
    }
    x=x+dx;
    y=y+dy;
    XYDrawCL(x,y);
  }
  return 0;
}


next up previous contents index
Next: Printing out graphs Up: Plotting graphs Previous: Graph Decorations   Contents   Index
CATAM admin 2010-02-23