MathGL draws intermittent line

16 views
Skip to first unread message

Denis Mihailovich

unread,
Feb 8, 2026, 2:06:19 PMFeb 8
to MathGL
This function draws a graph of a function in the memory:

    GetShape()->draw_func =
        [&](const Rect& r)
        {
            graph.NewFrame();
            graph.SetFlagAdv(1, MGL_NO_SCALE_REL);
            graph.SetScaleText(false);
            graph.SetSize(r.width, r.height, false);
            graph.SubPlot(1, 1, 0, "#");
            graph.InPlot(0.05, 0.95, 0.05, 0.95);

            graph.SetRanges(x_left, x_right, y_bottom, y_top);
            graph.SetFontSize(level);
            std::string f = "{" + format.color.ToRGB() + "}";
            graph.Axis("xy", std::string(f + "-1").c_str(), "h-1");
            if (format.grid_width > 0)
                graph.Grid("xy", std::string("h" + std::to_string(format.grid_width) + f).c_str());
            graph.SetQuality(MGL_DRAW_NORM);

            for (const Plot& p : plots)
            {
                auto& x = p.x;
                auto& y = p.y;
                if (!x.empty() && !y.empty())
                {
                    mglData x_data(x.size());
                    mglData y_data(y.size());
                    x_data.Set(x);
                    y_data.Set(y);
                    f = "{" + p.format.color.ToRGB() + "}-" + std::to_string(p.format.width);
                    graph.Plot(x_data, y_data, f.c_str());
                }
            }

            const unsigned char* picture = graph.GetRGBA();
            std::vector<unsigned char> arr(picture, picture + 4 * (graph.GetWidth() * graph.GetHeight()));
            window->DrawImage(r.left + 1, r.top + 1, r.width, r.height, arr);
        };

I get such a graph:
2026-02-08_22-01.png
As you see the line is not very beautiful. How can I resolve it? I tried to set quality to MGL_DRAW_LMEM. The result is much better, but the grid is not shown. Is this issue about antialiasing? How can I turn it on or off? If width of graph >= 2, the line is solid, but I need to draw it in width = 1 also.

Alexey Balakin

unread,
Feb 19, 2026, 3:33:44 AMFeb 19
to MathGL
Hi,

The problem is caused by too narrow line. There are 2 possible solutions. First, you can just increase the line width. Second one is changing 'pendelta' parameter (see https://mathgl.sourceforge.net/doc_en/Default-sizes.html#pendelta ). Note, that pendelta parameter will blur all lines and text (for later primitives if quality=6 or for whole picture else).

The sample code and the image below.
#######################

quality 6

subplot 2 2 0 '<_'

ranges 0 10 0 1

axis:box:fplot 'abs(sin(x))' 'r'


subplot 2 2 1 '<_'

ranges 0 10 0 1

axis:box:fplot 'abs(sin(x))' 'r2'


pendelta 0.7

subplot 2 2 2 '<_'

ranges 0 10 0 1

axis:box:fplot 'abs(sin(x))' 'r'


pendelta 0.5

subplot 2 2 3 '<_'

ranges 0 10 0 1

axis:box:fplot 'abs(sin(x))' 'r'

#######################

sample.png

воскресенье, 8 февраля 2026 г. в 22:06:19 UTC+3, dgor...@gmail.com:
Reply all
Reply to author
Forward
0 new messages