ThomasL. - 10 months ago 100

C++ Question

I’m using MathGL 2.3.5.1 (http://mathgl.sourceforge.net/doc_en/Main.html) under Windows 10 with Qt 4.8.7.

I have problems to use the vector field of MathGL. There is a documentation of MathGL with the file name mathgl-2.3.5.eng.pdf under

https://sourceforge.net/projects/mathgl/files/mathgl/mathgl%202.3.5/

The section „4.15 Vector fields“ describes the usage of the method “Vect”. And of course there is a vector field example in section “2.8 Vector field samples”.

First of all, my understanding of a vector field is that there are points (in my case it is always in 2D) and (mathematically) vectors. So the combination of point P1(1,1) and the vector V1(1,1) means the arrow starts at point P1(1,1) and ends at point P2(2,2) which is simple vector addition: P2 = P1 + V1.

Now back to MathGL. I’m talking about this method:

void Vect (const mglDataA &x, const mglDataA &y, const mglDataA &ax, const mglDataA &ay, const char *sch="", const char *opt="");

If I understand the parameters correctly then

- x represents the start of the arrow in x units
- y represents the start of the arrow in y units
- ax represents the units in x direction for the (mathematically)

vector - ay represents the units in y direction for the (mathematically)

vector

So let’s start with coding.

`int sample(mglGraph *gr)`

{

long cols=2;

long rows=2;

mglData x; x.Create(cols, rows);

x.a[0] = 1.0; x.a[1] = 0.0; x.a[2] = 0.0; x.a[3] = 0.0;

mglData y; y.Create(cols, rows);

y.a[0] = 1.0; y.a[1] = 0.0; y.a[2] = 0.0; y.a[3] = 0.0;

mglData ax; ax.Create(cols, rows);

ax.a[0] = 1.0; ax.a[1] = 0.0; ax.a[2] = 0.0; ax.a[3] = 0.0;

mglData ay; ay.Create(cols, rows);

ay.a[0] = 1.0; ay.a[1] = 0.0; ay.a[2] = 0.0; ay.a[3] = 0.0;

gr->Title("Vector field P1(1,1) V1(1,1)");

gr->SetRanges(0, 3, 0, 3);

gr->Vect(x, y, ax, ay, "<");

gr->Box();

gr->Axis();

gr->Grid("xy", "h:");

return 0;

}

First of all I can’t create “column vectors” for x, y, ax and ay by meaning I can’t write:

`long cols=1;`

long rows=2;

mglData x; x.Create(cols, rows); x.a[0] = 1.0; x.a[1] = 0.0;

mglData y; y.Create(cols, rows); y.a[0] = 1.0; y.a[1] = 0.0;

mglData ax; ax.Create(cols, rows); ax.a[0] = 1.0; ax.a[1] = 0.0;

mglData ay; ay.Create(cols, rows); ay.a[0] = 1.0; ay.a[1] = 0.0;

Then gr->Vect(x, y, ax, ay, "<"); puts this message at the console:

`MathGL message - Vect: data dimension(s) is too small`

In this code we have set Point P1: x.a[0] = 1.0; and y.a[0] = 1.0;.

The vector V1 should be represented by: ax.a[0] = 1.0; and ay.a[0] = 1.0;.

The resulting graph display an arrow witch start at P1(1,1) but doesn’t end at P2(2,2).

Who can help me with this problem?

Greetings,

Thomas

Answer

For my purpose I found a solution. I draw the “vectors” by hand. So I use only this code for drawing a vector:

```
mglGraph gr;
gr.Line(1,1, 2,2), "BA_"); // a vector from (1,1) to (2,2)
```

Best wishes,

Thomas

Source (Stackoverflow)