Java Rectangle Fit reScale(Rectangle rect, int oldScale, int newScale)

Here you can find the source of reScale(Rectangle rect, int oldScale, int newScale)

Description

re Scale

License

Open Source License

Declaration

public static Rectangle reScale(Rectangle rect, int oldScale, int newScale) 

Method Source Code

//package com.java2s;
/* Barlog Game Engine/*from w w w.ja va  2 s.c o  m*/
 * Copyright (C) 2011 Jieni Luchijinzhou a.k.a. Denis Luchkin-Zhou
 * -----------------------------------------------------------------------
 * /com/wyvernzora/barlog/Utilities.java
 * -----------------------------------------------------------------------
 * 
 * Utility class for miscellaneous methods.
 * 
 * 
 * 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.Point;
import java.awt.Rectangle;

public class Main {
    public static int reScale(int num, int oldScale, int newScale) {
        double dnum = new Double(num);
        double dold = new Double(oldScale);
        double dnew = new Double(newScale);

        return (int) (dnum * (dnew / dold));
    }

    public static float reScale(float num, int oldScale, int newScale) {
        double dnum = new Double(num);
        double dold = new Double(oldScale);
        double dnew = new Double(newScale);

        return (float) (dnum * (dnew / dold));
    }

    public static Point reScale(Point p, int oldScale, int newScale) {
        return new Point(reScale(p.x, oldScale, newScale), reScale(p.y, oldScale, newScale));
    }

    public static Rectangle reScale(Rectangle rect, int oldScale, int newScale) {
        Rectangle res = new Rectangle();
        res.x = reScale(rect.x, oldScale, newScale);
        res.y = reScale(rect.y, oldScale, newScale);
        res.width = reScale(rect.width, oldScale, newScale);
        res.height = reScale(rect.height, oldScale, newScale);
        return res;
    }
}

Related

  1. fitRectInRect(Rectangle rect, Rectangle bounds)
  2. fits(Rectangle r)
  3. fits(Rectangle2D o1, Rectangle2D o2)
  4. fitsInside(Dimension2D dim, Rectangle2D rect)
  5. fitsRotated(Rectangle2D o1, Rectangle2D o2)
  6. shrinkToFit(Rectangle container, Rectangle item)