[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
2.1.1 Using FLTK/Qt/GLUT window
The “interactive” way of drawing in MathGL consists in window creation with help of class mglGraphFLTK
, mglGraphQT
or mglGraphGLUT
(see section Widget classes) and the following drawing in this window. There is a corresponding code:
int sample(mglGraph *gr, void *) { gr->Rotate(60,40); gr->Box(); return 0; } //----------------------------------------------------- int main(int argc,char **argv) { mglGraphFLTK gr; gr.Window(argc,argv,sample,"MathGL examples"); return mglFlRun(); }
Here function sample
is defined. This function does all drawing. Other function main
is entry point function for console program. Arguments of main
should be transfered to Window()
since it may contain OS specific information (see section mglGraphAB class).
Alternatively you can create yours own class inherited from class mglDraw
and re-implement the function Draw()
in it:
class Foo : public mglDraw { public: int Draw(mglGraph *gr); } foo; //----------------------------------------------------- int Foo::Draw(mglGraph *gr) { gr->Rotate(60,40); gr->Box(); return 0; } //----------------------------------------------------- int main(int argc,char **argv) { mglGraphFLTK gr; gr.Window(argc,argv,&foo,"MathGL examples"); return mglFlRun(); }
The similar code can be written for mglGraphQT
or for mglGraphGLUT
window (function sample()
is the same):
int main(int argc,char **argv) { mglGraphGLUT gr; gr.Window(argc,argv,sample,"MathGL examples"); return 0; }
The rotation, shift, zooming, switching on/off transparency and lighting can be done with help of tool-buttons (for mglGraphFLTK
and mglGraphQT
) 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 section Transformation matrix) and draws the bounding box (Box()
). Drawing procedure is separated in a function since it will be used on demand when window canvas needs to be redrawn. Widget classes (mglGraphFLTK
, mglGraphGLUT
and so on) support a delayed drawing, when all plotting functions are called once at the beginning of writing to memory lists. Further program displays the saved lists faster. Resulting redrawing will be faster but it requires sufficient memory. Several lists (frames) can be displayed one after another (by pressing ‘,’, ‘.’) or run as cinema. To switch these feature on one needs to modify function sample
:
int sample1(mglGraph *gr, void *) { gr->NewFrame(); // the first frame gr->Rotate(60,40); gr->Box(); gr->EndFrame(); // end of the first frame gr->NewFrame(); // the second frame gr->Box(); gr->Axis("xy"); gr->EndFrame(); // end of the second frame return GetNumFrame(); // returns the frame number }
First, the function creates a frame NewFrame()
for rotated axes and draws the bounding box. After the frame drawing the function EndFrame()
must be called! The second frame contains the bounding box and axes Axis("xy")
in the initial (unrotated) coordinates. Function sample
returns the number of created frames GetNumFrame()
.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |