gpt4 book ai didi

java - 矩形重叠,但说它在里面?

转载 作者:搜寻专家 更新时间:2023-11-01 03:49:02 26 4
gpt4 key购买 nike

我制作了一个程序,用户可以在其中创建两个具有 CenterX 和 CenterY 坐标、宽度和高度的自定义矩形。用户指定这些尺寸后,程序将显示一个矩形是否包含另一个矩形、是否与另一个矩形重叠或根本不相交。这是我的代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.*;
import javafx.scene.text.*;
import javafx.scene.paint.Color;
import javafx.scene.layout.Pane;
import java.util.*;

public class TwoRectangles extends Application
{
public void start(Stage stage)
{
Pane pane = new Pane();

Scanner input = new Scanner(System.in);
System.out.print("Enter the X and Y center coordinates for rectangle1: ");
double xCord1 = input.nextDouble();
double yCord1 = input.nextDouble();

System.out.print("Enter the Width and Height for rectangle1: ");
double width1 = input.nextDouble();
double height1 = input.nextDouble();

System.out.print("Enter the X and Y center coordinates for rectangle2: ");
double xCord2 = input.nextDouble();
double yCord2 = input.nextDouble();

System.out.print("Enter the Width and Height for rectangle2: ");
double width2 = input.nextDouble();
double height2 = input.nextDouble();

Rectangle rectangle1 = new Rectangle(xCord1, yCord1, width1, height1);
Rectangle rectangle2 = new Rectangle(xCord2, yCord2, width2, height2);

rectangle1.setFill(null);
rectangle2.setFill(null);
rectangle1.setStroke(Color.BLACK);
rectangle2.setStroke(Color.BLACK);

// Compute the 4 corners's coordinates for the rectangle
double r1x1 = xCord1 - (width1 / 2.0);
double r1x2 = xCord1 + (width1 / 2.0);
double r1y1 = yCord1 + (height1 / 2.0);
double r1y2 = yCord1 - (height1 / 2.0);

double r2x1 = xCord2 - (width2 / 2.0);
double r2x2 = xCord2 + (width2 / 2.0);
double r2y1 = yCord2 + (height2 / 2.0);
double r2y2 = yCord2 - (height2 / 2.0);


if ((r1x1 >= r2x1) && (r1x2 <= r2x2) && (r1y1 >= r2y1) && (r1y2 <= r2y2))
{
Text containText = new Text(500, 500, "One rectangle is contained in another");
pane.getChildren().add(containText);
}

else if ((r1x1 < r2x2) && (r1x2 > r2x1) && (r1y1 < r2y2) && (r1y2 > r2y1))
{
Text overlapText = new Text(500, 500, "The rectangles overlap");
pane.getChildren().add(overlapText);

}

else
{
Text noneText = new Text(500, 500, "The rectangles do not overlap");
pane.getChildren().add(noneText);
}
pane.getChildren().addAll(rectangle1, rectangle2);
Scene scene = new Scene(pane);
stage.setTitle("Overlapping Rectangles");
stage.setScene(scene);
stage.show();
}
}

每当我输入时,两个矩形的 centerX 和 centerY 坐标为 (50, 50),矩形 1 的宽度为 20,高度为 50,矩形 2 的宽度为 50,高度为 20,它告诉我矩形在彼此内部而不是相互重叠。我做错了什么?

最佳答案

您的问题是 Y 坐标。第二个比第一个小。这意味着测试将变得不那么可读,例如

(r1x1 >= r2x1) && (r1x2 <= r2x2) && (r1y1 >= r2y1) && (r1y2 <= r2y2)

变成(同时检查 r2 是否在 r1 中):

(r1x1 >= r2x1 && r1x2 <= r2x2 && r1y1 <= r2y1 && r1y2 >= r2y2) ||
// ^^ ^^
(r2x1 >= r1x1 && r2x2 <= r1x2 && r2y1 <= r1y1 && r2y1 >= r1y2)

关于java - 矩形重叠,但说它在里面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33704871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com