Compares the x, y, width, and height values of each Rectangle2D. - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

Compares the x, y, width, and height values of each Rectangle2D.

Demo Code

/**// w  ww  . j av  a 2  s  .  co  m
 * Copyright 1998-2007, CHISEL Group, University of Victoria, Victoria, BC, Canada.
 * All rights reserved.
 */
//package com.java2s;

import java.awt.geom.Rectangle2D;

public class Main {
    /**
     * Compares the x, y, width, and height values of each Rectangle2D.
     * If the difference between each of them is within the given delta then true is returned.
     * @param bounds1   The first rectangle
     * @param bounds2   The second rectangle
     * @param delta      The upper limit on the difference between 2 values that are considered the same
     * @return boolean   if the two bounds are the same with the given delta
     */
    public static boolean compareBounds(Rectangle2D bounds1,
            Rectangle2D bounds2, double delta) {
        if ((bounds1 == null) || (bounds2 == null)) {
            return false;
        }

        double dx = Math.abs(bounds1.getX() - bounds2.getX());
        double dy = Math.abs(bounds1.getY() - bounds2.getY());
        double dw = Math.abs(bounds1.getWidth() - bounds2.getWidth());
        double dh = Math.abs(bounds1.getHeight() - bounds2.getHeight());
        boolean same = ((dx <= delta) && (dy <= delta) && (dw <= delta) && (dh <= delta));
        return same;
    }
}

Related Tutorials