由透明png精灵制作多边形的算法 [英] Algorithm to make a polygon from transparent png sprite
本文介绍了由透明png精灵制作多边形的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说,我有一个游戏对象的子画面,它是具有透明度的png
图像.
Say, I have a sprite of a game object which is a png
image with transparency.
我想从此图像创建一个包含我的游戏对象的多边形.
I want to create a polygon from this image which will contain my game object.
我很确定有一种现有的算法,但是我还没有找到任何算法.
I am pretty sure there is an existing algorithm for it, but I haven't found any.
我希望这样:
public static Polygon getPolygon(BufferedImage sprite)
{
// get coordinates of all points for polygon
return polygon;
}
推荐答案
请参阅此问题.它会很慢,但要取决于您想要的精度(第二个答案更草率,但要快一些).在另一个问题上从getOutline()
获得Area
后,请尝试使用此代码(未经测试):
See this question. It will be slow, but it depends on how accurate you want it (second answer is sloppier, but a bit faster). After you get the Area
from getOutline()
on the other question, try using this code (untested):
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;
}
这篇关于由透明png精灵制作多边形的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文