说,我有一个游戏对象的精灵,它是一个具有透明度的png
图像。从透明PNG精灵制作多边形的算法
我想从这个图像,这将包含我的游戏对象创建多边形。
我敢肯定有它现有的算法,但我还没有发现任何。
我希望是这样的:
public static Polygon getPolygon(BufferedImage sprite)
{
// get coordinates of all points for polygon
return polygon;
}
说,我有一个游戏对象的精灵,它是一个具有透明度的png
图像。从透明PNG精灵制作多边形的算法
我想从这个图像,这将包含我的游戏对象创建多边形。
我敢肯定有它现有的算法,但我还没有发现任何。
我希望是这样的:
public static Polygon getPolygon(BufferedImage sprite)
{
// get coordinates of all points for polygon
return polygon;
}
见this question。它将会缓慢,但它取决于你想要多准确(第二个答案是sloppier,但更快一点)。你从getOutline()
得到Area
对其他问题后,尝试使用此代码(未经测试):
public static Polygon getPolygonOutline(BufferedImage image) {
Area a = getOutline(image, new Color(0, 0, 0, 0), false, 10); // 10 or whatever color tolerance you want
Polygon p = new Polygon();
FlatteningPathIterator fpi = new FlatteningPathIterator(a.getPathIterator(null), 0.1); // 0.1 or how sloppy you want it
double[] pts = new double[6];
while (!fpi.isDone()) {
switch (fpi.currentSegment(pts)) {
case FlatteningPathIterator.SEG_MOVETO:
case FlatteningPathIterator.SEG_LINETO:
p.addPoint((int) pts[0], (int) pts[1]);
break;
}
fpi.next();
}
return p;
}
谢谢!这正是我想要的。 – 2013-03-11 16:34:33
你知道在JavaScript中是否有相同的东西?有一个图像png =>获取多边形 – 2014-03-27 12:00:07
这取决于你想如何准确它是。为了100%的准确性,这将是非常昂贵的,可以填补超过1000分。这可以通过填充填充或通过颜色分析沿边界移动的系列来完成。 – 2013-03-11 15:45:00
@传奇人物谢谢!我需要精确度和效率之间的一个中间值。我将阅读关于洪水填充算法。 – 2013-03-11 15:52:48