Java将曲面划分为小正方形
我想知道是否有这样的算法: 给定一个特定的表面,它将其划分为大小相同的较小矩形 类似于此示例图: 灰色区域是曲面,红色正方形是分区本身 我在想,是否有一个优化的方法来做到这一点 一种非常糟糕的方法是在所有像素中使用for循环,并检查该特定点是否有矩形,如果没有,将创建一个矩形,依此类推 也许有人知道一个已经完成的算法?还是更好的解决方案Java将曲面划分为小正方形,java,image,graphics,rectangles,partition,Java,Image,Graphics,Rectangles,Partition,我想知道是否有这样的算法: 给定一个特定的表面,它将其划分为大小相同的较小矩形 类似于此示例图: 灰色区域是曲面,红色正方形是分区本身 我在想,是否有一个优化的方法来做到这一点 一种非常糟糕的方法是在所有像素中使用for循环,并检查该特定点是否有矩形,如果没有,将创建一个矩形,依此类推 也许有人知道一个已经完成的算法?还是更好的解决方案 提前多谢;) 您是只想用正方形填充它,还是想用最佳数量的正方形填充它 第二种算法更难 对于第一步,只需一次通过一个正方形大小的图像。如果该点的像素已填充,则扫
提前多谢;) 您是只想用正方形填充它,还是想用最佳数量的正方形填充它 第二种算法更难 对于第一步,只需一次通过一个正方形大小的图像。如果该点的像素已填充,则扫描整个正方形,如果全部填充,则绘制正方形。如果没有,则转到下一点 i、 e.如果正方形为10*10像素:
for (int x=0;x<width;x+=SQUARE_SIZE) {
for (int y=0;y<height;y+=SQUARE_SIZE) {
// Now check if you can put a valid square here, if so draw it
}
}
for(intx=0;x这里有一种方法
创建图像的遮罩。(我刚刚使用Photoshop)
窃取AndrewThompson的代码,并使用它创建图像的区域
Area imageArea = getOutline(Color.BLACK, imageMask);
Rectangle2D[][] grid = new Rectangle2D[rows][cols];
for (int i = 0; i < grid.length; i++) {
int y = i * CELL_SIZE;
for (int j = 0; j < grid[i].length; j++) {
int x = j * CELL_SIZE;
grid[i][j] = new Rectangle2D.Double(x, y, cellSize, cellSize);
}
}
为整个图像创建栅格矩形2D
对象
Area imageArea = getOutline(Color.BLACK, imageMask);
Rectangle2D[][] grid = new Rectangle2D[rows][cols];
for (int i = 0; i < grid.length; i++) {
int y = i * CELL_SIZE;
for (int j = 0; j < grid[i].length; j++) {
int x = j * CELL_SIZE;
grid[i][j] = new Rectangle2D.Double(x, y, cellSize, cellSize);
}
}
完整示例
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Area;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class SquaresInArea extends JPanel {
private static final int CELL_SIZE = 30;
BufferedImage image;
BufferedImage imageMask;
Area imageArea;
Rectangle2D[][] imageGrid;
public SquaresInArea() {
try {
image = ImageIO.read(getClass().getResource("/resources/floorplan.png"));
imageMask = ImageIO.read(getClass().getResource("/resources/floorplan-black.png"));
} catch (IOException ex) {
Logger.getLogger(SquaresInArea.class.getName()).log(Level.SEVERE, null, ex);
}
imageArea = getOutline(Color.BLACK, imageMask);
imageGrid = createGrid();
}
private Rectangle2D[][] createGrid() {
int width = image.getWidth();
int height = image.getHeight();
int rows = height / CELL_SIZE;
int cols = width / CELL_SIZE;
Rectangle2D[][] grid = new Rectangle2D[rows][cols];
for (int i = 0; i < grid.length; i++) {
int y = i * CELL_SIZE;
for (int j = 0; j < grid[i].length; j++) {
int x = j * CELL_SIZE;
grid[i][j] = new Rectangle2D.Double(x, y, CELL_SIZE, CELL_SIZE);
}
}
return grid;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(image, 0, 0, this);
g2.setColor(Color.YELLOW);
g2.setStroke(new BasicStroke(3f));
for (Rectangle2D[] rects : imageGrid) {
for (Rectangle2D rect : rects) {
if (imageArea.contains(rect)) {
g2.drawRect((int) rect.getX(), (int) rect.getY(),
(int) rect.getWidth(), (int) rect.getHeight());
}
}
}
}
@Override
public Dimension getPreferredSize() {
return image == null ? new Dimension(300, 300)
: new Dimension(image.getWidth(), image.getHeight());
}
private Area getOutline(Color target, BufferedImage bi) {
// construct the GeneralPath
GeneralPath gp = new GeneralPath();
boolean cont = false;
int targetRGB = target.getRGB();
for (int xx = 0; xx < bi.getWidth(); xx++) {
for (int yy = 0; yy < bi.getHeight(); yy++) {
if (bi.getRGB(xx, yy) == targetRGB) {
if (cont) {
gp.lineTo(xx, yy);
gp.lineTo(xx, yy + 1);
gp.lineTo(xx + 1, yy + 1);
gp.lineTo(xx + 1, yy);
gp.lineTo(xx, yy);
} else {
gp.moveTo(xx, yy);
}
cont = true;
} else {
cont = false;
}
}
cont = false;
}
gp.closePath();
// construct the Area from the GP & return it
return new Area(gp);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new SquaresInArea());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
感谢您的回复,您所说的最佳正方形数是什么意思?我想在给定正方形的特定宽度和高度的情况下,将曲面划分为可能的最大正方形数。正确定位正方形可能意味着您适合不同的数字。例如,如果一个空间的宽度为11个单位,那么您可以适合两个5个单位的正方形如果从0或1开始,则宽度为,但如果从2开始,则只能容纳一个正方形。非常感谢您的回答,这将帮助我;)