Another way of using MathGL library is the direct writing of the picture to the file. It is most usable for plot creation during long calculation or for using of small programs (like Matlab or Scilab scripts) for visualizing repetitive sets of data. But the speed of drawing is much higher in comparison with a script language.
The following code produces a bitmap PNG picture:
#include <mgl2/mgl.h> int main(int ,char **) { mglGraph gr; gr.Alpha(true); gr.Light(true); sample(&gr); // The same drawing function. gr.WritePNG("test.png"); // Don't forget to save the result! return 0; }
For compilation, you need only libmgl library not the one with widgets
gcc test.cpp -lmgl
This can be important if you create a console program in computer/cluster where X-server (and widgets) is inaccessible.
The only difference from the previous variant (using windows) is manual switching on the transparency Alpha
and lightning Light
, if you need it. The usage of frames (see Animation) is not advisable since the whole image is prepared each time. If function sample
contains frames then only last one will be saved to the file. In principle, one does not need to separate drawing functions in case of direct file writing in consequence of the single calling of this function for each picture. However, one may use the same drawing procedure to create a plot with changeable parameters, to export in different file types, to emphasize the drawing code and so on. So, in future I will put the drawing in the separate function.
The code for export into other formats (for example, into vector EPS file) looks the same:
#include <mgl2/mgl.h> int main(int ,char **) { mglGraph gr; gr.Light(true); sample(&gr); // The same drawing function. gr.WriteEPS("test.eps"); // Don't forget to save the result! return 0; }
The difference from the previous one is using other function WriteEPS()
for EPS format instead of function WritePNG()
. Also, there is no switching on of the plot transparency Alpha
since EPS format does not support it.