抱歉内容有些长...但至少有图片吧?
我编写了一个算法,通过统计生成N个凸多边形来创建一幅马赛克图像,这些多边形覆盖整个图像且不重叠。这些多边形的边数在3-8之间,每条边的角度都是45度的倍数。这些多边形在内部被存储为矩形,并记录每个角落的位移。下面是一张图片,解释了这个算法的工作原理:
getRight()
返回x + width - 1
,getBottom()
返回y + height - 1
。该类旨在维护填充像素周围的紧密边界框,因此图像中显示的坐标是正确的。请注意,width >= ul + ur + 1
,width >= ll + lr + 1
,height >= ul + ll + 1
,height >= ur + ul + 1
,否则将在一侧出现空像素。还要注意,角落的位移可能为0,从而指示该角落中的所有像素都已填充。这使得该表示可以存储3-8边凸多边形,每个边至少有一个像素的长度。虽然将这些区域进行数学表示很好,但我想绘制它们以便能够看到它们。使用一个简单的lambda和一种迭代每个多边形像素的方法,我可以完美地渲染图像。例如,下面是使用99个允许所有分裂方向的多边形绘制的克洛德·莫奈的戴遮阳伞的女人。
渲染此图像的代码如下:
public void drawOnto(Graphics graphics) {
graphics.setColor(getColor());
forEach(
(i, j) -> {
graphics.fillRect(x + i, y + j, 1, 1);
}
);
}
private void forEach(PerPixel algorithm) {
for (int j = 0; j < height; ++j) {
int nj = height - 1 - j;
int minX;
if (j < ul) {
minX = ul - j;
} else if (nj < ll) {
minX = ll - nj;
} else {
minX = 0;
}
int maxX = width;
if (j < ur) {
maxX -= ur - j;
} else if (nj < lr) {
maxX -= lr - nj;
}
for (int i = minX; i < maxX; ++i) {
algorithm.perform(i, j);
}
}
}
然而,出于许多原因,这并不是理想的。首先,图形化表示多边形的概念现在已经成为类本身的一部分;最好允许其他专注于表示这些多边形的类存在。其次,这需要许多、许多次调用
fillRect()
来绘制单个像素。最后,我希望能够开发其他渲染这些多边形的方法,而不是直接绘制它们(例如,在由多边形中心表示的Voronoi图上执行加权插值)。所有这些都指向生成一个代表多边形顶点的
java.awt.Polygon
(我将其命名为Region
以区分于Polygon
类)。没问题;我编写了一个方法来生成一个Polygon
,它具有上面的角落,并且没有重复,以处理位移为0或一侧只有一个像素的情况:public Polygon getPolygon() {
int[] xes = {
x + ul,
getRight() - ur,
getRight(),
getRight(),
getRight() - lr,
x + ll,
x,
x
};
int[] yes = {
y,
y,
y + ur,
getBottom() - lr,
getBottom(),
getBottom(),
getBottom() - ll,
y + ul
};
int[] keptXes = new int[8];
int[] keptYes = new int[8];
int length = 0;
for (int i = 0; i < 8; ++i) {
if (
length == 0 ||
keptXes[length - 1] != xes[i] ||
keptYes[length - 1] != yes[i]
) {
keptXes[length] = xes[i];
keptYes[length] = yes[i];
length++;
}
}
return new Polygon(keptXes, keptYes, length);
}
问题在于,当我尝试使用
Polygon
和Graphics.fillPolygon()
方法时,它没有填充所有的像素!下面是使用这种不同方法呈现的相同马赛克:
所以我有几个关于这种行为的相关问题:
为什么
Polygon
类没有填充所有这些像素,即使角度是45度的简单倍数?如何在我的渲染器中一贯地编写代码以解决这个缺陷(就我的应用程序而言),以便我可以使用我的
getPolygon()
方法?我不想改变它输出的顶点,因为我需要它们精确进行重心计算。
MCE
如果上面的代码片段和图片无法充分解释问题,我已经添加了一个最小、完整和可验证的示例,演示了我所描述的行为。
package com.sadakatsu.mce;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Main {
@FunctionalInterface
private static interface PerPixel {
void perform(int x, int y);
}
private static class Region {
private int height;
private int ll;
private int lr;
private int width;
private int ul;
private int ur;
private int x;
private int y;
public Region(
int x,
int y,
int width,
int height,
int ul,
int ur,
int ll,
int lr
) {
if (
width < 0 || width <= ll + lr || width <= ul + ur ||
height < 0 || height <= ul + ll || height <= ur + lr ||
ul < 0 ||
ur < 0 ||
ll < 0 ||
lr < 0
) {
throw new IllegalArgumentException();
}
this.height = height;
this.ll = ll;
this.lr = lr;
this.width = width;
this.ul = ul;
this.ur = ur;
this.x = x;
this.y = y;
}
public Color getColor() {
return Color.BLACK;
}
public int getBottom() {
return y + height - 1;
}
public int getRight() {
return x + width - 1;
}
public Polygon getPolygon() {
int[] xes = {
x + ul,
getRight() - ur,
getRight(),
getRight(),
getRight() - lr,
x + ll,
x,
x
};
int[] yes = {
y,
y,
y + ur,
getBottom() - lr,
getBottom(),
getBottom(),
getBottom() - ll,
y + ul
};
int[] keptXes = new int[8];
int[] keptYes = new int[8];
int length = 0;
for (int i = 0; i < 8; ++i) {
if (
length == 0 ||
keptXes[length - 1] != xes[i] ||
keptYes[length - 1] != yes[i]
) {
keptXes[length] = xes[i];
keptYes[length] = yes[i];
length++;
}
}
return new Polygon(keptXes, keptYes, length);
}
public void drawOnto(Graphics graphics) {
graphics.setColor(getColor());
forEach(
(i, j) -> {
graphics.fillRect(x + i, y + j, 1, 1);
}
);
}
private void forEach(PerPixel algorithm) {
for (int j = 0; j < height; ++j) {
int nj = height - 1 - j;
int minX;
if (j < ul) {
minX = ul - j;
} else if (nj < ll) {
minX = ll - nj;
} else {
minX = 0;
}
int maxX = width;
if (j < ur) {
maxX -= ur - j;
} else if (nj < lr) {
maxX -= lr - nj;
}
for (int i = minX; i < maxX; ++i) {
algorithm.perform(i, j);
}
}
}
}
public static void main(String[] args) throws IOException {
int width = 10;
int height = 8;
Region region = new Region(0, 0, 10, 8, 2, 3, 4, 1);
BufferedImage image = new BufferedImage(
width,
height,
BufferedImage.TYPE_3BYTE_BGR
);
Graphics graphics = image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
region.drawOnto(graphics);
ImageIO.write(image, "PNG", new File("expected.png"));
image = new BufferedImage(
width,
height,
BufferedImage.TYPE_3BYTE_BGR
);
graphics = image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
graphics.setColor(Color.BLACK);
graphics.fillPolygon(region.getPolygon());
ImageIO.write(image, "PNG", new File("got.png"));
}
}