Java - 将图像分割成拼图

时间:2021-02-22 21:39:11

I need to create a jigsaw puzzle game. I've already done this in the past using AndEngine, however I've only cut texture into rectangles. Now I need to cut it into proper jigsaw pieces. How can I do that?

我需要创建一个拼图游戏。我以前已经使用AndEngine完成了这项工作,但是我只将纹理切割成矩形。现在我需要把它切成适当的拼图。我怎样才能做到这一点?

1 个解决方案

#1


0  

Cut the texture into rectangles but for every rectangle take extra space. So you would have a lot of rectangles which overlap each other. Then you need to have some set of patterns for jigsaw edges (black and white images or you can call it a mask) and generate a mask for every rectangle using those patterns. The algorithm would be: create a mask with a size of rectangle and initialise it with white color. Then choose edge pattens based on rectangle-neighbors if they are initialised or choose edges randomly if neighbors are not yet initialize. After you chose the patterns, draw them on a mask for every side. So in the end you would have a mask with a shape of a jigsaw piece. white color = visible, black color - transparent. Then apply the mask to the rectangle when you draw it. And bare in mind that you don't stack these rectangles based on their actual size, but stack in a way that they would overlay each other...

将纹理切割成矩形,但每个矩形都需要额外的空间。所以你会有很多相互重叠的矩形。然后你需要为拼图边缘(黑白图像或者你可以称之为遮罩)设置一些模式,并使用这些模式为每个矩形生成遮罩。算法将是:创建一个矩形大小的掩码,并用白色初始化它。然后如果初始化则选择基于矩形邻居的边缘模式,或者如果邻居尚未初始化则选择边缘随机选择边缘。选择图案后,在每个面的面具上绘制它们。所以最后你会得到一个具有拼图形状的面具。白色=可见,黑色 - 透明。然后在绘制时将蒙版应用于矩形。并且请记住,您不会根据实际大小堆叠这些矩形,而是以相互叠加的方式堆叠...

P.S. I hope you understood what I was trying to say. Sorry, English is not my native language...

附:我希望你明白我想说的话。对不起,英语不是我的母语......

#1


0  

Cut the texture into rectangles but for every rectangle take extra space. So you would have a lot of rectangles which overlap each other. Then you need to have some set of patterns for jigsaw edges (black and white images or you can call it a mask) and generate a mask for every rectangle using those patterns. The algorithm would be: create a mask with a size of rectangle and initialise it with white color. Then choose edge pattens based on rectangle-neighbors if they are initialised or choose edges randomly if neighbors are not yet initialize. After you chose the patterns, draw them on a mask for every side. So in the end you would have a mask with a shape of a jigsaw piece. white color = visible, black color - transparent. Then apply the mask to the rectangle when you draw it. And bare in mind that you don't stack these rectangles based on their actual size, but stack in a way that they would overlay each other...

将纹理切割成矩形,但每个矩形都需要额外的空间。所以你会有很多相互重叠的矩形。然后你需要为拼图边缘(黑白图像或者你可以称之为遮罩)设置一些模式,并使用这些模式为每个矩形生成遮罩。算法将是:创建一个矩形大小的掩码,并用白色初始化它。然后如果初始化则选择基于矩形邻居的边缘模式,或者如果邻居尚未初始化则选择边缘随机选择边缘。选择图案后,在每个面的面具上绘制它们。所以最后你会得到一个具有拼图形状的面具。白色=可见,黑色 - 透明。然后在绘制时将蒙版应用于矩形。并且请记住,您不会根据实际大小堆叠这些矩形,而是以相互叠加的方式堆叠...

P.S. I hope you understood what I was trying to say. Sorry, English is not my native language...

附:我希望你明白我想说的话。对不起,英语不是我的母语......