Since you're using rectangles you just can cover right-angled areas. So you have to leave some area of polygons uncovered. You have to reduce the actual length of edges like this:
In this solution you can reduce length of edges just form heads which meet an acute angle. With Alpha
as the inner angle of corner, the reduction amount would be Rectangle.Width / Tan(Alpha)
. It keeps rectangles inside polygon but wont solve the overlap problem.
To prevent rectangles from overlapping you have to leave more area uncovered:
To do this you have to reduce the length of edge form both heads by Rectangle.Width / Tan(Alpha / 2)
(Alpha
may differ at each corner).
But all of these wont help since width of rectangle is unknown. In fact width of rectangle is calculated by length of edge itself, and we are using it to calculate the length of edge!!
So we have a mathematical problem here:
Known:
L1: actual length of edge
N: number of divisions
Alpha & Beta: half of inner angle of head corners
Unknown:
L2: reduced length of edge
W: width of rectangles
We can form two equations:
(1): L2 = 2 * N * W
(2): L2 = L1 - (W / Tan(Alpha) - (W / Tan(Beta)
By solving them we can find W:
W = L1 / (2 * N + Cot(Alpha) + Cot(Beta))
All these are true in convex polygons. If either head corners of edge are concave you can replace their inner angle with Pi / 2
to prevent reduction from those corners.
Please recheck everything! Considering your lovely UI, I suppose you can handle trigonometric calculations for finding start and end point of rectangles.