帮助我理解 Java 多边形填充的奇怪之处

发布于 2024-09-12 08:20:03 字数 1396 浏览 1 评论 0原文

我正在根据给定的线绘制多边形。我的逻辑运行良好,除非多边形看起来与自身相交。然而,它似乎并不是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千仐 2024-09-19 08:20:03
  1. 该规则适用于每个多边形。 Java 并不关心你之前绘制的多边形,哪怕只是一条语句。
  2. 您似乎有点误解了奇偶规则。该规则的实际版本有点像这样......对于多边形交叉的每个“y”坐标,都有一个它交叉的所有 x 坐标的有序列表。多边形的“内部”是从每个偶数索引(0、2、4...)到下一个奇数索引的部分。
  1. The rule applies per polygon. Java doesn't care about the polygon you drew even one statement ago.
  2. You seem to be misunderstanding the even-odd rule a bit. The practical version of the rule goes a bit like this...for each 'y' coordinate the polygon crosses, there's an ordered list of all the x coordinates where it crosses. The 'inside' of the polygon is the sections from each even-numbered index (0, 2, 4...) to the next odd-numbered index.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文