0

One of the exercises in my Java textbook says "Consult the API documentation to find methods for: Computing the smallest rectangle that contains two given rectangles. • Returning a random floating-point number."

I've looked at the Java API for class Rectangle, but I can't find one that computes the smaller rectangle. The closest methods I've found are union and bounds, but I don't think that's correct.

I found min from the Java Math class and wrote a test program to see if it would work, but min cannot have arguments of rectangles.

Here's the code I wrote:

import java.awt.Rectangle;
public class RectangleSize {
    public static void main(String[] args)
    {
        Rectangle a = new Rectangle(5, 5, 10, 10);
        Rectangle b = new Rectangle(5, 5, 20, 20);
        int min = Math.min(a, b); //In Eclipse, I get an error.
        System.out.println(min);
    }
}
Meico Hsiao
  • 13
  • 1
  • 7
  • 1
    "... finds the smaller rectangle." Reading is important. – Ignacio Vazquez-Abrams Oct 11 '14 at 04:36
  • "the smallest rectangle that contains two given rectangles". To find this you must have multiple rectangles. If you have many then only, some large rectangle can have two small rectangle inside. – PM. Oct 11 '14 at 04:38
  • Math.min(int,int); takes int args. And u trying to pass Rectangle! – George Rosario Oct 11 '14 at 04:59
  • "The smallest rectangle that contains two given rectangles" is not the same as "the smaller rectangle" (of the two given). It sounds like you're actually being asked to create a *new* rectangle that contains the original two with a minimum of extra space. – Wyzard Oct 11 '14 at 05:37

4 Answers4

1

I am studying java recently using the textbook of Big Java Early Object. I found the same question in chapter 2. Eventually I found the answer by looking at Java SE8 API list.

  1. First use method .add() to combine 2 rectangles. Note: actually, this combined one is already the one you need. But you can get a new rectangle (same size and location) at step 2.

  2. Them use .getBounds() to get the smallest rectangle containing the combined one.

.

import java.awt.*;

public class RectangleTester01 {
    public static void main(String[] args) {
        Rectangle box1 = new Rectangle(10, 20, 40, 40);
        Rectangle box2 = new Rectangle(20, 30, 60, 60);
        box1.add(box2);
        System.out.println(box1);

        Rectangle box3 = box1.getBounds();
        System.out.println(box3);

    }
}

output is:

java.awt.Rectangle[x=10,y=20,width=70,height=70] java.awt.Rectangle[x=10,y=20,width=70,height=70]

bummi
  • 27,123
  • 14
  • 62
  • 101
Qirui
  • 11
  • 3
0

You want to use Rectangle.contains . You would be given many rectangles. You'd need to loop through all the rectangles and see if it contains the two rectangles given. If it does you should calculate the size of that rectangle. In the end you take the rectangle with the smallest size.

public Rectangle getSmallest(Rectangle one, Rectangle two, Rectangle[] rectangles) {
   Rectangle smallest = null;
   double area = Double.MAX_VALUE;

   for (Rectangle r: rectangles) {
      if (r.contains(one) && r.contains(two)) {
         calculatedArea = r.getWidth() * r.getHeight();
         if (calculatedArea < area) {
            area = calculatedArea;
            smallest = r;
         }
      }
   }

   return r;
}
0

Here is another question to find a rectangle which contains a list of rectangles.find-smallest-area-that-contains-all-the-rectangles.

Here is my brute answer which is not accuracy and did not try the union function of Rectengle and also the createUnion(Rectengle2D r) as well.

Math.min can help find the minimum number in a number array. You may write a similar one for Rectangle. This is something from 1D to 2D in my mind. BTW, it would be much interesting if you extends it to 3D or nD objects calculation.

package com.stackoverflow.q26311076;

import java.awt.Rectangle;

public class Test {

    public static void main(String[] args) {
        Rectangle a = new Rectangle(5, 5, 10 , 10);
        Rectangle b = new Rectangle(5, 5, 20 , 20);
        Rectangle min = getMin(a, b) ; 
        // ... 
    }


    public static Rectangle getMin(Rectangle a, Rectangle b) {
        //find the min range in X.
        {
        double x1 = a.getX();
        double x2 = a.getX() + a.getWidth();
        double x3 = b.getX();
        double x4 = b.getX() + b.getWidth();
        double minX1 =Math.min( Math.min(x1, x2), Math.min(x3, x4)) ;
        double maxX1 =Math.max( Math.max(x1, x2), Math.max(x3, x4)) ;
        }
        //find the min range in Y.
        {           

        double y1 = a.getY();
        double y2 = a.getY() + a.getHeight();
        double y3 = b.getY();
        double y4 = b.getY() + b.getHeight();
        double minY1 =Math.min( Math.min(y1, y2), Math.min(y3, y4)) ;
        double maxY1 =Math.max( Math.max(y1, y2), Math.max(y3, y4)) ;
        }                       
        //build new rectangle  with X & Y     
        Rectangle r = new Rectangle();
        r.setRect(minX1, minY1, maxX1 - minX1, maxY1 - minY1);
        return r;
    }

}
Community
  • 1
  • 1
paco alcacer
  • 381
  • 2
  • 13
  • 1
    OP explicitly said this is homework question.. Why would you post the answer? Also, what does your code do? Simply posting code with no comments or explanation doesn't make a good answer. – theGreenCabbage Oct 11 '14 at 06:26
  • @theGreenCabbage err.. Only provide a way using brute force to do so. IMO, brute force is not a good idea in most scenario. Hope OP can think of a much better answer for the homework. Use the little to get the big. -->>> As I read, I just write a API to "Computing the smallest rectangle that contains two given rectangles." – paco alcacer Oct 11 '14 at 06:45
  • Hope the add comments and explanation would help you understand what I want to say. – paco alcacer Oct 11 '14 at 07:37
0

I think the method you're looking for is Rectangle2D.createUnion. It combines two rectangles to make a bigger one that contains both with a minimum of extra space.

Wyzard
  • 33,849
  • 3
  • 67
  • 87