【发布时间】:2022-01-20 01:48:40
【问题描述】:
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() {
}
}
我正在尝试编写一个名为intersection 的方法,它接受两个矩形参数并返回当/如果它们重叠时形成的矩形,但是如果它们不重叠,该方法应该返回一个所有字段都为 0 的矩形
如果以下条件之一为真,则两个矩形不重叠。
- 一个矩形高于另一个矩形的上边缘。
- 一个矩形位于另一个矩形左边缘的左侧。
如果矩形仅接触,但不重叠,则宽度或高度应为零,但所有其他参数应正确计算和存储。
我真的可以在如何构建这个问题上使用一些帮助,因为逻辑让我感到困惑!
【问题讨论】:
标签: java intersection rectangles