java 矩形重叠问题_java – 矩形重叠,但说它在里面?

我制作了一个程序,用户创建两个带有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();

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值