Reputation: 83
I have written my own poly-line class that essentially keeps a list of points that can be modified using a Matrix. The poly lines can be added to other poly lines and joined at common endpoints. The poly line will represent a polygon when the end point equals the starting point.
I then have a method to turn my polygon into a Path object by iterating the list of points with a series of lineTo() calls. This path is then applied to a canvas as a clip path in my View's onDraw method.
It works perfect for complex polygons and I can draw that path to verify it's accuracy.
So far so good, except that I'm noticing issues when I have a compound polygon with an irregular hole in the middle. I should probably stop calling it a polygon at this point since it is a polygon inside another polygon.
For example consider the diagram below where the outer box and "castle" looking shape in the middle are both parts of the same Path object that is used as a clip-path. The # represent painted area.
+---------+
|#########|
|#+-+#+-+#|
|#| |#| |#|
|#| +-+ |#|
|#| |#|
|#+-----+#|
|#########|
+---------+
I expect that everything outside of the outer box and inside the inner "castle" shape to be clipped. The issue I'm seeing is that the inside shape isn't being clipped, properly. Seems to be an issue with the ray-tracing algorithm.
Any ideas would be helpful.
EDIT: Also, I tried testing every Region.Op mode, and none of them solved the problem. I suspect I'll need to put measures in place to detect if there is a "hole" and do something creative.
Upvotes: 1
Views: 2430
Reputation: 83
After spending a couple of day's playing with this I've half-solved my problem.
I needed to set the Path.FillType with:
path.setFillType(Path.FillType.EVEN_ODD)
But then I had an instance where the opposite happened and only the center path was painted. A little more investigation and I was able to fix that with adding:
canvas.clipPath(path, Region.Op.DIFFERENCE);
But then polygons with a single path on the outside have their clip inverted. While I'm satisified that I've found the right nerd-knobs to get the right clipping behavior, I haven't found a way to determine which clipping methods are needed.
I'd be happy if someone has any ideas to share. I suspect it has something to do with the order in which lines are added to the path, such as if the inner is defined beore the outer, etc.
Upvotes: 2