I am currently using gdextension with Godot 4.3-stable

I am basically making a simple 2D graph which plots data (Vector2 type) but I have been getting a weird issue with the line width when populating my data with draw_polyline compared to draw_line

Essentially, my draw_plot function is just the following:

void Graph_2D::_draw_plot() {

  // Ensure the range in the display frame is within range
  _data1.set_range();
  if (_data1.packed_v2_data.is_empty()) {
    return;
  }
  
  PackedVector2Array data = _coordinate_to_pixel(_data1.packed_v2_data);
  // Enable anti-aliasing for better resolution
  draw_polyline(data, _data1.color, _data1.width, true);
  for (size_t i = 0; i < data.size(); i++) {
    draw_circle(data[i], 5.0, _data1.color);
  }
}

with the following implementation, I am getting the following output:

You can see how some of the line width are not constant, but as soon as I change the same data to use draw_line, it is a lot better (see below for the necessary changes). Here is the output of the following changes.

// Swap draw_polyline with this line of code
  for (size_t i = 0; i < data.size() - 1; i++) {
    draw_line(data[i], data[i + 1], _data1.color, _data1.width, true);
  }

I was wondering if I am doing something wrong here, or there may be some parameters that I am not aware of. Any help would be much appreciated.

  • xyz replied to this.

    xyz I've tried it before, but unfortunately, in the documentation, it says

    Draws multiple disconnected lines with a uniform width and color. Each line is defined by two consecutive points from points array

    This causes the plot to be like so, as it creates disconnected lines not good for my application.

    You can see in this gif what happens when multiline is used.

    Here is the expected behaviour

    • xyz replied to this.

      ShenOwlsHoot Well, insert the additional points in the list. It'll still be faster than drawing individual lines because you don't have to iterate each time you draw. Or just keep the draw_line() version if it does what you want.

        xyz Fair enough.

        I took a look at the source code of add_multiline in the source code, and it seems it is by itself doing a for loop as well.

        I think adding the disconnected lines may as well use up more resources than simply using draw_line, so I'll probably stick with draw_line. Thanks for the help though!

        • xyz replied to this.

          ShenOwlsHoot I took a look at the source code of add_multiline in the source code, and it seems it is by itself doing a for loop as well.

          Yes but native code loop is order of magnitude faster than a script loop.

            xyz Ahh, is that so. I may try to benchmark the performance at some point if I have time. Hopefully, I'll remember to update this thread so others can potentially benefit.

            • xyz replied to this.

              ShenOwlsHoot It is but it probably wouldn't noticeably affect performance for a small number of points you've shown in your screenshot.