从透明png精灵制作多边形的算法

说,我有一个游戏对象的精灵,它是一个透明的png图像。

宇宙飞船精灵

我想从这个图像创建一个包含我的游戏对象的多边形。

太空飞船多边形

我很确定它有一个现有的算法,但我还没有找到。

我希望有类似的东西:

 public static Polygon getPolygon(BufferedImage sprite) { // get coordinates of all points for polygon return polygon; } 

看到这个问题 。 它很慢,但这取决于你想要它的准确度(第二个答案比较粗,但速度要快一些)。 在另一个问题上从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; }