I am working with drawing polygons based on a given line. I have the logic working out well except in cases where it appears that the polygon intersects itself. However, it doesn't seem to be 100% consistent, nor does it make sense based on what I'm reading. Below are two images created using the same code. The yellow polygons are the ones I'm concerned with.
Image: http://i31.tinypic.com/24cxxlf.png
I want every case to work like the first case (where the empty area "wrapped" by the polygon is not filled in).
These images are produced by this code:
BufferedImage drawingImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = drawingImage.createGraphics();
Polygon polygon = new Polygon(parsedPoints[0], parsedPoints[1], parsedPoints[0].length);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g.setColor(drawingColor);
g.fillPolygon(polygon);
float[] scales = {1f, 1f, 1f, 0.7f};
float[] offsets = new float[4];
RescaleOp rop = new RescaleOp(scales, offsets, null);
graphics.drawImage(drawingImage, rop, 0, 0);
graphics.setStroke(new BasicStroke(2));
graphics.setColor(drawingColor);
graphics.drawPolygon(polygon);
(I'm filling the polygon applying a rescale to get some transparency to the fill, and then drawing the border without transparency.)
According to the Java documentation for the Graphics.fillPolygon method:
The area inside the polygon is defined using an even-odd fill rule, also known as the alternating rule.
If I understand that correctly, then in both cases a pixel contained within the area "wrapped" by the thick polygon would cross exactly two paths, so it would be considered "outside" the polygon.
So my questions are: (a) am I understanding the even-odd fill rule and (b) is there a way in Java to make the second image work more like the first?
Any thoughts on this would be greatly appreciated.
Thanks.