Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 2f67f9c7d34a113061b128c67572102c > files > 12

mathgl-doc-1.11.0.1-4.fc15.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- This manual is for MathGL (version 1.11), a collection of classes and routines for scientific plotting. Please report any errors in this manual to mathgl.abalakin@gmail.org.

Copyright C 2008 Alexey Balakin.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with no
Invariant Sections, with the Front-Cover Texts being "A GNU Manual,"
and with the Back-Cover Texts as in (a) below.  A copy of the
license is included in the section entitled "GNU Free Documentation
License."

(a) The FSF's Back-Cover Text is: "You have the freedom to
copy and modify this GNU manual.  Buying copies from the FSF
supports it in developing GNU and promoting software freedom."

 -->
<!-- Created on February 27, 2011 by texi2html 1.82
texi2html was written by: 
            Lionel Cons <Lionel.Cons@cern.ch> (original author)
            Karl Berry  <karl@freefriends.org>
            Olaf Bachmann <obachman@mathematik.uni-kl.de>
            and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>MathGL 1.11: 2. MathGL examples</title>

<meta name="description" content="MathGL 1.11: 2. MathGL examples">
<meta name="keywords" content="MathGL 1.11: 2. MathGL examples">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">

<a name="Examples"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="mathgl_en_1.html#Thanks" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en_1.html#Overview" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="MathGL-examples"></a>
<h1 class="chapter">2. MathGL examples</h1>

<p>This chapter contain information about basic and advanced MathGL, hints and samples for all types of graphics. I recommend you read first 2 sections one after another and at least look on &ldquo;Hints&rdquo; section. Also I recommend you to look at <a href="mathgl_en_1.html#General-concepts">General concepts</a> and <a href="mathgl_en_1.html#FAQ">FAQ</a>. Sample code for some of these examples can be found in <a href="http://mathgl.sf.net/pictures.html">http://mathgl.sf.net/pictures.html</a> and in <a href="mathgl_en_9.html#Samples">Samples</a>.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Basic-usage">2.1 Basic usage</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                 
</td></tr>
<tr><td align="left" valign="top"><a href="#Advanced-usage">2.2 Advanced usage</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">              
</td></tr>
<tr><td align="left" valign="top"><a href="#Data-handling">2.3 Data handling</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">               
</td></tr>
<tr><td align="left" valign="top"><a href="#Data-plotting">2.4 Data plotting</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">               
</td></tr>
<tr><td align="left" valign="top"><a href="#C_002fFortran-interface">2.5 C/Fortran interface</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">         
</td></tr>
<tr><td align="left" valign="top"><a href="#MathGL-and-PyQt">2.6 MathGL and PyQt</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Hints">2.7 Hints</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr size="6">
<a name="Basic-usage"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Examples" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Using-FLTK_002fGLUT-window" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Basic-usage-1"></a>
<h2 class="section">2.1 Basic usage</h2>

<p>MathGL library can be used by several manners. Each has positive and negative sides:
</p><ul>
<li>
<em>The using of MathGL library features for creating graphical window (requires FLTK or GLUT libraries).</em>

<p>Positive side is the possibility to view the plot at once and to modify it (rotate, zoom or switch on transparency or lighting) by hand or by mouse. Negative sides are: the need  of X-terminal and limitation consisting in working with the only one set of data at a time.
</p></li><li>
<em>Direct writing to file in bitmap or vector format without creation of graphical window.</em>

<p>Positive aspects are: batch processing of similar data set (for example, a set of resulting data files for different calculation parameters), running from the console program (including the cluster calculation), fast and automated drawing, saving pictures for further analysis (or demonstration). Negative sides are: the usage of the external program for picture viewing. Also, the data plotting is non-visual. So, you have to imagine the picture (view angles, lighting and so on) before the plotting. I recommend to use graphical window for determining the optimal parameters of plotting on the base of some typical data set. And later use these parameters for batch processing in console program.
</p></li><li>
<em>Drawing in memory with the following displaying by other graphical program.</em>

<p>In this case the programmer has more freedom in selecting the window libraries (not only FLTK or GLUT), in positioning and surroundings control and so on. I recommend to use such way for &ldquo;settled&rdquo; programs.
</p></li></ul>

<p>Let me consider the aforesaid in more detail.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Using-FLTK_002fGLUT-window">2.1.1 Using FLTK/Qt/GLUT window</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">      
</td></tr>
<tr><td align="left" valign="top"><a href="#Drawing-to-file">2.1.2 Drawing to file</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">             
</td></tr>
<tr><td align="left" valign="top"><a href="#Drawing-in-memory">2.1.3 Drawing in memory</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">           
</td></tr>
<tr><td align="left" valign="top"><a href="#Using-QMathGL">2.1.4 Using QMathGL</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">               
</td></tr>
</table>


<hr size="6">
<a name="Using-FLTK_002fGLUT-window"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Basic-usage" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Drawing-to-file" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Using-FLTK_002fQt_002fGLUT-window"></a>
<h3 class="subsection">2.1.1 Using FLTK/Qt/GLUT window</h3>
<a name="index-window"></a>
<a name="index-widgets"></a>

<p>The &ldquo;interactive&rdquo; way of drawing in MathGL consists in window creation  with help of class <code>mglGraphFLTK</code>, <code>mglGraphQT</code> or <code>mglGraphGLUT</code> (see section <a href="mathgl_en_5.html#Widget-classes">Widget classes</a>) and the following drawing in this window. There is a corresponding code:
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        gr-&gt;Rotate(60,40);
        gr-&gt;Box();
        return 0;
    }
    //-----------------------------------------------------
    int main(int argc,char **argv)
    {
        mglGraphFLTK gr;
        gr.Window(argc,argv,sample,&quot;MathGL examples&quot;);
        return mglFlRun();
    }
</pre><p>Here function <code>sample</code> is defined. This function does all drawing. Other function <code>main</code> is entry point function for console program. Arguments of <code>main</code> should be transfered to <code>Window()</code> since it may contain OS specific information (see section <a href="mathgl_en_4.html#mglGraphAB-class">mglGraphAB class</a>).
</p>
<p>Alternatively you can create yours own class inherited from class <code>mglDraw</code> and re-implement the function <code>Draw()</code> in it:
</p><pre class="verbatim">    class Foo : public mglDraw
    {
    public:
        int Draw(mglGraph *gr);
    } foo;
    //-----------------------------------------------------
    int Foo::Draw(mglGraph *gr)
    {
        gr-&gt;Rotate(60,40);
        gr-&gt;Box();
        return 0;
    }
    //-----------------------------------------------------
    int main(int argc,char **argv)
    {
        mglGraphFLTK gr;
        gr.Window(argc,argv,foo,&quot;MathGL examples&quot;);
        return mglFlRun();
    }
</pre>
<p>The similar code can be written for <code>mglGraphQT</code> or for <code>mglGraphGLUT</code> window (function <code>sample()</code> is the same):
</p><pre class="verbatim">    int main(int argc,char **argv)
    {
        mglGraphGLUT gr;
        gr.Window(argc,argv,sample,&quot;MathGL examples&quot;);
        return 0;
    }
</pre>
<p>The rotation, shift, zooming, switching on/off transparency and lighting can be done with help of tool-buttons (for <code>mglGraphFLTK</code> and <code>mglGraphQT</code>) or by hot-keys: &lsquo;<samp>a</samp>&rsquo;, &lsquo;<samp>d</samp>&rsquo;, &lsquo;<samp>w</samp>&rsquo;, &lsquo;<samp>s</samp>&rsquo; for plot rotation, &lsquo;<samp>r</samp>&rsquo; and &lsquo;<samp>f</samp>&rsquo; switching on/off transparency and lighting. Press &lsquo;<samp>x</samp>&rsquo; for exit (or closing the window).
</p>
<p>In this example function <code>sample</code> rotates axes (<code>Rotate()</code>, see section <a href="mathgl_en_3.html#Transformation-matrix">Transformation matrix</a>) and draws the bounding box (<code>Box()</code>). Drawing procedure is separated in a function since it will be used on demand when window canvas needs to be redrawn. Widget classes (<code>mglGraphFLTK</code>, <code>mglGraphGLUT</code> 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 &lsquo;<samp>,</samp>&rsquo;, &lsquo;<samp>.</samp>&rsquo;) or run as cinema. To switch these feature on one needs to modify function <code>sample</code>:
</p><pre class="verbatim">    int sample1(mglGraph *gr, void *)
    {
        gr-&gt;NewFrame();             // the first frame
        gr-&gt;Rotate(60,40);
        gr-&gt;Box();
        gr-&gt;EndFrame();             // end of the first frame
        gr-&gt;NewFrame();             // the second frame
        gr-&gt;Box();
        gr-&gt;Axis(&quot;xy&quot;);
        gr-&gt;EndFrame();             // end of the second frame
        return GetNumFrame();       // returns the frame number
    }
</pre><p>First, the function creates a frame <code>NewFrame()</code> for rotated axes and draws the bounding box. After the frame drawing the function <code>EndFrame()</code> <strong>must be</strong> called! The second frame contains the bounding box and axes <code>Axis(&quot;xy&quot;)</code> in the initial (unrotated) coordinates. Function <code>sample</code> returns the number of created frames <code>GetNumFrame()</code>.
</p>
<hr size="6">
<a name="Drawing-to-file"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Using-FLTK_002fGLUT-window" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Drawing-in-memory" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Drawing-to-file-1"></a>
<h3 class="subsection">2.1.2 Drawing to file</h3>

<p>Another way of using MathGL library is the direct picture writing to file. It is most usable for plot creating during 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. There are two classes for exporting in file: class <code>mglGraphZB</code> saves in bitmap format (like PNG), <code>mglGraphPS</code> saves in vector PostScript format (see section <a href="mathgl_en_4.html#Plotter-classes">Plotter classes</a>).
</p>
<p>The following code produces a bitmap PNG picture:
</p><pre class="verbatim">    int main(int ,char **)
    {
        mglGraphZB gr;
        gr.Alpha(true);
        gr.Light(true);             gr.Light(0,mglPoint(1,0,-1));
        sample(&amp;gr,NULL);           // The same drawing function.
        gr.WritePNG(&quot;test.png&quot;);    // Don't forget to save the result!
        return 0;
    }
</pre><p>The only difference from the previous (using windows) variant is manual switching the transparency  <code>Alpha</code> and lightning <code>Light</code> on, if the plot requires it. The using of frames is not advisable since the whole image is prepared each time. If function <code>sample</code> contains frames then each frame will be saved to a separate 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 changed parameters, to export in different file types, to emphasize the drawing code and so on. So, in future I will put the drawing in separate function.
</p>
<p>The code for export in vector EPS file looks the same:
</p><pre class="verbatim">    int main(int ,char **)
    {
        mglGraphPS gr;
        gr.Light(true);             gr.Light(0,mglPoint(1,0,-1));
        sample(&amp;gr,NULL);           // The same drawing function.
        gr.WriteEPS(&quot;test.eps&quot;);    // Don't forget to save the result!
        return 0;
    }
</pre><p>The differences from the using of bitmap picture are: applying of the other class <code>mglGraphPS</code>, and writing to other format (function <code>WriteEPS()</code> instead of function <code>WritePNG()</code>). Moreover, there is no switching of the plot transparency <code>Alpha</code> since EPS format does not support it. Possibly I shall include transparency in future by program emulation.
</p>
<p>Classes <code>mglGraphZB</code> and <code>mglGraphPS</code> have some merits and demerits. Class <code>mglGraphZB</code> draws beautiful surface with transparency, smoothed colors and lightning, but the output picture is <em>bitmap</em>, that leads to a bad scalability. On the contrary, class <code>mglGraphPS</code> creates vector file with excellent scalability. But file has large size (especially for surfaces), it does not support transparency and color smoothing. So, vector picture looks stylish but a bit angularly.
</p>
<hr size="6">
<a name="Drawing-in-memory"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Drawing-to-file" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Using-QMathGL" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Drawing-in-memory-1"></a>
<h3 class="subsection">2.1.3 Drawing in memory</h3>

<p>The last way of MathGL using is the drawing in memory. Class <code>mglGraphZB</code> allows one  to create a bitmap picture in memory. Further this picture can be displayed in window by some window libraries (like wxWidgets, FLTK, Windows GDI and so on). For example, the code for drawing in wxWidget library looks like:
</p><pre class="verbatim">    void MyForm::OnPaint(wxPaintEvent&amp; event)
    {
        int w,h,x,y;
        GetClientSize(&amp;w,&amp;h);   // size of the picture
        mglGraphZB gr(w,h);

        gr.Alpha(true);         // draws something using MathGL
        gr.Light(true);         gr.Light(0,mglPoint(1,0,-1));
        sample(&amp;gr,NULL);

        wxImage img(w,h,gr.GetBits(),true);
        ToolBar-&gt;GetSize(&amp;x,&amp;y);    // gets a height of the toolbar if any
        wxPaintDC dc(this);         // and draws it
        dc.DrawBitmap(wxBitmap(img),0,y);
    }
</pre><p>The drawing in other libraries is most the same.
</p>
<p>For example, FLTK code will look like
</p><pre class="verbatim">    void Fl_MyWidget::draw()
    {
        mglGraphZB gr(w(),h());
        gr.Alpha(true);         // draws something using MathGL
        gr.Light(true);         gr.Light(0,mglPoint(1,0,-1));
        sample(&amp;gr,NULL);
        fl_draw_image(gr.GetBits(), x(), y(), gr.GetWidth(), gr.GetHeight(), 3);
    }
</pre><p>Qt code will look like
</p><pre class="verbatim">    void MyWidget::paintEvent(QPaintEvent *)
    {
        mglGraphZB gr(w(),h());
        gr.Alpha(true);         // draws something using MathGL
        gr.Light(true);         gr.Light(0,mglPoint(1,0,-1));
        sample(&amp;gr,NULL);

        // Qt don't support RGB format as is. So, let convert it to BGRN.
        const uchar *bb = gr.GetBits();
        register long i, w=gr.GetWidth(), h=gr.GetHeight();
        *buf = new uchar[4*w*h];
        for(i=0;i&lt;w*h;i++)
        {
            (*buf)[4*i]   = bb[3*i+2];
            (*buf)[4*i+1] = bb[3*i+1];
            (*buf)[4*i+2] = bb[3*i];
            (*buf)[4*i+3] = 255;
        }
        QPixmap pic = QPixmap::fromImage(QImage(*buf, w, h, QImage::Format_RGB32));

        QPainter paint;
        paint.begin(this);  paint.drawPixmap(0,0,pic);  paint.end();
        delete []buf;
    }
</pre>
<hr size="6">
<a name="Using-QMathGL"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Drawing-in-memory" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Advanced-usage" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Basic-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Using-QMathGL-1"></a>
<h3 class="subsection">2.1.4 Using QMathGL</h3>

<p>MathGL have several interface widgets for different widget libraries. There are QMathGL for Qt, Fl_MathGL for FLTK in MathGL v.1.8. These classes provide control which display MathGL graphics. Unfortunately there is no uniform interface for widget classes because all libraries have slightly different set of functions, features and so on. However the usage of MathGL widgets is rather simple. Let me show it on the example of QMathGL.
</p>
<p>First of all you have to define the drawing function or inherit a class from <code>mglDraw</code> class. After it just create a window and setup QMathGL instance as any other Qt widget:
</p><pre class="verbatim">    int main(int argc,char **argv)
    {
        QApplication a(argc,argv);
        QMainWindow *Wnd = new QMainWindow;
        Wnd-&gt;resize(650,480);  // for fill up the QMGL, menu and toolbars
        Wnd-&gt;setWindowTitle(title);
        // here I allow to scroll QMathGL -- the case 
        // then user want to prepare huge picture
        QScrollArea *scroll = new QScrollArea(Wnd);

        // Create and setup QMathGL
        QMathGL *QMGL = new QMathGL(Wnd);
        QMGL-&gt;setPopup(popup); // if you want to setup popup menu for QMGL
        QMGL-&gt;setDraw(sample, NULL);
        // or use QMGL-&gt;setDraw(foo); for instance of class Foo:public mglDraw
        QMGL-&gt;update();

        // continue other setup (menu, toolbar and so on)
        makeMenu();
        scroll-&gt;setWidget(QMGL);
        Wnd-&gt;setCentralWidget(scroll);
        Wnd-&gt;show();
        return a.exec();
    }
</pre>

<hr size="6">
<a name="Advanced-usage"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Using-QMathGL" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Subplots" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Advanced-usage-1"></a>
<h2 class="section">2.2 Advanced usage</h2>

<p>Now I show several non-obvious features of MathGL: several subplots in a single picture, curvilinear coordinates, text printing and so on. Generally you may miss this section at first reading, but I don&rsquo;t recommend it.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Subplots">2.2.1 Subplots</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                    
</td></tr>
<tr><td align="left" valign="top"><a href="#Axis-and-grids">2.2.2 Axis and grids</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">              
</td></tr>
<tr><td align="left" valign="top"><a href="#Curvilinear-coordinates">2.2.3 Curvilinear coordinates</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">     
</td></tr>
<tr><td align="left" valign="top"><a href="#Text-printing-example">2.2.4 Text printing example</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">       
</td></tr>
<tr><td align="left" valign="top"><a href="#Animation">2.2.5 Animation</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                   
</td></tr>
</table>

<hr size="6">
<a name="Subplots"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Advanced-usage" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Axis-and-grids" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Advanced-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Subplots-1"></a>
<h3 class="subsection">2.2.1 Subplots</h3>

<p>Let me demonstrate possibilities of axes transformation. MathGL has the following functions: <code>SubPlot</code>, <code>InPlot</code>, <code>Aspect</code> and <code>Rotate</code> (see section <a href="mathgl_en_3.html#Transformation-matrix">Transformation matrix</a>). The order of their calling is strictly determined. First, one changes the position of axes in image area (functions <code>SubPlot</code> and <code>InPlot</code>). After that one may rotate the plot (function <code>Rotate</code>). Finally, one may change aspects of axes (function <code>Aspect</code>). The following code illustrates the aforesaid it:
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        gr-&gt;SubPlot(2,2,0);
        gr-&gt;Box();
        gr-&gt;Puts(mglPoint(-1,1.1,1),&quot;Just box&quot;,&quot;rL&quot;);
        gr-&gt;InPlot(0.2,0.5,0.7,1);
        gr-&gt;Box();
        gr-&gt;Puts(mglPoint(0,1.2,1),&quot;InPlot example&quot;);

        gr-&gt;SubPlot(2,2,1);
        gr-&gt;Rotate(60,40);
        gr-&gt;Aspect(1,1,1);
        gr-&gt;Box();
        gr-&gt;Puts(mglPoint(1,1,1.5),&quot;Rotate only&quot;,&quot;rR&quot;);

        gr-&gt;SubPlot(2,2,2);
        gr-&gt;Rotate(60,40);
        gr-&gt;Aspect(1,1,2);
        gr-&gt;Box();
        gr-&gt;Puts(mglPoint(0,0,2),&quot;Aspect and Rotate&quot;);

        gr-&gt;SubPlot(2,2,3);
        gr-&gt;Rotate(60,40);
        gr-&gt;Aspect(1,2,2);
        gr-&gt;Box();
        gr-&gt;Puts(mglPoint(0,0,1.5),&quot;Aspect in other direction&quot;);
        return 0;
    }
</pre><p>Here I used function <code>Puts</code> for printing the text in arbitrary position of picture (see section <a href="mathgl_en_3.html#Text-printing">Text printing</a>). Text coordinates and size are connected with axes. However, text coordinates may be everywhere, including the outside the bounding box. I shall show its features later in See section <a href="#Text-printing-example">Text printing example</a>.
</p>
<div class="float">
<img src="../png/sample1.png" alt="../png/sample1">

</div><p><strong>Example of several subplots on the single picture.
</strong>
</p>
<hr size="6">
<a name="Axis-and-grids"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Subplots" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Curvilinear-coordinates" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Advanced-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Axis-and-grids-1"></a>
<h3 class="subsection">2.2.2 Axis and grids</h3>

<p>MathGL library can draw not only the bounding box but also the axes, grids, labels and so on. The limits of axes and their origin (the point of intersection) are determined by function <code>Axis()</code>. Also you can use <code>XRange(), YRange(), ZRange()</code> functions (see section <a href="mathgl_en_3.html#Ranges-_0028bounding-box_0029">Ranges (bounding box)</a>). Ticks on axis are specified by function <code>SetTicks</code> (see section <a href="mathgl_en_3.html#Ticks">Ticks</a>). First argument the direction for each change will be applied. Second argument gives the step between ticks (if positive) or gives the number of ticks on the axis (if negative) or set to use logarithmic ticks (if zero). Third argument gives numbers of sub-ticks between ticks (default is zero). Last argument define the initial ticks position.
</p>
<p>Function <code>Axis</code> draws axes. Its textual string shows in which directions the axis or axes will be drawn (by default <code>&quot;xyz&quot;</code>, function draws axes in all directions). Function <code>Grid</code> draws grid perpendicularly to specified directions. Example of axes and grid drawing is:
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        gr-&gt;SubPlot(2,2,0);
        gr-&gt;SetTicks('x', 0.4, 3);  // sets tick step to 0.5
        gr-&gt;SetTicks('y', 0.4, 3);  // and draws 3 subticks
        gr-&gt;Box();                  // should be after the ticks change
        gr-&gt;Axis(&quot;xy&quot;);
        gr-&gt;Grid();
        gr-&gt;Puts(mglPoint(0,1.3,1),&quot;Axis and grid&quot;);

        gr-&gt;SetTicks('x');  gr-&gt;SetTicks('y'); // restore back
        gr-&gt;Axis(mglPoint(-1,-1,-1),mglPoint(1,1,1),mglPoint(0,0,0));

        gr-&gt;SubPlot(2,2,1);
        gr-&gt;Rotate(60,40);
        gr-&gt;Axis();
        gr-&gt;Label('x',&quot;x&quot;);
        gr-&gt;Label('y',&quot;y&quot;);
        gr-&gt;Label('z',&quot;z&quot;);
        gr-&gt;Puts(mglPoint(0,0,1.5),&quot;Axis and labels&quot;);

        gr-&gt;SubPlot(2,2,2);
        gr-&gt;Rotate(60,40);
        gr-&gt;SetTicks('x', 0.2); gr-&gt;SetTicks('y', 0.2);
        gr-&gt;SetTicks('z', 0.2); // too low step of ticks
        gr-&gt;Axis(mglPoint(-1,-1,-1),mglPoint(1,1,1),mglPoint(-1,-1,-1));
        gr-&gt;Axis();
        gr-&gt;Grid();
        gr-&gt;Puts(mglPoint(0,0,1.5),&quot;Shift origin and add grid&quot;);
        gr-&gt;Puts(mglPoint(0,0,1.2),&quot;(note, too many ticks)&quot;);

        gr-&gt;SubPlot(2,2,3);
        gr-&gt;Rotate(60,40);
        gr-&gt;SetTicks('x', -6);  // decrease the number of ticks
        gr-&gt;SetTicks('y', -6);
        gr-&gt;Axis(&quot;yz&quot;);
        gr-&gt;Label('y',&quot;Y axis&quot;,0);
        gr-&gt;Label('z',&quot;Z axis&quot;,0);
        gr-&gt;Puts(mglPoint(0,0,1.5),&quot;Remove X axis, and&quot;);
        gr-&gt;Puts(mglPoint(0,0,1.2),&quot;decrease number of ticks&quot;);
        return 0;
    }
</pre>
<p>This example shows the importance of the correct choosing of the number of ticks on axis. If tick step is too small then its text may overlap and becomes unreadable. This code has the example of <code>Label</code> function. It draws label for axis in specified direction. The text position on axis is specified by third argument of <code>Label</code> function. If it is positive then then text is drawn near the axis maximum, if negative then the same takes place near the minimum of axis, if zero - then at the center of axis.
</p>
<div class="float">
<img src="../png/sample2.png" alt="../png/sample2">

</div><p><strong>Example of setting up axis range and axis ticks.
</strong>
</p>
<hr size="6">
<a name="Curvilinear-coordinates"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Axis-and-grids" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Text-printing-example" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Advanced-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Curvilinear-coordinates-1"></a>
<h3 class="subsection">2.2.3 Curvilinear coordinates</h3>


<p>Now let use curvilinear coordinates. In difference from other systems of plot creation, MathGL uses textual formulas for connection of the old (data) and new (output) coordinates. This allows one to plot in arbitrary coordinates. The following code plots the line <var>y</var>=0, <var>z</var>=0 in Cartesian, polar, parabolic and spiral coordinates:
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        mglData x(50),y(50),z(50);
        y.Fill(0.5,0.5);
        x.Fill(-1,1);           // creates data arrays

        gr-&gt;Axis(mglPoint(-1,-1,-1),mglPoint(1,1,1),mglPoint(-1,1,-1));
        gr-&gt;dz = 0.5;           // sets tick step to 0.5

        gr-&gt;SubPlot(2,2,0);
        gr-&gt;Rotate(60,40);
        gr-&gt;Plot(x,y,z,&quot;r2&quot;);
        gr-&gt;Axis(); gr-&gt;Grid();
        gr-&gt;Puts(mglPoint(0,1.3,1),&quot;Cartesian&quot;);

        gr-&gt;SubPlot(2,2,1);
        gr-&gt;SetFunc(&quot;y*sin(pi*x)&quot;,&quot;y*cos(pi*x)&quot;,0);
        gr-&gt;Rotate(60,40);
        gr-&gt;Plot(x,y,z,&quot;r2&quot;);
        gr-&gt;Axis(); gr-&gt;Grid();
        gr-&gt;Puts(mglPoint(0,1.3,1),&quot;Cylindrical&quot;);

        gr-&gt;SubPlot(2,2,2);
        gr-&gt;Rotate(60,40);
        gr-&gt;SetFunc(&quot;2*y*x&quot;,&quot;y*y - x*x&quot;,0);
        gr-&gt;Plot(x,y,z,&quot;r2&quot;);
        gr-&gt;Axis(); gr-&gt;Grid();
        gr-&gt;Puts(mglPoint(0,1.3,1),&quot;Parabolic&quot;);

        gr-&gt;SubPlot(2,2,3);
        gr-&gt;Rotate(60,40);
        gr-&gt;SetFunc(&quot;y*sin(pi*x)&quot;,&quot;y*cos(pi*x)&quot;,&quot;x+z&quot;);
        gr-&gt;Plot(x,y,z,&quot;r2&quot;);
        gr-&gt;Axis(); gr-&gt;Grid();
        gr-&gt;Puts(mglPoint(0,1.3,1),&quot;Spiral&quot;);
        return 0;
    }
</pre>
<div class="float">
<img src="../png/sample3.png" alt="../png/sample3">

</div><p><strong>Example of curvilinear coordinates
</strong>
</p>

<hr size="6">
<a name="Text-printing-example"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Curvilinear-coordinates" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Animation" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Advanced-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Text-printing-example-1"></a>
<h3 class="subsection">2.2.4 Text printing example</h3>

<p>MathGL prints text by vector font. There are functions for manual specifying of text position (like <code>Puts</code>) and for its automatic selection (like <code>Label</code>, <code>Legend</code> and so on). MathGL prints text always in specified position even if it lies outside the bounding box. The default size of font is specified by variables <var>FontSize</var> (see section <a href="mathgl_en_3.html#Font-settings">Font settings</a>). However, the actual size of output string depends on position of axes (depends on functions <code>SubPlot</code>, <code>InPlot</code>). The switching of the font style (italic, bold, wire and so on) can be done for the whole string (by function parameter) or inside the string. By default MathGL parses TeX-like commands for symbols and indexes (see see section <a href="mathgl_en_1.html#Font-styles">Font styles</a>). Example of MathGL font drawing is:
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        setlocale(LC_CTYPE, &quot;ru_RU.cp1251&quot;);
        gr-&gt;Puts(mglPoint(0,1),&quot;Text can be in ASCII and in Unicode&quot;);
        gr-&gt;Puts(mglPoint(0,0.6),&quot;It can be \\wire{wire}, \\big{big} &quot;
            &quot;or #r{colored}&quot;);
        gr-&gt;Puts(mglPoint(0,0.2),&quot;One can change style in string: &quot;
            &quot;\\b{bold}, \\i{italic, \\b{both}}&quot;);
        gr-&gt;Puts(mglPoint(0,-0.2),&quot;Easy to \\a{overline} or &quot;
            &quot;\\u{underline}&quot;);
        gr-&gt;Puts(mglPoint(0,-0.6),&quot;Easy to change indexes &quot;
            &quot;^{up} _{down} @{center}&quot;);
        gr-&gt;Puts(mglPoint(0,-1),&quot;It parse TeX: \\int \\alpha \\cdot &quot;
            &quot;\\sqrt3{sin(\\pi x)^2 + \\gamma_{i_k}} dx&quot;);
        return 0;
    }
</pre>
<div class="float">
<img src="../png/sample4.png" alt="../png/sample4">

</div><p><strong>Example of text printing with different font effects
</strong>
</p>
<p>Another example demonstrate the features of TeX formula parsing.
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        gr-&gt;Puts(mglPoint(0), &quot;\\sqrt{\\frac{\\alpha^{\\gamma^2}+&quot;
            &quot;\\overset 1{\\big\\infty}}{\\sqrt3{2+b}}}&quot;, 0, -4);
        return 0;
    }
</pre>
<div class="float">
<img src="../png/samplee.png" alt="../png/samplee">

</div><p><strong>Example of TeX formula parsing
</strong>
</p>
<p>Finally you can change font during execution (this work well for mglGraphZB class only).
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        float h=1.1, d=0.25;
        gr-&gt;LoadFont(&quot;STIX&quot;);       gr-&gt;Puts(mglPoint(0,h), &quot;default font (STIX)&quot;);
        gr-&gt;LoadFont(&quot;adventor&quot;);   gr-&gt;Puts(mglPoint(0,h-d), &quot;adventor font&quot;);
        gr-&gt;LoadFont(&quot;bonum&quot;);      gr-&gt;Puts(mglPoint(0,h-2*d), &quot;bonum font&quot;);
        gr-&gt;LoadFont(&quot;chorus&quot;);     gr-&gt;Puts(mglPoint(0,h-3*d), &quot;chorus font&quot;);
        gr-&gt;LoadFont(&quot;cursor&quot;);     gr-&gt;Puts(mglPoint(0,h-4*d), &quot;cursor font&quot;);
        gr-&gt;LoadFont(&quot;heros&quot;);      gr-&gt;Puts(mglPoint(0,h-5*d), &quot;heros font&quot;);
        gr-&gt;LoadFont(&quot;heroscn&quot;);    gr-&gt;Puts(mglPoint(0,h-6*d), &quot;heroscn font&quot;);
        gr-&gt;LoadFont(&quot;pagella&quot;);    gr-&gt;Puts(mglPoint(0,h-7*d), &quot;pagella font&quot;);
        gr-&gt;LoadFont(&quot;schola&quot;);     gr-&gt;Puts(mglPoint(0,h-8*d), &quot;schola font&quot;);
        gr-&gt;LoadFont(&quot;termes&quot;);     gr-&gt;Puts(mglPoint(0,h-9*d), &quot;termes font&quot;);
    }
</pre>
<div class="float">
<img src="../png/fonts.png" alt="../png/fonts">

</div><p><strong>Example of font face changing.
</strong>
</p>
<hr size="6">
<a name="Animation"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Text-printing-example" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Data-handling" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Advanced-usage" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Animation-1"></a>
<h3 class="subsection">2.2.5 Animation</h3>

<p>You can make animation by several methods in MathGL: by export in animated GIF, or by save each frame in separate file (usually JPEG) and convert these files into movie. Let me show both methods.
</p>
<p>The simplest methods is making animated GIF. There are 3 steps: (1) open GIF file by <code>StartGIF()</code> function; (2) create the frames by calling <code>NewFrame()</code> before and <code>EndFrame()</code> after plotting; (3) close GIF by <code>CloseGIF()</code> function. So the simplest code for &ldquo;running&rdquo; sinusoid will look like this:
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        mglData dat(100);
        char str[32];
        gr-&gt;StartGIF(&quot;sample.gif&quot;);
        for(int i=0;i&lt;100;i++)
        {
            gr-&gt;NewFrame();     // start frame
            gr-&gt;Box();          // some plotting
            sprintf(str,&quot;sin(pi*x+%g*pi)&quot;,0.02*i);
            dat.Modify(str);
            gr-&gt;Plot(dat,&quot;b&quot;);
            gr-&gt;EndFrame();     // end frame
        }
        gr-&gt;CloseGIF();
        return 0;
    }
</pre>
<p>The second way is saving each frame in separate file (usually JPEG) and later make the movie from them. MathGL have special function for saving frames &ndash; it is <code>WriteFrame()</code>. This function save each frame with automatic name &lsquo;<samp>frame0001.jpg, frame0002.jpg</samp>&rsquo; and so on. Here prefix &lsquo;<samp>frame</samp>&rsquo; is defined by <var>PlotId</var> variable of <code>mglGraph</code> class. So the similar code will look like this:
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        mglData dat(100);
        char str[32];
        for(int i=0;i&lt;100;i++)
        {
            gr-&gt;NewFrame();     // start frame
            gr-&gt;Box();          // some plotting
            sprintf(str,&quot;sin(pi*x+%g*pi)&quot;,0.02*i);
            dat.Modify(str);
            gr-&gt;Plot(dat,&quot;b&quot;);
            gr-&gt;EndFrame();     // end frame
            gr-&gt;WriteFrame();   // save frame
        }
        return 0;
    }
</pre>
<p>Created files can be converted to movie by help of a lot of programs. For example, you can use ImageMagic (command &lsquo;<samp>convert frame*.jpg movie.mpg</samp>&rsquo;), MPEG library, GIMP and so on.
</p>
<p>Finally, you can use <code>mgl2gif</code> tool for doing the same with MGL scripts (see section <a href="mathgl_en_8.html#Utilities">Utilities for parsing MGL</a>).
</p>
<hr size="6">
<a name="Data-handling"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Animation" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Array-creation" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Data-handling-1"></a>
<h2 class="section">2.3 Data handling</h2>

<p>Class <code>mglData</code> contains all functions for the data handling in MathGL (see section <a href="mathgl_en_6.html#mglData-class">mglData class</a>). There are several matters why I use class <code>mglData</code> but not a single array: it does not depend on type of data (float or double), sizes of data arrays are kept with data, memory working is simpler and safer.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Array-creation">2.3.1 Array creation</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">              
</td></tr>
<tr><td align="left" valign="top"><a href="#Data-changing">2.3.2 Data changing</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">               
</td></tr>
</table>

<hr size="6">
<a name="Array-creation"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Data-handling" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Data-changing" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Data-handling" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Array-creation-1"></a>
<h3 class="subsection">2.3.1 Array creation</h3>

<p>One can put the data in <code>mglData</code> instance by several ways. Let us do it for sinus function:
</p><ul>
<li>
one can create external array, fill it and put to <code>mglData</code> variable
<pre class="verbatim">    double *a = new double[50];
    for(int i=0;i&lt;50;i++)   a[i] = sin(M_PI*i/49.);

    mglData y;
    y.Set(a,50);
</pre>
</li><li>
another way is to create <code>mglData</code> instance of the desired size and then to work directly with data in this variable
<pre class="verbatim">    mglData y(50);
    for(int i=0;i&lt;50;i++)   y.a[i] = sin(M_PI*i/49.);
</pre>
</li><li>
next way is to fill the data in <code>mglData</code> instance by textual formula with the help of <code>Modify()</code> function
<pre class="verbatim">    mglData y(50);
    y.Modify(&quot;sin(pi*x)&quot;);
</pre>
</li><li>
or one may fill the array in some interval and modify it later
<pre class="verbatim">    mglData y(50);
    y.Fill(0,M_PI);
    y.Modify(&quot;sin(u)&quot;);
</pre>
</li><li>
finally it can be loaded from file
<pre class="verbatim">    FILE *fp=fopen(&quot;sin.dat&quot;,&quot;wt&quot;);   // create file first
    for(int i=0;i&lt;50;i++)   fprintf(fp,&quot;%g\n&quot;,sin(M_PI*i/49.));
    fclose(fp);

    mglData y(&quot;sin.dat&quot;);             // load it
</pre>
</li><li>
at this one can read only part of data
<pre class="verbatim">    FILE *fp-fopen(&quot;sin.dat&quot;,&quot;wt&quot;);   // create large file first
    for(int i=0;i&lt;70;i++)   fprintf(fp,&quot;%g\n&quot;,sin(M_PI*i/49.));
    fclose(fp);

    mglData y;
    y.Read(&quot;sin.dat&quot;,50);             // load it
</pre></li></ul>

<p>Creation of 2d- and 3d-arrays is mostly the same. But one should keep in mind that class <code>mglData</code> uses flat data representation. For example, matrix 30*40 is presented as flat (1d-) array with length 30*40=1200 (nx=30, ny=40). The element with indexes {i,j} is a[i+nx*j]. So for 2d array we have:
</p><pre class="verbatim">    mglData z(30,40);
    for(int i=0;i&lt;30;i++)   for(int j=0;j&lt;40;j++)
        z.a[i+30*j] = sin(M_PI*i/29.)*sin(M_PI*j/39.);
</pre><p>or by using <code>Modify()</code> function
</p><pre class="verbatim">    mglData z(30,40);
    z.Modify(&quot;sin(pi*x)*cos(pi*y)&quot;);
</pre>
<p>The only non-obvious thing here is using multidimensional arrays in C/C++, i.e. arrays defined like <code>float dat[40][30];</code>. Since, formaly this arrays element <code>dat[i]</code> can address the memory in arbitrary place you should use the proper function to convert such arrays to <code>mglData</code> object. For C++ this is functions like <code>mglData::Set(float **dat, int N1, int N2);</code>. For C this is functions like <code>mgl_data_set_float2(HMDT d, const float **dat, int N1, int N2);</code>. At this, you should keep in mind that <code>nx=N2</code> and <code>ny=N1</code> after conversion.
</p>
<hr size="6">
<a name="Data-changing"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Array-creation" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Data-plotting" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Data-handling" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Data-changing-1"></a>
<h3 class="subsection">2.3.2 Data changing</h3>

<p>MathGL has functions for data processing: differentiating, integrating, smoothing and so on (for more detail, see section <a href="mathgl_en_6.html#mglData-class">mglData class</a>). Let us consider some examples. The simplest ones are integration and differentiation. The direction in which operation will be performed is specified by textual string, which may contain symbols &lsquo;<samp>x</samp>&rsquo;, &lsquo;<samp>y</samp>&rsquo; or &lsquo;<samp>z</samp>&rsquo;. For example, the call of <code>Diff(&quot;x&quot;)</code> will differentiate data along &lsquo;<samp>x</samp>&rsquo; direction; the call of <code>Integral(&quot;xy&quot;)</code> perform the double integration of data along &lsquo;<samp>x</samp>&rsquo; and &lsquo;<samp>y</samp>&rsquo; directions; the call of <code>Diff2(&quot;xyz&quot;)</code> will apply 3d Laplace operator to data and so on. Example of this operations on 2d array a=x*y is presented in code:
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        mglData a(30,40);   a.Modify(&quot;x*y&quot;);
        gr-&gt;Axis(mglPoint(0,0,0),mglPoint(1,1,1));
        gr-&gt;SubPlot(2,2,0); gr-&gt;Rotate(60,40);
        gr-&gt;Surf(a);        gr-&gt;Box();
        gr-&gt;Puts(mglPoint(0.7,1,1.2),&quot;a(x,y)&quot;);
        gr-&gt;SubPlot(2,2,1); gr-&gt;Rotate(60,40);
        a.Diff(&quot;x&quot;);        gr-&gt;Surf(a);      gr-&gt;Box();
        gr-&gt;Puts(mglPoint(0.7,1,1.2),&quot;da/dx&quot;);
        gr-&gt;SubPlot(2,2,2); gr-&gt;Rotate(60,40);
        a.Integral(&quot;xy&quot;);   gr-&gt;Surf(a);      gr-&gt;Box();
        gr-&gt;Puts(mglPoint(0.7,1,1.2),&quot;\\int da/dx dxdy&quot;);
        gr-&gt;SubPlot(2,2,3); gr-&gt;Rotate(60,40);
        a.Diff2(&quot;y&quot;);       gr-&gt;Surf(a);      gr-&gt;Box();
        gr-&gt;Puts(mglPoint(0.7,1,1.2),&quot;\\int {d^2}a/dxdy dx&quot;);
        return 0;
    }
</pre>
<div class="float">
<img src="../png/sample6.png" alt="../png/sample6">

</div><p><strong>Example of data differentiation and integration
</strong>
</p>
<p>Data smoothing (function <code>Smooth()</code>) is more interesting and important. This function has 2 main arguments: type of smoothing and its direction. Now 4 methods are supported: <code>SMOOTH_NONE</code> does nothing for delta=0 or approaches data to zero with the step delta, <code>SMOOTH_LINE_3</code> linear averaging by 3 points, <code>SMOOTH_LINE_5</code> linear averaging by 5 points, <code>SMOOTH_QUAD_5</code> quadratic averaging by 5 points. Let me demonstrate it for 1d case:
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        mglData y0(30),y1,y2,y3;
        y0.Modify(&quot;0.4*sin(2*pi*x)+0.3*cos(3*pi*x)-0.4*sin(4*pi*x)+0.2*rnd&quot;);

        y1=y0;  y1.Smooth(SMOOTH_LINE_3);
        y2=y0;  y2.Smooth(SMOOTH_LINE_5);
        y3=y0;  y3.Smooth(SMOOTH_QUAD_5);

        gr-&gt;Plot(y0,&quot;k&quot;);   gr-&gt;AddLegend(&quot;NONE&quot;,&quot;k&quot;);
        gr-&gt;Plot(y1,&quot;r&quot;);   gr-&gt;AddLegend(&quot;LINE_3&quot;,&quot;r&quot;);
        gr-&gt;Plot(y2,&quot;g&quot;);   gr-&gt;AddLegend(&quot;LINE_5&quot;,&quot;g&quot;);
        gr-&gt;Plot(y3,&quot;b&quot;);   gr-&gt;AddLegend(&quot;QUAD_5&quot;,&quot;b&quot;);
        gr-&gt;Legend();       gr-&gt;Box();
        return 0;
    }
</pre>
<div class="float">
<img src="../png/sample7.png" alt="../png/sample7">

</div><p><strong>Example of data smoothing
</strong>
</p>
<p>Finally one can create new data arrays on base of the existing one: extract slice, row or column of data (<code>SubData()</code>), summarize along some of direction(s) (<code>Sum()</code>), find distribution of data elements (<code>Hist()</code>). Note, that all these functions are not thread-safe because they use static internal variable for output array. In particular, the using of several of them in arguments of the same function will lead to unpredictable result.
</p>
<hr size="6">
<a name="Data-plotting"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Data-changing" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Plots-for-1D-data" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Data-plotting-1"></a>
<h2 class="section">2.4 Data plotting</h2>

<p>Let me now show how to plot the data. MathGL generally has 2 types of plotting functions. Simple variant requires a single data array for plotting, other data (coordinates) are considered uniformly distributed in interval <var>Min</var>*<var>Max</var>. Second variant requires data arrays for all coordinates. It allows one to plot rather complex multivalent curves and surfaces (in case of parametric dependencies). Argument setting to default values allows one to plot data in standard form. Manual arguments setting gives possibility for fine tuning of colors, positions and view of graphics. Note, that the call of drawing function adds something to picture but does not clear the previous plots (as it does in Matlab). Another difference from Matlab is that all setup (like transparency, lightning, axis borders and so on) must be specified <strong>before</strong> plotting functions.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Plots-for-1D-data">2.4.1 Plots for 1D data</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">           
</td></tr>
<tr><td align="left" valign="top"><a href="#Plots-for-2D-data">2.4.2 Plots for 2D data</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">           
</td></tr>
<tr><td align="left" valign="top"><a href="#Plots-for-3D-data">2.4.3 Plots for 3D data</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">           
</td></tr>
<tr><td align="left" valign="top"><a href="#Surface-transparency">2.4.4 Surface transparency</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">        
</td></tr>
</table>

<hr size="6">
<a name="Plots-for-1D-data"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Data-plotting" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Plots-for-2D-data" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Data-plotting" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Plots-for-1D-data-1"></a>
<h3 class="subsection">2.4.1 Plots for 1D data</h3>

<p>Term &ldquo;1D data&rdquo; means that data depend on single index (parameter) like curve in parametric form {x(i),y(i),z(i)}, i=1...n. There are 5 generally different types of data representations: simple line plot, line plot with filling under it, stairs plot, bar plot and vertical lines (see section <a href="mathgl_en_3.html#g_t1D-plotting">1D plotting</a>). Each type of plotting has similar interface. There are 3D version and two 2D versions. One of last requires single array. The parameters of line and marks (see section <a href="mathgl_en_1.html#Line-styles">Line styles</a>) are specified by the string argument. If the string parameter is <code>NULL</code> then solid line with color from palette is used (see section <a href="mathgl_en_3.html#Palette-and-colors">Pallete and colors</a>).
</p>
<p>Below I shall show the features of 1D plotting on base of <code>Plot()</code> function (see section <a href="mathgl_en_3.html#Plot">Plot</a>). Let us start from sinus plot:
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        mglData y0(50); 	y0.Modify(&quot;sin(pi*(2*x-1))&quot;);
        gr-&gt;SubPlot(2,2,0);
        gr-&gt;Plot(y0);   	gr-&gt;Box();
</pre><p>Style of line is not specified in <code>Plot()</code> function. So MathGL uses the solid line with first color of palette (this is blue). Next subplot shows array <var>y1</var> with 2 rows:
</p><pre class="verbatim">        gr-&gt;SubPlot(2,2,1);
        mglData y1(50,2);
        y1.Modify(&quot;sin(pi*2*x-pi)&quot;);
        y1.Modify(&quot;cos(pi*2*x-pi)/2&quot;,1);
        gr-&gt;Plot(y1);   	gr-&gt;Box();
</pre><p>As previously I did not specify the style of lines. As a result, MathGL again uses solid line with next colors in palette (there are green and red). Now let us plot a circle on the same subplot. The circle is parametric curve <em>x=cos(\pi t), y=sin(\pi t)</em>. I will set the color of the circle (dark yellow, &lsquo;<samp>Y</samp>&rsquo;) and put marks &lsquo;<samp>+</samp>&rsquo; at point position:
</p><pre class="verbatim">        mglData x(50);  	x.Modify(&quot;cos(pi*2*x-pi)&quot;);
        gr-&gt;Plot(x,y0,&quot;Y+&quot;);
</pre><p>Note that solid line is used because I did not specify the type of line. The same picture can be achieved by <code>Plot2()</code> function. Let us draw ellipse by orange dash line:
</p><pre class="verbatim">        gr-&gt;Plot2(y1,&quot;q|&quot;);
</pre>
<p>Drawing in 3D space is mostly the same. Let us draw spiral with default line style. Now its color is 4-th color from palette (this is cyan):
</p><pre class="verbatim">        gr-&gt;SubPlot(2,2,2);	gr-&gt;Rotate(60,40);
        mglData z(50);  	z.Modify(&quot;2*x-1&quot;);
        gr-&gt;Plot(x,y0,z);	gr-&gt;Box();
</pre><p>Function <code>Plot3()</code> does 3D curve plot but for single array. Use it to put circle marks on the previous plot:
</p><pre class="verbatim">        mglData y2(10,3);	y2.Modify(&quot;cos(pi*(2*x-1+y))&quot;);
        y2.Modify(&quot;2*x-1&quot;,2);
        gr-&gt;Plot3(y2,&quot;bo &quot;);
</pre><p>Note that line style is empty &lsquo;<samp> </samp>&rsquo; here. Usage of other 1D plotting functions looks similar:
</p><pre class="verbatim">        gr-&gt;SubPlot(2,2,3);	gr-&gt;Rotate(60,40);
        gr-&gt;Bars(x,y0,z,&quot;r&quot;);	gr-&gt;Box();
        return 0;
    }
</pre>
<div class="float">
<img src="../png/sample8.png" alt="../png/sample8">

</div><p><strong>Example of 1D data plot
</strong>
</p>
<hr size="6">
<a name="Plots-for-2D-data"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Plots-for-1D-data" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Plots-for-3D-data" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Data-plotting" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Plots-for-2D-data-1"></a>
<h3 class="subsection">2.4.2 Plots for 2D data</h3>

<p>Surfaces <code>Surf()</code> and other 2D plots (see section <a href="mathgl_en_3.html#g_t2D-plotting">2D plotting</a>) are drown the same simpler as 1D one. The difference is that the string parameter specifies not by line style but by the color scheme of the plot (see section <a href="mathgl_en_1.html#Color-scheme">Color scheme</a>). Here I draw attention on 4 most interesting color schemes. There is gray scheme where color is changed from black to white (string &lsquo;<samp>kw</samp>&rsquo;) or from white to black (string &lsquo;<samp>wk</samp>&rsquo;). Another scheme is useful for accentuation of negative (by blue color) and positive (by red color) regions on plot (string &lsquo;<samp>&quot;BbwrR&quot;</samp>&rsquo;). Last one is the popular &ldquo;jet&rdquo; scheme (string &lsquo;<samp>&quot;BbcyrR&quot;</samp>&rsquo;).
</p>
<p>Now I shall show the example of a surface drawing. At first let us switch lightning on
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        gr-&gt;Light(true);	gr-&gt;Light(0,mglPoint(0,0,1));
</pre><p>and draw the surface, considering coordinates x,y to be uniformly distributed in interval <var>Min</var>*<var>Max</var>
</p><pre class="verbatim">        mglData a0(50,40);
        a0.Modify(&quot;0.6*sin(2*pi*x)*sin(3*pi*y)+0.4*cos(3*pi*(x*y))&quot;);
        gr-&gt;SubPlot(2,2,0);	gr-&gt;Rotate(60,40);
        gr-&gt;Surf(a0);		gr-&gt;Box();
</pre><p>Color scheme was not specified. So previous color scheme is used. In this case it is default color scheme (&ldquo;jet&rdquo;) for the first plot. Next example is a sphere. The sphere is parametrically specified surface:
</p><pre class="verbatim">        mglData x(50,40),y(50,40),z(50,40);
        x.Modify(&quot;0.8*sin(2*pi*x)*sin(pi*y)&quot;);
        y.Modify(&quot;0.8*cos(2*pi*x)*sin(pi*y)&quot;);
        z.Modify(&quot;0.8*cos(pi*y)&quot;);
        gr-&gt;SubPlot(2,2,1);	gr-&gt;Rotate(60,40);
        gr-&gt;Surf(x,y,z,&quot;BbwrR&quot;);gr-&gt;Box();
</pre><p>I set color scheme to <code>&quot;BbwrR&quot;</code> that corresponds to red top and blue bottom of the sphere.
</p>
<p>Surfaces will be plotted for each of slice of the data if <var>nz</var>&gt;1. Next example draws surfaces for data arrays with <var>nz</var>=3:
</p><pre class="verbatim">        mglData a1(50,40,3);
        a1.Modify(&quot;0.6*sin(2*pi*x)*sin(3*pi*y)+0.4*cos(3*pi*(x*y))&quot;);
        a1.Modify(&quot;0.6*cos(2*pi*x)*cos(3*pi*y)+0.4*sin(3*pi*(x*y))&quot;,1);
        a1.Modify(&quot;0.6*cos(2*pi*x)*cos(3*pi*y)+0.4*cos(3*pi*(x*y))&quot;,2);
        gr-&gt;SubPlot(2,2,2);	gr-&gt;Rotate(60,40);
        gr-&gt;Alpha(true);
        gr-&gt;Surf(a1);		gr-&gt;Box();
</pre><p>Note, that it may entail a confusion. However, if one will use density plot then the picture will look better:
</p><pre class="verbatim">        gr-&gt;SubPlot(2,2,3);	gr-&gt;Rotate(60,40);
        gr-&gt;Dens(a1);		gr-&gt;Box();
        return 0;
    }
</pre><p>Note, that the previous color scheme is used in last plots because there are no direct specification of the one.
</p>
<div class="float">
<img src="../png/sample9.png" alt="../png/sample9">

</div><p><strong>Example of surface plot for 2D data
</strong>
</p>
<p>Drawing of other 2D plots is analogous. The only peculiarity is the usage of flag &lsquo;<samp>#</samp>&rsquo;. By default this flag switches on the drawing of a grid on plot (<code>Grid()</code> or <code>Mesh()</code> for plots in plain or in volume). However, for isosurfaces (including surfaces of rotation <code>Axial()</code>) this flag switches the  face drawing off. Figure becomes wired. The following code gives example of flag &lsquo;<samp>#</samp>&rsquo; using (compare with normal function drawing as in its description):
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        gr-&gt;Alpha(true);	gr-&gt;Light(true);	gr-&gt;Light(0,mglPoint(0,0,1));
        mglData a(30,20);
        a.Modify(&quot;0.6*sin(2*pi*x)*sin(3*pi*y) + 0.4*cos(3*pi*(x*y))&quot;);

        gr-&gt;SubPlot(2,2,0);	gr-&gt;Rotate(40,60);
        gr-&gt;Surf(a,&quot;BbcyrR#&quot;);		gr-&gt;Box();
        gr-&gt;SubPlot(2,2,1);	gr-&gt;Rotate(40,60);
        gr-&gt;Dens(a,&quot;BbcyrR#&quot;);		gr-&gt;Box();
        gr-&gt;SubPlot(2,2,2);	gr-&gt;Rotate(40,60);
        gr-&gt;Cont(a,&quot;BbcyrR#&quot;);		gr-&gt;Box();
        gr-&gt;SubPlot(2,2,3);	gr-&gt;Rotate(40,60);
        gr-&gt;Axial(a,&quot;BbcyrR#&quot;);		gr-&gt;Box();
        return 0;
    }
</pre>
<div class="float">
<img src="../png/samplea.png" alt="../png/samplea">

</div><p><strong>Example of 2D data plot with color scheme contained &lsquo;<samp>#</samp>&rsquo; symbol
</strong>
</p>
<hr size="6">
<a name="Plots-for-3D-data"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Plots-for-2D-data" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Surface-transparency" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Data-plotting" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Plots-for-3D-data-1"></a>
<h3 class="subsection">2.4.3 Plots for 3D data</h3>

<p>Drawing procedures for 3D plot looks similarly to 1D and 2D plots described above. There are 3 general types of 3D plots (see section <a href="mathgl_en_3.html#g_t3D-plotting">3D plotting</a>): (i) plots on slices or on projections, (ii) isosurfaces, (iii) cloud-like plots. Plots on slice are clear enough &ndash; one specifies a slice (as its index or as coordinate value) and MathGL draws contour lines or density plot on slice plane. Isosurface gives more information. Isosurface is 3D analogue of the contour line <code>Cont()</code>. It shows the region where data array values exceed specified isosurface level. Plot becomes more informative if one adds transparency, lightning or sets color scheme depending on coordinates. Generalization of isosurface is the cloud-like plot. For this plot the darker color and less transparent regions correspond to higher values of data. Contrary, the regions with low values are transparent. For plotting of the phase of fields (or beams or pulses) one can use isosurface which transparency depends on the other data array (see function <code>Surf3A()</code>). As example of 3D data plots let us draw the Gaussian beam diffraction in space. Beam propagates along <var>x</var> axis:
</p><pre class="verbatim">    int sample(mglGraph *gr, void *)
    {
        gr-&gt;Alpha(true);    gr-&gt;Light(true);
        gr-&gt;Light(0,mglPoint(0,0,1));
        mglData a(30,30,30),b(30,30,30);
        a.Modify(&quot;exp(-16*((z-0.5)^2+(y-0.5)^2)/(1+4*x^2))&quot;);
        b.Modify(&quot;16*((z-0.5)^2+(y-0.5)^2)*(x)/(1+4*x^2)&quot;);
        gr-&gt;CAxis(0,1);

        gr-&gt;SubPlot(2,2,0); gr-&gt;Rotate(40,60);
        gr-&gt;Surf3(a,&quot;wgk&quot;); gr-&gt;Box();
        gr-&gt;SubPlot(2,2,1); gr-&gt;Rotate(40,60);
        gr-&gt;DensA(a);       gr-&gt;Box();  gr-&gt;Axis();
        gr-&gt;SubPlot(2,2,2); gr-&gt;Rotate(40,60);
        gr-&gt;CloudQ(a);      gr-&gt;Box();
        gr-&gt;SubPlot(2,2,3); gr-&gt;Rotate(40,60);
        gr-&gt;Surf3A(b,a,&quot;q&quot;);gr-&gt;Box();
        return 0;
    }
</pre>
<div class="float">
<img src="../png/sampleb.png" alt="../png/sampleb">

</div><p><strong>Example of Gaussian beam diffraction (3D data)
</strong>
</p>

<hr size="6">
<a name="Surface-transparency"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Plots-for-3D-data" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#C_002fFortran-interface" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Data-plotting" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Surface-transparency-1"></a>
<h3 class="subsection">2.4.4 Surface transparency</h3>

<p>MathGL library has advanced features for setting and handling the surface transparency. The simplest way to add transparency is the using of function <code>Alpha()</code>. As a result, all further surfaces (and isosurfaces, density plots and so on) become transparent. However, their  look can be additionally improved.
</p>
<p>First, the selected surface will be non-transparent if one sets the flag <code>Transparent</code> before the surface drawing and sets it off after the drawing.
</p>
<p>Second, the value of transparency can be different from surface to surface. To do it just change the value of <code>AlphaDef</code> before the drawing of the selected surface. If its value is close to 0 then the surface becomes more and more transparent. Contrary, if its value is close to 1 then the surface becomes practically non-transparent. This is some analogue of <code>Transparent=true</code>.
</p>
<p>Third feature is the changing of the way how the light goes through overlapped surfaces. The variable <code>TranspType</code> defines it. By default the usual transparency is used (<code>TranspType=0</code>) &ndash; surfaces below is less visible than the upper ones. A &ldquo;glass-like&rdquo; transparency (<code>TranspType=1</code>) has a different look when the surface just decreases the background light (the surfaces are commutable in this case).
</p>
<p>A &ldquo;neon-like&rdquo; transparency (<code>TranspType=2</code>) has more interesting look. In this case a surface is the light source (like a lamp on the dark background) and just adds some intensity to the color. At this, the library sets automatically the black color for the background and changes the default line color to white.
</p>
<p>As example I shall show the variant of plot from <a href="#Plots-for-2D-data">Plots for 2D data</a> (grid drawing is disabled) for different types of transparency.
</p><div class="float">
<img src="../png/type0.png" alt="../png/type0">

</div><p><strong>Example of <code>TranspType=0</code>.
</strong>
</p><div class="float">
<img src="../png/type1.png" alt="../png/type1">

</div><p><strong>Example of <code>TranspType=1</code>.
</strong>
</p><div class="float">
<img src="../png/type2.png" alt="../png/type2">

</div><p><strong>Example of <code>TranspType=2</code>.
</strong>
</p>
<hr size="6">
<a name="C_002fFortran-interface"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Surface-transparency" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#MathGL-and-PyQt" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="C_002fFortran-interface-1"></a>
<h2 class="section">2.5 C/Fortran interface</h2>

<p>The usage of pure C or Fortran or any similar interfaces (see section <a href="mathgl_en_1.html#C-interface">C interface</a>) is practically identical to classes usage. But there are some differences. C functions must have argument HMGL (for graphics) and/or HMDT (for data arrays) which specifies the object for drawing or manipulating (changing). Fortran users may regard these variables as integer. So, firstly the user has to create this object by function mgl_create_*() and has to delete it after the using by function mgl_delete_*().
</p>
<p>Also, all arguments of C function have to be defined. So there are several functions with practically identical names doing practically the same. But some of them have simplified interface for the quick plotting and some of them have access to all plotting parameters for manual tunning.
</p>
<p>As an example of C function usage let me draw the plot from <a href="#Plots-for-2D-data">Plots for 2D data</a>. The C code which does it is shown below:
</p><pre class="verbatim">    #include &lt;mgl/mgl_c.h&gt;
    int main()
    {
        HMGL gr = mgl_create_graph_zb(600, 400);
        mgl_set_alpha(gr, true);
        mgl_set_light(gr, true);
        HMDT a = mgl_create_data_size(30,20,1);
        mgl_data_modify(a,&quot;0.6*sin(2*pi*x)*sin(3*pi*y) + 0.4*cos(3*pi*(x*y))&quot;,0);

        mgl_subplot(gr, 2,2,0);
        mgl_rotate(gr, 40,60,0);
        mgl_surf(gr,a,&quot;BbcyrR#&quot;);
        mgl_box(gr, true);
        mgl_subplot(gr, 2,2,1);
        mgl_rotate(gr, 40,60,0);
        mgl_dens(gr,a,&quot;BbcyrR#&quot;,NAN);
        mgl_box(gr, true);
        mgl_subplot(gr, 2,2,2);
        mgl_rotate(gr, 40,60,0);
        mgl_cont(gr,a,&quot;BbcyrR#&quot;,7,NAN);
        mgl_box(gr, true);
        mgl_subplot(gr, 2,2,3);
        mgl_rotate(gr, 40,60,0);
        mgl_axial(gr,a,&quot;BbcyrR#&quot;,3);
        mgl_box(gr, true);

        /* don't forgot to save graphics */
        mgl_write_png(gr,&quot;sample.png&quot;,0);
        return 0;
    }
</pre>
<p>Practically the same simple to create a window. For example let rewrite the code from for window creation (see section <a href="#Using-FLTK_002fGLUT-window">Using FLTK/Qt/GLUT window</a>):
</p><pre class="verbatim">    int sample(HMGL gr, void *)
    {
        mgl_rotate(gr,60,40,0);
        mgl_box(gr,1);
        return 0;
    }
    //-----------------------------------------------------
    int main(int argc,char **argv)
    {
        mgl_create_graph_fltk(sample, &quot;MathGL examples&quot;, NULL);
        mgl_fltk_run();
        return 0;
    }
</pre>
<p>The Fortran code have some peculiarities. Exactly it not allow one to send arbitrary parameter (which was <code>NULL</code> in previous example) to function. This is limitation of Fortran language. So, the corresponding code will be <strong>NOT TESTED NOW!!!</strong>:
</p><pre class="verbatim">    program TEST
    integer x,f,func
        call mgl_create_graph_fltk(sample, 'MathGL examples');
        call mgl_fltk_run();
    end program TEST
 
    integer function sample(gr)
    integer*8 gr
        call mgl_rotate(gr,60,40,0);
        call mgl_box(gr,1);
        sample=0
    return
    end
</pre>
<hr size="6">
<a name="MathGL-and-PyQt"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#C_002fFortran-interface" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="MathGL-and-PyQt-1"></a>
<h2 class="section">2.6 MathGL and PyQt</h2>

<p>Generally SWIG based classes (including the Python one) are the same as C++ classes. However, there are few tips for using MathGL with PyQt. Below I place a very simple python code which demonstrate how MathGL can be used with PyQt. This code is mostly written by Prof. Dr. Heino Falcke. You can just copy it to a file <code>mgl-pyqt-test.py</code> and execute it from python shell by command <code>execfile(&quot;mgl-pyqt-test.py&quot;)</code>
</p>
<pre class="verbatim">from PyQt4 import QtGui,QtCore
from mathgl import *
import sys
app = QtGui.QApplication(sys.argv)
qpointf=QtCore.QPointF()

class hfQtPlot(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.img=(QtGui.QImage())
    def setgraph(self,gr):
        self.buffer='\t' 
        self.buffer=self.buffer.expandtabs(4*gr.GetWidth()*gr.GetHeight())
        gr.GetBGRN(self.buffer,len(self.buffer))
        self.img=QtGui.QImage(self.buffer, gr.GetWidth(),gr.GetHeight(),QtGui.QImage.Format_ARGB32)
        self.update()
    def paintEvent(self, event):
        paint = QtGui.QPainter()
        paint.begin(self)
        paint.drawImage(qpointf,self.img)
        paint.end()

BackgroundColor=[1.0,1.0,1.0]
size=100
gr=mglGraph()
y=mglData(size)
#y.Modify(&quot;((0.7*cos(2*pi*(x+.2)*500)+0.3)*(rnd*0.5+0.5)+362.135+10000.)&quot;)
y.Modify(&quot;(cos(2*pi*x*10)+1.1)*1000.*rnd-501&quot;)
x=mglData(size)
x.Modify(&quot;x^2&quot;);

def plotpanel(gr,x,y,n):
    gr.SubPlot(2,2,n)
    gr.SetXRange(x)
    gr.SetYRange(y)
    gr.AdjustTicks()
    gr.Axis()
    gr.Box()
    gr.Label(&quot;x&quot;,&quot;x-Axis&quot;,1)
    gr.Label(&quot;y&quot;,&quot;y-Axis&quot;,1)
    gr.ClearLegend()
    gr.AddLegend(&quot;Legend: &quot;+str(n),&quot;k&quot;)
    gr.Legend()
    gr.Plot(x,y)


gr.Clf(BackgroundColor[0],BackgroundColor[1],BackgroundColor[2])
gr.SetPlotFactor(1.5)
plotpanel(gr,x,y,0)
y.Modify(&quot;(cos(2*pi*x*10)+1.1)*1000.*rnd-501&quot;)
plotpanel(gr,x,y,1)
y.Modify(&quot;(cos(2*pi*x*10)+1.1)*1000.*rnd-501&quot;)
plotpanel(gr,x,y,2)
y.Modify(&quot;(cos(2*pi*x*10)+1.1)*1000.*rnd-501&quot;)
plotpanel(gr,x,y,3)

gr.WritePNG(&quot;test.png&quot;,&quot;Test Plot&quot;)

qw = hfQtPlot()
qw.show()
qw.setgraph(gr)
qw.raise_()
</pre>

<hr size="6">
<a name="Hints"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#MathGL-and-PyQt" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#g_t_0060_0060Compound_0027_0027-graphics" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Hints-1"></a>
<h2 class="section">2.7 Hints</h2>

<p>In this section I have included some small hints and advices for the improving of the quality of plots and for the demonstration of some non-trivial features of MathGL library. In contrast to previous examples I showed mostly the idea but not the whole drawing function. More examples with the source code can be find at <a href="http://mathgl.sf.net/">http://mathgl.sf.net/</a> or in section <a href="mathgl_en_9.html#Samples">Samples</a>.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#g_t_0060_0060Compound_0027_0027-graphics">2.7.1 &ldquo;Compound&rdquo; graphics</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">       
</td></tr>
<tr><td align="left" valign="top"><a href="#Two-axes-in-one-plot">2.7.2 Two axes in one plot</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">        
</td></tr>
<tr><td align="left" valign="top"><a href="#Titles-for-the-plot">2.7.3 Titles for the plot</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">         
</td></tr>
<tr><td align="left" valign="top"><a href="#Changing-of-the-color-range">2.7.4 Changing of the color range</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top"> 
</td></tr>
<tr><td align="left" valign="top"><a href="#Management-of-a-point-cutting">2.7.5 Management of a point cutting</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Vector-field-visualization">2.7.6 Vector field visualization</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">  
</td></tr>
<tr><td align="left" valign="top"><a href="#Several-light-sources">2.7.7 Several light sources</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">       
</td></tr>
<tr><td align="left" valign="top"><a href="#CutMin-and-CutMax-features">2.7.8 CutMin and CutMax features</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">  
</td></tr>
<tr><td align="left" valign="top"><a href="#Mapping-visualization">2.7.9 Mapping visualization</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">       
</td></tr>
<tr><td align="left" valign="top"><a href="#Log_002dscaled-plot">2.7.10 Log-scaled plot</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">             
</td></tr>
<tr><td align="left" valign="top"><a href="#g_t_0060_0060Templates_0027_0027">2.7.11 &ldquo;Templates&rdquo;</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">               
</td></tr>
<tr><td align="left" valign="top"><a href="#Nonlinear-fitting-hints">2.7.12 Nonlinear fitting hints</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">     
</td></tr>
<tr><td align="left" valign="top"><a href="#PDE-solving-hints">2.7.13 PDE solving hints</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">           
</td></tr>
<tr><td align="left" valign="top"><a href="#MGL-parser-using">2.7.14 MGL parser using</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">            
</td></tr>
<tr><td align="left" valign="top"><a href="#Stereo-image">2.7.15 Stereo image</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                
</td></tr>
</table>

<hr size="6">
<a name="g_t_0060_0060Compound_0027_0027-graphics"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Hints" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Two-axes-in-one-plot" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="g_t_0060_0060Compound_0027_0027-graphics-1"></a>
<h3 class="subsection">2.7.1 &ldquo;Compound&rdquo; graphics</h3>

<p>As I noted above, MathGL functions (except the special one, like Clf()) do  not erase the previous plotting but just add the new one. It allows one to draw &ldquo;compound&rdquo; plots easily. For example, popular Matlab command <code>surfc</code> can be emulated in MathGL by 2 calls:
</p><pre class="verbatim">    Surf(a);
    Cont(a, 0, 7, -1);     // draw contours at z = -1
</pre><p>Here <var>a</var> is 2-dimensional data for the plotting, <code>-1</code> is the value of z-coordinate at which the contour should be plotted (at the bottom in this example). Analogously, one can draw density plot instead of contour lines and so on.
</p>
<p>Another nice plot is contour lines plotted directly on the surface:
</p><pre class="verbatim">    Light(true);       // switch on light for the surface
    Surf(a, &quot;BbcyrR&quot;); // select 'jet' colormap for the surface
    Cont(a, &quot;y&quot;);      // and yellow color for contours
</pre><p>The possible difficulties arise in black&amp;white case, when the color of the surface can be close to the color of a contour line. In that case I may suggest the following code:
</p><pre class="verbatim">    Light(true);       // switch on light for the surface
    Surf(a, &quot;kw&quot;);     // select 'gray' colormap for the surface
    CAxis(-1,0);       // first draw for darker surface colors
    Cont(a, &quot;w&quot;);      // white contours
    CAxis(0,1);        // now draw for brighter surface colors
    Cont(a, &quot;k&quot;);      // black contours
    CAxis(-1,1);       // return color range to original state
</pre><p>The idea is to divide the color range on 2 parts (dark and bright) and to select the contrasting color for contour lines for each of part.
</p>
<p>Similarly, one can plot flow thread over density plot of vector field amplitude (this is another amusing plot from Matlab) and so on. The list of compound graphics can be prolonged but I hope that the general idea is clear.
</p>
<hr size="6">
<a name="Two-axes-in-one-plot"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#g_t_0060_0060Compound_0027_0027-graphics" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Titles-for-the-plot" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Two-axes-in-one-plot-1"></a>
<h3 class="subsection">2.7.2 Two axes in one plot</h3>

<p>Developing the previous hint one can make a plot with 2 or more axes. The idea is that the change of settings does not influence on the already drawn graphics. So, for 2-axes plot let us set the first axis and draw everything concerning it. Then let us setup the second axis and draw things for the second axis. The corresponding code is (see section <a href="mathgl_en_9.html#g_t2_002daxes-sample">2-axes sample</a>):
</p><pre class="verbatim">    // set up first axis
    Axis(mglPoint(-1,-1,-1),mglPoint(1,1,1),mglPoint(-1,-1,-1));
    Axis();            // draw it
    Plot(y1,&quot;b&quot;);      // draw something in first axis
    // set up second axis
    Axis(mglPoint(0,0,0),mglPoint(1,1,1),mglPoint(1,1,1));
    Axis();            // draw it
    Stem(y2,&quot;r&quot;);      // draw something in second axis
</pre><p>Note, that the first and the second axes look better if being placed in different corners. In the code presented above the first axis is placed in the left-bottom corner, and the second one is placed in the right-top corner.
</p>
<hr size="6">
<a name="Titles-for-the-plot"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Two-axes-in-one-plot" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Changing-of-the-color-range" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Titles-for-the-plot-1"></a>
<h3 class="subsection">2.7.3 Titles for the plot</h3>

<p>The printing of nice titles for the plot is not so trivial task in general case. The problem is that the rotation and aspect change lead to different looks for titles of different subplots. So, the resulting look is not so good as it could be. The solution is simple &ndash; to print titles exactly after <code>SubPlot</code>() call and before any rotation, aspect change and so on! Analogously, the title for the whole picture looks better if it is printed first (before any <code>SubPlot()</code> calls). Alternatively you can use function <code>Title()</code> for plotting title for the picture at any time.
</p>
<hr size="6">
<a name="Changing-of-the-color-range"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Titles-for-the-plot" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Management-of-a-point-cutting" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Changing-of-the-color-range-1"></a>
<h3 class="subsection">2.7.4 Changing of the color range</h3>

<p>By default (for the user comfort), the color range is set equal to z-range of the plot. However, there are different ranges. So, one can obtain amusing plot by the change of color range manually. For example, there are plots with one-color bottom (or top) or practically bi-color picture and so on.
</p>
<p>For example, compare 2 surfaces:
</p><pre class="verbatim">    SubPlot(2,1,0)
    Surf(a);           // usual coloring range
    SubPlot(2,1,1)
    CAxis(0,1);
    Surf(a);           // bottom of the surface have one-colour filling
</pre>
<hr size="6">
<a name="Management-of-a-point-cutting"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Changing-of-the-color-range" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Vector-field-visualization" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Management-of-a-point-cutting-1"></a>
<h3 class="subsection">2.7.5 Management of a point cutting</h3>

<p>Sometimes an experimental or numerical surface has outstanding points. Visualization of such surface will lead to the hole(s) in place of such points. The standard method of &ldquo;fighting&rdquo;  &ndash; to change data values &ndash; is not always good and is not so convenient. MathGL library has another method &ndash; to set variable <code>Cut=false</code>. As a consequence, all outstanding points will be projected on the bounding box.
</p>
<p>Such method is good not only for outstanding points but also for the case when one need to plane the bottom or the top of the plot. Exactly such case is demonstrated in the code:
</p><pre class="verbatim">        mglData a(20,30);  // create some data
        a.Modify(&quot;0.6*sin(2*pi*x)*sin(3*pi*y) + 0.4*cos(3*pi*(x*y))&quot;);
        // set lower border above the data minimal value
        Axis(mglPoint(-1,-1,0),mglPoint(1,1,1));
        Cut = false;       // set off cutting flag
`       Surf(a);           // and draw the surface
</pre><p>It is an interesting result, is not it?
</p>
<hr size="6">
<a name="Vector-field-visualization"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Management-of-a-point-cutting" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Several-light-sources" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Vector-field-visualization-1"></a>
<h3 class="subsection">2.7.6 Vector field visualization</h3>

<p>Vector field visualization (especially in 3d case <code>Vect3</code> or <code>VectC</code>) may look tangly &ndash; there are too many overlapping lines. I may suggest 2 ways to solve this problem. The first one is to change <code>MeshNum</code> for decreasing the number of hachures. The second way is to use the flow thread chart <code>Flow</code>. Unfortunately, I don&rsquo;t know any other methods to visualize 3d vector field. If you know any, e-mail me and I shall add it to MatGL.
</p>
<hr size="6">
<a name="Several-light-sources"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Vector-field-visualization" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#CutMin-and-CutMax-features" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Several-light-sources-1"></a>
<h3 class="subsection">2.7.7 Several light sources</h3>

<p>In contrast to the most of other programs, MathGL supports several (up to 10) light sources. Moreover, the color each of them can be different: white (this is usual), yellow, red, cyan, green and so on. The use of several light sources may be interesting for the highlighting of some peculiarities of the plot or just to make an amusing picture. Note, each light source can be switched on/off individually (see section <a href="mathgl_en_9.html#Several-light-sample">Several light sample</a>).
</p>
<hr size="6">
<a name="CutMin-and-CutMax-features"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Several-light-sources" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Mapping-visualization" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="CutMin-and-CutMax-features-1"></a>
<h3 class="subsection">2.7.8 CutMin and CutMax features</h3>

<p>MathGL library has a feature for cutting of points in some region <var>CutMin*CutMax</var>. Such an excision can be used to improve the look of the graphics. Moreover, this cutting may help to show an internal structure of an object (like <code>isocaps</code> plot in Matlab). For example, let us use the standard 3D data array and show its interior (see section <a href="mathgl_en_9.html#CutMinMax-sample">CutMinMax sample</a>).
</p>
<pre class="verbatim">    mglData  c(61,51,40);      // create the data
    mglData v(10);	v.Fill(-0.5,1);
    c.Modify(&quot;(-2*((2*x-1)^2 + (2*y-1)^2 + (2*z-1)^4 - (2*z-1)^2 - 0.1))&quot;);
    gr-&gt;CutMin = mglPoint(0,-1,-1);     gr-&gt;CutMax = mglPoint(1,0,1.1);
    gr-&gt;Surf3(-0.5,c,&quot;BbcyrR&quot;);
    gr-&gt;ContF3(v,c,'x',-1,&quot;BbcyrR&quot;);    gr-&gt;ContF3(v,c,'y',-1,&quot;BbcyrR&quot;);
    gr-&gt;ContF3(v,c,'z',0,&quot;BbcyrR&quot;);     gr-&gt;ContF3(v,c,'z',39,&quot;BbcyrR&quot;);
</pre>
<p>One can also exclude points from arbitrary area in space. This area defined by textual formula <code>CutOff()</code> (see section <a href="mathgl_en_3.html#Cutting">Cutting</a>). The algorithm is the same as shown for &ldquo;rectangular cutting&rdquo;.
</p>
<hr size="6">
<a name="Mapping-visualization"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#CutMin-and-CutMax-features" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Log_002dscaled-plot" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Mapping-visualization-1"></a>
<h3 class="subsection">2.7.9 Mapping visualization</h3>

<p>Sometime ago I worked with mapping and have a question about its visualization. Let me remember you that mapping is some transformation rule for one set of number to another one. The 1d mapping is just an ordinary function &ndash; it takes a number and transforms it to another one. The 2d mapping (which I used) is a pair of functions which take 2 numbers and transform them to another 2 ones. Except general plots (like <code>SurfC</code>, <code>SurfA</code>) there is a special plot &ndash; Arnold diagram. It shows the area which is the result of mapping of some initial area (usually square).
</p>
<p>I tried to make such plot in <code>Map</code>. It shows the set of points or set of faces, which final position is the result of mapping. At this, the color gives information about their initial position and the height describes Jacobian value of the transformation. Unfortunately, it looks good only for the simplest mapping but for the  real multivalent quasi-chaotic mapping it produces a confusion. So, use it if you like :).
</p>
<hr size="6">
<a name="Log_002dscaled-plot"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Mapping-visualization" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#g_t_0060_0060Templates_0027_0027" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Log_002dscaled-plot-1"></a>
<h3 class="subsection">2.7.10 Log-scaled plot</h3>

<p>Log-scaled plot can be drawn by 2 steps. First, one should change the scale of axis by call <code>SetFunc(&quot;lg(x)&quot;, &quot;lg(y)&quot;);</code> (in this example, x- and y-axis will be log-scaled). Second, one should set logarithmic scale for axis ticks by changing variables: <code>SetTicks('x',0); SetTicks('y',0);</code>. Finally, one should check (or change) the axis ranges and origin so that their values to be positive. For example of log-log plot see section <a href="mathgl_en_9.html#Log_002dlog-sample">Log-log sample</a>.
</p>
<hr size="6">
<a name="g_t_0060_0060Templates_0027_0027"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Log_002dscaled-plot" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Nonlinear-fitting-hints" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="g_t_0060_0060Templates_0027_0027-1"></a>
<h3 class="subsection">2.7.11 &ldquo;Templates&rdquo;</h3>

<p>As I have noted before, the change of settings will influence only for the further plotting commands. This allows one to create template function which will contain settings and primitive drawing for often used plots. Correspondingly one may call this template-function for drawing simplification.
</p>
<p>For example, let one has a set of points (experimental or numerical) and wants to compare it with theoretical law (for example, with exponent law <em>\exp(-x/2), x \in [0, 20]</em>). The template-function for this task is:
</p><pre class="verbatim">    void template(mglGraph *gr)
    {
        mglData  law(100);      // create the law
        law.Modify(&quot;exp(-10*x)&quot;);
        gr-&gt;Axis(mglPoint(0,0.0001), mglPoint(20,1), mglPoint(0,0.0001));
        gr-&gt;SetFunc(0,&quot;lg(y)&quot;,0);   gr-&gt;dy = 0;
        gr-&gt;Plot(law,&quot;r2&quot;);
        gr-&gt;Text(mglPoint(10,0.2),&quot;Theoretical law: e^x&quot;,&quot;rL&quot;);
        gr-&gt;Label('x',&quot;x val.&quot;); gr-&gt;Label('y',&quot;y val.&quot;);
        gr-&gt;Axis(); gr-&gt;Grid(&quot;xy&quot;,&quot;g;&quot;); gr-&gt;Box();
    }
</pre><p>At this, one will only write a few lines for data drawing:
</p><pre class="verbatim">    template(gr);     // apply settings and default drawing from template
    mglData dat(&quot;fname.dat&quot;); // load the data
    // and draw it (suppose that data file have 2 columns)
    gr-&gt;Plot(dat.SubData(0),dat.SubData(1),&quot;bx &quot;);
</pre><p>A template-function can also contain settings for font, transparency, lightning, color scheme and so on.
</p>
<hr size="6">
<a name="Nonlinear-fitting-hints"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#g_t_0060_0060Templates_0027_0027" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#PDE-solving-hints" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Nonlinear-fitting-hints-1"></a>
<h3 class="subsection">2.7.12 Nonlinear fitting hints</h3>

<p>Nonlinear fitting is rather simple. All that you need is the data to fit, the approximation formula and the list of coefficients to fit (better with its initial guess values). Let me demonstrate it on the following simple example (see section <a href="mathgl_en_9.html#Fitting-sample">Fitting sample</a>). First, let us use sin function with some random noise:
</p><pre class="verbatim">    mglData rnd(100), idl(50); //data to be fitted and ideal data
    rnd.Modify(&quot;0.4*rnd+0.1+sin(4*pi*x)&quot;);
    idl.Modify(&quot;0.3+sin(4*pi*x)&quot;);
</pre><p>and plot it to see that data we will fit
</p><pre class="verbatim">    gr-&gt;Axis(mglPoint(-1,-2), mglPoint(1,2));
    gr-&gt;Plot(rnd, &quot;. &quot;); gr-&gt;Plot(idl, &quot;b&quot;);
    gr-&gt;Box();
    gr-&gt;Text(mglPoint(0,2.2), &quot;initial: y = 0.3+sin(2\pi x)&quot;, &quot;C:b&quot;, -1);
</pre>
<p>The next step is the fitting itself. For that let me specify an initial values <var>ini</var> for coefficients &lsquo;<samp>abc</samp>&rsquo; and do the fitting for approximation formula &lsquo;<samp>a+b*sin(c*x)</samp>&rsquo;
</p><pre class="verbatim">    mglData res;   // The data for found formula
    float ini[3] = {1, 1, 3};
    gr-&gt;Fit(res, rnd, &quot;a+b*sin(c*x)&quot;, &quot;abc&quot;, ini);
</pre><p>Now display it
</p><pre class="verbatim">    gr-&gt;Plot(res, &quot;r&quot;);
    gr-&gt;Text(mglPoint(-1,-1.3), &quot;fitted:&quot;, &quot;L:r&quot;, -1);
    gr-&gt;PutsFit(mglPoint(0,-1.8), &quot;y = &quot;, &quot;C:r&quot;, -1);
</pre>
<p>NOTE! the fitting results may have strong dependence on initial values for coefficients due to algorithm features. The problem is that in general case there are several local &quot;optimums&quot; for coefficients and the program returns only first found one! There are no guaranties that it will be the best. Try for example to set <code>ini[3] = {0, 0, 0}</code> in the code above.
</p>
<hr size="6">
<a name="PDE-solving-hints"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Nonlinear-fitting-hints" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#MGL-parser-using" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="PDE-solving-hints-1"></a>
<h3 class="subsection">2.7.13 PDE solving hints</h3>

<p>Solving of Partial Differential Equations (PDE, including beam tracing) and ray tracing (or finding particle trajectory) are more or less common task. So, MathGL have several functions for that. There are <code>mglRay()</code> for ray tracing, <code>mglPDE()</code> for PDE solving, <code>mglQO2d()</code> for beam tracing in 2D case (see section <a href="mathgl_en_6.html#Global-functions">Global functions</a>). Note, that these functions take &ldquo;Hamiltonian&rdquo; or equations as string values. And I don&rsquo;t plan now to allow one to use user-defined functions. There are 2 reasons: the complexity of corresponding interface; and the basic nature of used methods which are good for samples but may not good for serious scientific calculations.
</p>
<p>The ray tracing can be done by <code>mglRay()</code> function. Really ray tracing equation is Hamiltonian equation for 3D space. So, the function can be also used for finding a particle trajectory (i.e. solve Hamiltonian ODE) for 1D, 2D or 3D cases. The function have a set of arguments. First of all, it is Hamiltonian which defined the media (or the equation) you are planning to use. The Hamiltonian is defined by string which may depend on coordinates &lsquo;<samp>x</samp>&rsquo;, &lsquo;<samp>y</samp>&rsquo;, &lsquo;<samp>z</samp>&rsquo;, time &lsquo;<samp>t</samp>&rsquo; (for particle dynamics) and momentums &lsquo;<samp>p</samp>&rsquo;=<em>p_x</em>, &lsquo;<samp>q</samp>&rsquo;=<em>p_y</em>, &lsquo;<samp>v</samp>&rsquo;=<em>p_z</em>. Next, you have to define the initial conditions for coordinates and momentums at &lsquo;<samp>t</samp>&rsquo;=0 and set the integrations step (default is 0.1) and its duration (default is 10). The Runge-Kutta method of 4-th order is used for integration.
</p><pre class="verbatim">    mglData r,a,re(128),im(128);
    r = mglRay(&quot;p^2+q^2-x-1&quot;, mglPoint(-0.7, -1), mglPoint(0, 0.5));
    gr-&gt;Plot(r.SubData(0), r.SubData(1));
</pre><p>This example calculate the reflection from linear layer (media with Hamiltonian &lsquo;<samp>p^2+q^2-x-1</samp>&rsquo;=<em>p_x^2+p_y^2-x-1</em>). This is parabolic curve. The resulting array have 7 columns which contain data for {x,y,z,p,q,v,t}.
</p>
<p>The solution of PDE is a bit more complicated. As previous you have to specify the equation as pseudo-differential operator <em>\hat H(x, \nabla)</em> which is called sometime as &ldquo;Hamiltonian&rdquo; (for example, in beam tracing). As previously, it is defined by string which may depend on coordinates &lsquo;<samp>x</samp>&rsquo;, &lsquo;<samp>y</samp>&rsquo;, &lsquo;<samp>z</samp>&rsquo; (but not time!), momentums &lsquo;<samp>p</samp>&rsquo;=<em>(d/dx)/i k_0</em>, &lsquo;<samp>q</samp>&rsquo;=<em>(d/dy)/i k_0</em> and field amplitude &lsquo;<samp>u</samp>&rsquo;=<em>|u|</em>. The evolutionary coordinate is &lsquo;<samp>z</samp>&rsquo; in all cases. So that, the equation look like <em>du/dz = ik_0 H(x,y,\hat p, \hat q, |u|)[u]</em>. Dependence on field amplitude &lsquo;<samp>u</samp>&rsquo;=<em>|u|</em> allows one to solve nonlinear problems too. For example, for nonlinear Shrodinger equation you may set <code>ham=&quot;p^2 + q^2 - u^2&quot;</code>. Also you may specify imaginary part for wave absorption, like <code>ham = &quot;p^2 + i*x*(x&gt;0)&quot;</code>, but only if dependence on variable &lsquo;<samp>i</samp>&rsquo; is linear (i.e. <em>H = Hre+i*Him</em>).
</p>
<p>Next step is specifing the initial conditions at &lsquo;<samp>z</samp>&rsquo;=<code>Min.z</code>. The function need 2 arrays for real and for imaginary part. Note, that coordinates x,y,z are supposed to be in specified range [Min, Max]. So, the data arrays should have corresponding scales. Finally, you may set the integration step and paramter k0=<em>k_0</em>. Also keep in mind, that internally the 2 times large box is used (for suppressing numerical reflection from boundaries) and the equation should well defined even in this extended range.
</p>
<p>Final comment is concerning the possible form of pseudo-differential operator <em>H</em>. At this moment, simplified form of operator <em>H</em> is supported &ndash; all &ldquo;mixed&rdquo; terms (like &lsquo;<samp>x*p</samp>&rsquo;-&gt;x*d/dx) are excluded. For example, in 2D case this operator is effectively <em>H = f(p,z) + g(x,z,u)</em>. However commutable combinations (like &lsquo;<samp>x*q</samp>&rsquo;-&gt;x*d/dy) are allowed for 3D case. 
</p>
<p>So, for example let solve the equation for beam deflected from linear layer and absorbed later. The operator will have the form &lsquo;<samp>&quot;p^2+q^2-x-1+i*0.5*(z+x)*(z&gt;-x)&quot;</samp>&rsquo; that correspond to equation <em>ik_0 \partial_z u + \Delta u + x \cdot u + i (x+z)/2 \cdot u = 0</em>. This is typical equation for Electron Cyclotron (EC) absorption in magnetized plasmas. For initial conditions let me select the beam with plane phase front <em>exp(-48*(x+0.7)^2)</em>. The corresponding code looks like this (see section <a href="mathgl_en_9.html#PDE-sample">PDE sample</a>):
</p><pre class="verbatim">    mglData a,re(128),im(128);
    re.Fill(&quot;exp(-48*(x+0.7)^2)&quot;, gr-&gt;Min, gr-&gt;Max);
    a = mglPDE(&quot;p^2+q^2-x-1+i*0.5*(z+x)*(z&gt;-x)&quot;, re, im,
                gr-&gt;Min, gr-&gt;Max, 0.01, 30);
    a.Transpose(&quot;yxz&quot;);
    gr-&gt;CAxis(0, 1);
    gr-&gt;Dens(a,&quot;wyrRk&quot;);
</pre>
<p>The last example is example of beam tracing. Beam tracing equation is special kind of PDE equation written in coordinates accompanied to a ray. Generally this is the same parameters and limitation as for PDE solving but the coordinates are defined by the ray and by parameter of grid width <var>w</var> in direction transverse the ray. So, you don&rsquo;t need to specify the range of coordinates. <strong>BUT</strong> there is limitation. The accompanied coordinates are well defined only for smooth enough rays, i.e. then the ray curvature <em>K</em> (which is defined as <em>1/K^2 = (|\ddot r|^2 |\dot r|^2 - (\ddot r, \dot r)^2)/|\dot r|^6</em>) is much large then the grid width: <em>K&gt;&gt;w</em>. So, you may receive incorrect results if this condition will be broken.
</p>
<p>You may use following code for obtaining the same solution as in previous example:
</p><pre class="verbatim">    mglData r, xx, yy, a, im(128), re(128);
    const char *ham = &quot;p^2+q^2-x-1+i*0.5*(y+x)*(y&gt;-x)&quot;;
    r = mglRay(ham, mglPoint(-0.7, -1), mglPoint(0, 0.5), 0.02, 2);
    // now start beam tracing
    re.Fill(&quot;exp(-48*x^2)&quot;, gr-&gt;Min, gr-&gt;Max);
    a = mglQO2d(ham, re, im, r, 1, 30, &amp;xx, &amp;yy);
    gr-&gt;CAxis(0, 1);
    gr-&gt;Dens(xx, yy, a, &quot;wyrRk&quot;);
</pre>

<hr size="6">
<a name="MGL-parser-using"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#PDE-solving-hints" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Stereo-image" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="MGL-parser-using-1"></a>
<h3 class="subsection">2.7.14 MGL parser using</h3>

<p>Sometimes you may prefer to use MGL scripts in yours code. It is simpler (especially in comparison with C/Fortran interfaces) and fast way to plot the data with annotations, labels and so on. Class <code>mglParse</code> (see section <a href="mathgl_en_7.html#mglParse-class">mglParse class</a> parse MGL scripts in C++. It have also the corresponding interface for C/Fortran.
</p>
<p>The key function here is <code>mglParse::Parse()</code> (or <code>mgl_parse()</code> for C/Fortran) which execute one command per string. At this the detailed information about the possible errors or warnings is passed as function value. Or you may execute the whole script as long string with lines separated by &lsquo;<samp>\n</samp>&rsquo;. Functions <code>mglParse::Execute()</code> and <code>mgl_parse_text()</code> perform it. Also you may set the values of paramters &lsquo;<samp>$0</samp>&rsquo;...&lsquo;<samp>$9</samp>&rsquo; for the script by functions <code>mglParse::AddParam()</code> or <code>mgl_add_param()</code>, allow/disable picture resizing, check &ldquo;once&rdquo; status and so on. The usage is rather stright-forward.
</p>
<p>The only non-obvious thing is data transition between script and yours program. There are 2 stages: add or find variable; and set data to variable. In C++ you may use functions <code>mglParse::AddVar()</code> and <code>mglParse::FindVar()</code> which return pointer to <code>mglVar</code> structure. This structure contain data itself, the variable name and callback function which will be called if variable destroied. Last feature allows you to control the presence of the variable and, for example, close a window with data if this variable is destroyed. In C/Fortran the corresponding functions are <code>mgl_add_var()</code>, <code>mgl_find_var()</code>. But these functions return the data array only. Note, you <strong>must not delete or free</strong> the data obtained from these functions!
</p>
<p>So, some simple example at the end. Here I define a data array, create variable, put data into it and plot it. The C++ code looks like this:
</p><pre class="verbatim">    float a[100];   // let a_i = sin(4*pi*x), x=0...1
    for(int i=0;i&lt;100;i++)  a[i]=sin(4*M_PI*i/99);
    mglParse *parser = new mglParse;
    mglData &amp;d = (parser-&gt;AddVar(&quot;dat&quot;))-&gt;d;
    d.Set(a,100); // set data to variable
    parser-&gt;Execute(gr, &quot;plot dat; xrange 0 1\nbox\naxis&quot;);
    // you may break script at any line do something 
    // and continue after that
    parser-&gt;Execute(gr, &quot;xlabel 'x'\nylabel 'y'&quot;);
    // also you may use cycles or conditions in script
    parser-&gt;Execute(gr, &quot;for $0 -1 1 0.1\nline 0 0 -1 $0 'r'\nnext&quot;);
    gr-&gt;WritePNG(&quot;test.png&quot;);   // don't forgot to save picture
</pre><p>The code in C/Fortran looks practically the same:
</p><pre class="verbatim">    float a[100];   // let a_i = sin(4*pi*x), x=0...1
    int i;
    for(i=0;i&lt;100;i++)  a[i]=sin(4*M_PI*i/99);
    HMPR parser = mgl_create_parser();
    HMDT d = mgl_add_var(parser, &quot;dat&quot;);
    mgl_data_set_float(d,a,100,1,1);    // set data to variable
    mgl_parse_text(gr, parser, &quot;plot dat; xrange 0 1\nbox\naxis&quot;);
    // you may break script at any line do something 
    // and continue after that
    mgl_parse_text(gr, parser, &quot;xlabel 'x'\nylabel 'y'&quot;);
    // also you may use cycles or conditions in script
    mgl_parse_text(gr, parser, &quot;for $0 -1 1 0.1\nline 0 0 -1 $0 'r'\nnext&quot;);
    mgl_write_png(gr, &quot;test.png&quot;, &quot;&quot;);  // don't forgot to save picture
</pre>
<hr size="6">
<a name="Stereo-image"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#MGL-parser-using" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Hints" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Stereo-image-1"></a>
<h3 class="subsection">2.7.15 Stereo image</h3>

<p>One can easily create stereo image in MathGL. Stereo image can be produced by making two subplots with slightly different rotation angles. The corresponding code looks like this (see section <a href="mathgl_en_9.html#Stereo-image-sample">Stereo image sample</a>):
</p><pre class="verbatim">    gr-&gt;SubPlot(2,1,0);     // left image
    gr-&gt;Rotate(40,60+3);
    // draw something here
    
    gr-&gt;SubPlot(2,1,1);     // right image
    gr-&gt;Rotate(40,60-3);
    // draw the same here
</pre>

<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Examples" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_3.html#MathGL-core" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_12.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
 <font size="-1">
  This document was generated on <i>February 27, 2011</i> using <a href="http://www.nongnu.org/texi2html/"><i>texi2html 1.82</i></a>.
 </font>
 <br>

</p>
</body>
</html>