What’s the best way to draw a vertical line with a thickness greater than 1?
I currently use drawLine and pass 2 as the thickness, but I read that it’s a lot faster to use drawVerticalLine or fillRectangle. However, drawVerticalLine does not have a thickness parameter I can use, so should I use fillRectangle? If so, is simply setting a width of 2 for fillRectangle the same as drawing a line with a thickness of 2, or do I also need to offset the x position to center the rectangle vertically about the desired x position? Or…?
float top = 420.f, bottom = 420.f;
int x = 420;
int thicc = 3;
g.drawVerticalLine(x, top, bottom);
for(auto i = 1; i < thicc; ++i){
g.drawVerticalLine(x + i, top, bottom);
g.drawVerticalLine(x - i, top, bottom);
}
you could also do this if you don’t want to use rectangles btw
While this may work, it only does if your physical pixels align with the “int” coordinate you give it.
The moment you add a Transform somewhere, those lines will be rendered with antialiasing, and you’ll get artifacts where the antialiasing of two adjacent lines overlaps.
I guess this would even more visible if the drawing colour is not fully opaque.
And also, the loop above is performed by the CPU, while obtaining the same effect by filling a rectangle is an operation that can be entirely handled in a GPU with a performance gain.
I can’t comment on whether or not this is true but I’d recommend to not get caught in the trap of premature optimisations.
It may take 1ms to render a line with drawLine and only 0.95ms to render the same line with some other method, but the difference is so small the only noticeable difference you’ve made is to make your code less readable.
TIP: If you’re trying to draw horizontal or vertical lines, don’t use this -
it’s better to use fillRect() instead unless you really need an angled line.
Since I’m potentially drawing a lot of vertical (and horizontal) lines while scrolling through my graph, I thought it best to take this advice.
Looks like using fillRect, with floats instead of ints, might be the best solution, when I need a specific width.
In that case, I suggest not calling fillRect directly and instead populating a RectangleList, where it is drawn instead. There’s a specialisation of Graphics::fillRectList for float rectangles.
Under the hood in that call, like in CoreGraphics, native drawing optimisations will (usually) be applied.
Alternatively, if you’re using OpenGL, you can store the data as a bunch of triangles and draw them (it’s way faster to draw this way, even though it’s more maintenance on your end).