I need to draw lines onto a bitmap using a custom aperture. In the example below, there are two lines. The left line is horizontal. The right line is diagonal - down and to the right:
In the example image, the aperture and the path is shown in red. The resulting geometry is shown in black. Both of the above examples use the same aperture, but wipe it along a different path.
I'd like to be able to draw with an arbitrary shaped pen, even though the above example only shows rectangles.
I made an attempt at using the System.Drawing.2D namespace but it doesn't seem to do what I need it to do. Using custom end-caps on pens seemed promising, but the end caps rotate with the direction of the line. Also, getting the line-width right seems challenging.
I've considered drawing the aperture over and over again, centered at different points along the path, but that doesn't seem performant. It is also quite difficult to minimize the number of aperture draws.
The best idea I've got is to try to draw the 'line' as a filled shape. At first, I thought a convex hull algorithm would be the answer - simply take the vertices of the aperture at the beginning of the draw and at the end of the draw and put them through a convex hull algorithm to find the 'outer' vertices. This works for my 1st example above, - but a star-shaped aperture demonstrates that this solution is incomplete. It only works when the aperture is convex itself.

Simply putting all of the vertices through a convex hull algorithm would result in the areas highlighted in blue being filled in, but I need to only fill in the black areas.
This is an ugly, inelegant solution, but you could take the vector that represents the stroke direction and length and redraw your shape many times along that vector. (You'd have to choose an interval at which to draw it that didn't leave gaps.) This would only produce correct results if you want aliased output; it would screw up any anti-aliasing done by GDI+, because pixels with midtones would potentially be drawn over, making them darker than they should be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With