Next: , Up: Basic usage   [Contents][Index]


3.1.1 Using MathGL window

The “interactive” way of drawing in MathGL consists in window creation with help of class mglQT, mglFLTK or mglGLUT (see Widget classes) and the following drawing in this window. There is a corresponding code:

#include <mgl2/qt.h>
int sample(mglGraph *gr)
{
  gr->Rotate(60,40);
  gr->Box();
  return 0;
}
//-----------------------------------------------------
int main(int argc,char **argv)
{
  mglQT gr(sample,"MathGL examples");
  return gr.Run();
}

Here callback function sample is defined. This function does all drawing. Other function main is entry point function for console program. For compilation, just execute the command

gcc test.cpp -lmgl-qt5 -lmgl

You can use "-lmgl-qt4" instead of "-lmgl-qt5", if Qt4 is installed.

Alternatively you can create yours own class inherited from mglDraw class and re-implement the function Draw() in it:

#include <mgl2/qt.h>
class Foo : public mglDraw
{
public:
  int Draw(mglGraph *gr);
};
//-----------------------------------------------------
int Foo::Draw(mglGraph *gr)
{
  gr->Rotate(60,40);
  gr->Box();
  return 0;
}
//-----------------------------------------------------
int main(int argc,char **argv)
{
  Foo foo;
  mglQT gr(&foo,"MathGL examples");
  return gr.Run();
}

Or use pure C-functions:

#include <mgl2/mgl_cf.h>
int sample(HMGL gr, void *)
{
  mgl_rotate(gr,60,40,0);
  mgl_box(gr);
}
int main(int argc,char **argv)
{
  HMGL gr;
  gr = mgl_create_graph_qt(sample,"MathGL examples",0,0);
  return mgl_qt_run();
/* generally I should call mgl_delete_graph() here,
 * but I omit it in main() function. */
}

The similar code can be written for mglGLUT window (function sample() is the same):

#include <mgl2/glut.h>
int main(int argc,char **argv)
{
  mglGLUT gr(sample,"MathGL examples");
  return 0;
}

The rotation, shift, zooming, switching on/off transparency and lighting can be done with help of tool-buttons (for mglQT, mglFLTK) or by hot-keys: ‘a’, ‘d’, ‘w’, ‘s’ for plot rotation, ‘r’ and ‘f’ switching on/off transparency and lighting. Press ‘x’ for exit (or closing the window).

In this example function sample rotates axes (Rotate(), see Subplots and rotation) and draws the bounding box (Box()). Drawing is placed in separate function since it will be used on demand when window canvas needs to be redrawn.


Next: , Up: Basic usage   [Contents][Index]