How to triangulate a polyline with thickness
jvernay.fr
jvernay.fr
I notice you use 4 triangles per segment but I believe it's always possible to use only 2 if you only have miter and bevel caps.
But it is actually way more complicated than that in the general case. Notice how there is some overlap, not very pretty with transparency. There are ways around this problem, for example using a stencil buffer, but if you don't want that either and instead go a proper triangulation, this becomes quite involved.
The problem is when segments are shorter than the inner intersection point. In the example in the article, that's when I1 is outside of [A1, A1'] or [B1, B1']. Doing it properly would require taking the geometry of the entire figure into account, you can't just draw it segment by segment and fix the joints.
For example, if you draw a closed polygon that is smaller than the line thickness, all internal geometry will disappear so you have to triangulate the shape using only the outer vertices. How to calculate that looks like an interesting problem that would need a much longer blog post...
In my context the line represents a player path so the transparency overlap created by the bevel intersection is actually perfectly fine and looks good. The same for intersections over a previous section of the path.
I might go back and add the pixel buffer method anyway so the rounded curves can be both smooth and non intersecting.
Aside from that, for the sake of discussion and playing devil’s advocate I’ll take a small pedantic issue with the very first sentence. There are multiple ways to do this kind of thing without triangulating. (And I’m currently working on making one available publicly.) Clever shaders are one way to render lines without triangles. Ray tracing is another. Triangles are useful and easy and have broad support, so they make sense, but they aren’t the only option.
tell me how compiling using gcc simple one line