PREV UP next The Plotutils Package

9.3.3: Sample drawings in C++

In a previous section, there are several sample C programs that show how to draw vector graphics using libplot's C binding. See Sample C Drawings. In this section, we give a modified version of one of the C programs, showing how libplot's C++ binding, i.e., libplotter, can be used similarly.

The following C++ program draws an intricate and beautiful path (Bill Gosper's ``C'' curve).

#include <plotter.h>
#define MAXORDER 12
void draw_c_curve (Plotter& plotter, double dx, double dy, int order)
{
  if (order >= MAXORDER)
    plotter.fcontrel (dx, dy);	// continue path along (dx, dy)
  else
    {
      draw_c_curve (plotter, 
                    0.5 * (dx - dy), 0.5 * (dx + dy), order + 1);
      draw_c_curve (plotter,
                    0.5 * (dx + dy), 0.5 * (dy - dx), order + 1);
    }
}
int main ()
{
  // set a Plotter parameter
  Plotter::parampl ("PAGESIZE", "letter");
  PSPlotter plotter(cin, cout, cerr); // declare Plotter
  if (plotter.openpl () < 0)          // open Plotter
    {
      cerr << "Couldn't open Plotter\n";
      return 1;
    }

plotter.fspace (0.0, 0.0, 1000.0, 1000.0); // specify user coor system plotter.flinewidth (0.25); // line thickness in user coordinates plotter.pencolorname ("red"); // path will be drawn in red plotter.erase (); // erase Plotter's graphics display plotter.fmove (600.0, 300.0); // position the graphics cursor draw_c_curve (plotter, 0.0, 400.0, 0);

if (plotter.closepl () < 0) // close Plotter { cerr << "Couldn't close Plotter\n"; return 1; } return 0; }

The above is a straightforward translation of the corresponding C program. Here, plotter is declared as an instance of the PSPlotter class, which will write Postscript graphics to the output stream cout. The graphics are drawn by invoking member functions.

This object-oriented approach to drawing graphics is probably more natural than the approach used by the C binding. With libplotter, the use of multiple Plotters in an application becomes very easy. No switching between Plotters is required: the graphics operations can be immediately invoked on any existing Plotter.


Doc Search Man Page Site Search Web Search