帮助我理解 Java 多边形填充的奇怪之处
我正在根据给定的线绘制多边形。我的逻辑运行良好,除非多边形看起来与自身相交。然而,它似乎并不是 100% 一致,根据我正在阅读的内容,它也没有意义。下面是使用相同代码创建的两个图像。黄色多边形是我关心的多边形。
图片:http://i31.tinypic.com/24cxxlf.png
我希望每个案例与第一种情况类似(其中被多边形“包裹”的空白区域未被填充)。
这些图像是由以下代码生成的:(
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);
我正在填充多边形,应用重新缩放以获得填充的一些透明度,然后绘制不透明的边框。)
根据 Graphics.fillPolygon 方法的 Java 文档:
定义了多边形内部的面积 还使用奇偶填充规则 称为交替规则。
如果我理解正确,那么在这两种情况下,包含在厚多边形“包裹”区域内的像素将恰好穿过两条路径,因此它将被视为多边形“外部”。
所以我的问题是:(a)我是否理解奇偶填充规则以及(b)Java中有没有一种方法可以使第二个图像更像第一个图像?
任何对此的想法将不胜感激。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)