Here you can find the source of subdivideRectangle(int width, int height, int numTilesX, int numTilesY, int extraBorder)
Parameter | Description |
---|---|
width | the rectangle's width |
height | the rectangle's height |
numTilesX | the number of tiles in X direction |
numTilesY | the number of tiles in Y direction |
extraBorder | an extra border size to extend each tile |
public static Rectangle[] subdivideRectangle(int width, int height, int numTilesX, int numTilesY, int extraBorder)
//package com.java2s; /*//from ww w . j a v a 2 s. com * Copyright (C) 2010 Brockmann Consult GmbH (info@brockmann-consult.de) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 3 of the License, or (at your option) * any later version. * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, see http://www.gnu.org/licenses/ */ import java.awt.Rectangle; public class Main { /** * Subdivides a rectangle into tiles. The coordinates of each returned tile rectangle are guaranteed * to be within the given rectangle. * * @param width the rectangle's width * @param height the rectangle's height * @param numTilesX the number of tiles in X direction * @param numTilesY the number of tiles in Y direction * @param extraBorder an extra border size to extend each tile * * @return the tile coordinates as rectangles */ public static Rectangle[] subdivideRectangle(int width, int height, int numTilesX, int numTilesY, int extraBorder) { Rectangle[] rectangles = new Rectangle[numTilesX * numTilesY]; int k = 0; float w = (float) width / numTilesX; float h = (float) height / numTilesY; for (int j = 0; j < numTilesY; j++) { int y1 = (int) Math.floor((j + 0) * h); int y2 = (int) Math.floor((j + 1) * h) - 1; if (y2 < y1) { y2 = y1; } y1 -= extraBorder; y2 += extraBorder; if (y1 < 0) { y1 = 0; } if (y2 > height - 1) { y2 = height - 1; } for (int i = 0; i < numTilesX; i++) { int x1 = (int) Math.floor((i + 0) * w); int x2 = (int) Math.floor((i + 1) * w) - 1; if (x2 < x1) { x2 = x1; } x1 -= extraBorder; x2 += extraBorder; if (x1 < 0) { x1 = 0; } if (x2 > width - 1) { x2 = width - 1; } rectangles[k] = new Rectangle(x1, y1, (x2 - x1) + 1, (y2 - y1) + 1); k++; } } return rectangles; } }