In: Computer Science
Using the following in Java-
package intersectionprinter;
import java.awt.Rectangle;
public class IntersectionPrinter {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(0,0,100,150);
System.out.println(r1);
Rectangle r2 = new Rectangle(50,75,100,150);
System.out.println(r2);
Rectangle r3 = r1.intersection(r2);
}
}
Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the program print that there is no overlap.
Please find the code below:
package intersectionprinter;
import java.awt.Rectangle;
public class IntersectionPrinter {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(0,0,100,150);
System.out.println(r1);
Rectangle r2 = new Rectangle(50,75,100,150);
System.out.println(r2);
Rectangle r3 = r1.intersection(r2);
//if heigth and width is less than 0 than there is no overlap
if(r3.getWidth()<0 && r3.getHeight()<0){
System.out.println("No overlap");
}else{
System.out.println("Rectangles overlap. Coordinates is as below");
System.out.println(r3);
}
}
}
output: