import java.util.Scanner;
public class RectangleTester {
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle(0, 0, 0, 0);
Rectangle rectangle2 = new Rectangle(0, 0, 0, 0);
Scanner input = new Scanner(System.in);
System.out.println("Rectangle 1:");
System.out.println("Enter the x coordinate ");
int r1x1 = input.nextInt();
System.out.println("Enter the y coordinate");
int r1y1 = input.nextInt();
System.out.println("Enter the width");
int width1 = input.nextInt();
System.out.println("Enter the height");
int height1 = input.nextInt();
rectangle1.setLeft(r1x1);
rectangle1.setBottom(r1y1);
rectangle1.setWidth(width1);
rectangle1.setHeight(height1);
System.out.println("\n*****************\nRectangle 2:");
System.out.println("Enter the x coordinate ");
int r2x1 = input.nextInt();
System.out.println("Enter the y coordinate");
int r2y1 = input.nextInt();
System.out.println("Enter the width");
int width2 = input.nextInt();
System.out.println("Enter the height");
int height2 = input.nextInt();
rectangle2.setLeft(r2x1);
rectangle2.setBottom(r2y1);
rectangle2.setWidth(width2);
rectangle2.setHeight(height2);
System.out.println("\n****************\n"+"Rectangle 1: \n"+rectangle1.toString()+"\nArea is " + rectangle1.area(rectangle1) + "\nPerimeter is " + rectangle1.perimeter(rectangle1));
System.out.println("*****************"+"\nRectangle 2: \n"+rectangle2.toString()+"\nArea is " + rectangle2.area(rectangle2) + "\nPerimeter is " + rectangle2.perimeter(rectangle2));
int r1y2=r1y1+height1;
int r1x2=r1x1+width1;
int r2y2=r2y1+height2;
int r2x2=r2x1+width1;
int r3x1=Math.max(r1x1, r2x1);
int r3x2=Math.min(r1x2, r2x2);
int r3y1=Math.max(r1y1, r2y1);
int r3y2=Math.min(r1y2, r2y2);
}
public static boolean intersection() {
}
}
I am trying write a method called intersection
that takes two rectangles parameters and returns the rectangle that is formed when/if they overlap, if they don't overlap however, the method should return a rectangle where all fields are 0
Two rectangles do not overlap if one of the following conditions is true.
- One rectangle is above top edge of the other rectangle.
- One rectangle is on left side of left edge of the other rectangle.
If the rectangles only touch, but do not overlap, then the width or height should be zero, but all other parameters should be properly calculated and stored.
I could really use some help on how to structure this cause the logic is confusing to me!