Java tutorial
//package com.java2s; /** * Copyright (C) 2012-2014 GREE, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import android.graphics.Point; public class Main { /** * Find the smallest area that contains multiples rect defined by width & height * * @param width * @param height * @param num * @return */ public static Point getSmallestTextureSize(final int width, final int height, final int num, final int maxTextureSize, final boolean forcePo2) { int minWidth = 0; int minHeight = 0; int minArea = Integer.MAX_VALUE; for (int row = 1; row <= num; row++) { int col = (int) Math.ceil((float) num / (float) row); int po2Width = forcePo2 ? getNextPO2(col * width) : col * width; int po2Height = forcePo2 ? getNextPO2(row * height) : row * height; int area = po2Width * po2Height; if (area < minArea && po2Width <= maxTextureSize) { minArea = area; minWidth = po2Width; minHeight = (po2Height < maxTextureSize) ? po2Height : maxTextureSize; } } return new Point(minWidth, minHeight); } /** * Calculates the next highest power of two for a given integer. * * @param n the number * @return a power of two equal to or higher than n */ public static int getNextPO2(int n) { n -= 1; n = n | (n >> 1); n = n | (n >> 2); n = n | (n >> 4); n = n | (n >> 8); n = n | (n >> 16); n = n | (n >> 32); return n + 1; } }