Here you can find the source of randomRectangle(int minW, int maxW, int minH, int maxH)
Parameter | Description |
---|---|
minW | a parameter |
maxW | a parameter |
minH | a parameter |
maxH | a parameter |
public static Rectangle randomRectangle(int minW, int maxW, int minH, int maxH)
//package com.java2s; /*/* w w w .j a v a 2 s . c o m*/ * Neon, a roguelike engine. * Copyright (C) 2013 - Maarten Driesen * * 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 { /** * Returns a rectangle with the given min/max width and height. * * @param minW * @param maxW * @param minH * @param maxH * @return a random Rectangle with its origin a (0,0) */ public static Rectangle randomRectangle(int minW, int maxW, int minH, int maxH) { int w = random(minW, maxW); int h = random(minH, maxH); return new Rectangle(w, h); } /** * Returns a rectangle with the given min/max dimensions and a maximum width/height or height/width ratio. * * @param minW * @param maxW * @param minH * @param maxH * @param ratio * @return a random Rectangle with its origin at (0,0) */ public static Rectangle randomRectangle(int minW, int maxW, int minH, int maxH, double ratio) { int w = random(minW, maxW); int hMin = Math.max(minH, (int) (w / ratio)); int hMax = Math.min(maxH, (int) (w * ratio)); int h = random(hMin, hMax); return new Rectangle(w, h); } /** * Generates a random rectangle within the given rectangle. * * @param minW * @param bounds * @return a @code{Rectangle} */ public static Rectangle randomRectangle(int minW, Rectangle bounds) { int w = random(minW, bounds.width - 1); int h = random(minW, bounds.height - 1); Rectangle rec = new Rectangle(w, h); while (!bounds.contains(rec)) { rec.x = random(bounds.x, bounds.x + bounds.width - w); rec.y = random(bounds.y, bounds.y + bounds.height - h); } return rec; } /** * @param min * @param max * @return a random int between min and max (min and max included) */ public static int random(int min, int max) { return (int) (min + (max - min + 1) * Math.random()); } }