Saturday 11 June 2016

Chapter 3 Exercise 28, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

**3.28 (Geometry: two rectangles) Write a program that prompts the user to enter the center x-, y-coordinates, width, and height of two rectangles and determines whether the second rectangle is inside the first or overlaps with the first, as shown in Figure 3.9. Test your program to cover all cases. Here are the sample runs:
Enter r1's center x-, y-coordinates, width, and height: 2.5 4 2.5 43
Enter r2's center x-, y-coordinates, width, and height: 1.5 5 0.5 3
r2 is inside r1

Enter r1's center x-, y-coordinates, width, and height: 1 2 3 5.5
Enter r2's center x-, y-coordinates, width, and height: 3 4 4.5 5
r2 overlaps r1

Enter r1's center x-, y-coordinates, width, and height: 1 2 3 3
Enter r2's center x-, y-coordinates, width, and height: 40 45 3 2
r2 does not overlap r1







import java.util.Scanner;
 
 
 
import java.util.Scanner;
 
public class ProgrammingEx3_28 {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out
    .print("Enter r1's center x-, y-coordinates, width, and height:");
  double x1 = input.nextDouble();
  double y1 = input.nextDouble();
  double w1 = input.nextDouble();
  double h1 = input.nextDouble();
  w1 = w1 / 2;
  h1 = h1 / 2;
  System.out
    .print("Enter r2's center x-, y-coordinates, width, and height:");
  double x2 = input.nextDouble();
  double y2 = input.nextDouble();
  double w2 = input.nextDouble();
  double h2 = input.nextDouble();
  w2 = w2 / 2;
  h2 = h2 / 2;
 
  // Calculating range of r1 and r2
  double x1max = x1 + w1;
  double y1max = y1 + h1;
  double x1min = x1 - w1;
  double y1min = y1 - h1;
  double x2max = x2 + w2;
  double y2max = y2 + h2;
  double x2min = x2 - w2;
  double y2min = y2 - h2;
 
  if (x1max == x2max && x1min == x2min && y1max == y2max
    && y1min == y2min) {
   // Check if the two are identicle
   System.out.print("r1 and r2 are indentical");
 
  } else if (x1max <= x2max && x1min >= x2min && y1max <= y2max
    && y1min >= y2min) {
   // Check if r1 is in r2
   System.out.print("r1 is inside r2");
  } else if (x2max <= x1max && x2min >= x1min && y2max <= y1max
    && y2min >= y1min) {
   // Check if r2 is in r1
   System.out.print("r2 is inside r1");
  } else if (x1max < x2min || x1min > x2max || y1max < y2min
    || y2min > y1max) {
   // Check if the two overlap
   System.out.print("r2 does not overlaps r1");
  } else {
   System.out.print("r2 overlaps r1");
  }
 
 }
}

2 comments :

  1. there are error at else if (x1max < x2min || x1min > x2max || y1max < y2min|| y2min > y1max)


    error is y2min > y1max shuould change to y1min>y2max

    ReplyDelete
  2. another solution by Y. Daniel LiangY edited by me
    System.out.print("Enter r1’s center x-, y-coordinates, width, and height: ");
    double x1 = input.nextDouble();
    double y1 = input.nextDouble();
    double w1 = input.nextDouble();
    double h1 = input.nextDouble();

    System.out.print("Enter r2’s center x-, y-coordinates, width, and height: ");
    double x2 = input.nextDouble();
    double y2 = input.nextDouble();
    double w2 = input.nextDouble();
    double h2 = input.nextDouble();

    double xDistance = Math.abs(x1 - x2);
    double yDistance = Math.abs(y1 - y2);

    if (xDistance <=Math.abs (w1 - w2) / 2 && yDistance <= Math.abs(h1 - h2) / 2)
    {
    if (w1 > w2)
    System.out.println("rectangle 2 is in rectangle 1");
    else if(w2 > w1)
    System.out.println("rectangle 1 is in rectangle 2");
    else
    System.out.println(" identical");

    }

    else if (xDistance <=Math.abs (w1 + w2) / 2 && yDistance <=Math.abs (h1 + h2) / 2)
    System.out.println("r2 overlaps r1");
    else
    System.out.println("r2 does not overlap r1");

    ReplyDelete