Java Rectangle subdivide(Dimension dimension, int threshold, int max)

Here you can find the source of subdivide(Dimension dimension, int threshold, int max)

Description

Subdivides an area into a closest fit set of Rectangle with sides that are powers of 2.

License

LGPL

Parameter

Parameter Description
dimension The required total size
threshold The minimum required size
max The maximum required size

Return

A collection of the rectangles needed to construct the total size

Declaration

public static List subdivide(Dimension dimension, int threshold, int max) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 *                        Teseract Software, LLP (c) 2001
 *                               Java Source
 *
 * This source is licensed under the GNU LGPL v2.1
 * Please read http://www.gnu.org/copyleft/lgpl.html for more information
 *
 ****************************************************************************/

import java.awt.Dimension;
import java.awt.Rectangle;

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**/*from   ww w. jav a2  s. c  o  m*/
     * Subdivides an area into a closest fit set of Rectangle with sides that are
     * powers of 2. All elements will be less than max and greater than the minimum
     * value by threshhold.
     *
     * @param dimension The required total size
     * @param threshold The minimum required size
     * @param max The maximum required size
     * @return A collection of the rectangles needed to construct the total size
     */
    public static List subdivide(Dimension dimension, int threshold, int max) {
        return subdivide(dimension.width, dimension.height, threshold, max);
    }

    /**
     * Subdivides an area into a closest fit set of Rectangle with sides that are
     * powers of 2. All elements will be less than max and greater than the minimum
     * value by threshhold.
     *
     * @param threshold The minimum required size
     * @param max The maximum required size
     * @return A collection of the rectangles needed to construct the total size
     */
    public static List subdivide(int width, int height, int threshold, int max) {
        List cols = components(width, threshold, max);
        List rows = components(height, threshold, max);
        List parts = new ArrayList();

        int i = 0, j = 0;
        int x = 0, y = 0;
        int row_size = rows.size();
        int col_size = cols.size();

        for (i = 0; i < row_size; i++) {
            for (j = 0, x = 0; j < col_size; j++) {
                parts.add(new Rectangle(x, y, ((Integer) cols.get(j)).intValue(),
                        ((Integer) rows.get(i)).intValue()));
                x += ((Integer) cols.get(j)).intValue();
            }
            y += ((Integer) rows.get(i)).intValue();
        }

        return parts;
    }

    /**
     * Breaks an integer into powers of 2. The returned list contains a set of
     * Integers that if summed would be a closest fit to to value. Each returned
     * Integer is not greater than a power of 2 by more than threshhold and is not
     * greater than max.
     *
     * @param value The target value to achieve
     * @param threshold The minimum required size of the component total
     * @param max The maximum required size
     * @return A list, in order of the component values
     */
    public static List components(int value, int threshold, int max) {
        List components = new ArrayList();
        while (value > 0) {
            int p = Math.min(optimalPower(value, threshold, max), value);
            components.add(new Integer(p));
            value -= p;
        }

        return components;
    }

    /**
     * Returns an optimal power of two for the value given.
     * return the largest power of 2 which is less than or equal to the value, OR
     * it will return a larger power of two as long as the difference between
     * that and the value is not greater than the threshhold.
     */
    public static int optimalPower(int value, int threshhold, int max) {
        int optimal = 1;
        value = Math.min(value, max);
        while (optimal * 2 - value <= threshhold)
            optimal <<= 1;

        return optimal;
    }
}

Related

  1. rectFromArray(Rectangle2D aRectangle, double[] pts)
  2. rectFromRect2D(Rectangle2D rect)
  3. resetPosition(Window window1, Window window2)
  4. right(Rectangle r)
  5. size(final Component component)
  6. subdivideRectangle(int width, int height, int numTilesX, int numTilesY, int extraBorder)
  7. subtractInsets(Rectangle base, Insets insets)
  8. subtractInto(Insets i1, Rectangle r2)
  9. translate(RectangularShape r, double dx, double dy)