两个矩形的并集

问题描述 投票:0回答:1

我尝试为 Rectangle 类创建一个联合方法,该方法接受另一个矩形 r 作为参数,并将该矩形放入其自身和 r 的联合中。有人可以帮我解决这个问题吗?

Click on this link to see the problem please

我尝试这样做:

Please click on this link to see the image

但是按照我在上图中所做的那样,结果是这样的:

Please click on this link to see the image

该图像与我的预期相反,因为我想要所有绿色复选标记,所以有人可以帮助我并告诉我我做错了什么。

java methods rectangles
1个回答
0
投票

您可以使用 Rectangle 类,该类采用

(xloc, yloc, width, height)
并使用方法来获取并集。

 Rectangle r1 = new Rectangle(10,20,100,200);
 Rectangle r2 = new Rectangle(300,400,90,100);
 Rectangle union = (Rectangle)r1.createUnion(r2);
 
 System.out.println(r1);
 System.out.println(r2);
 System.out.println(union);

打印

ava.awt.Rectangle[x=10,y=20,width=100,height=200]
java.awt.Rectangle[x=300,y=400,width=90,height=100]
java.awt.Rectangle[x=10,y=20,width=380,height=480]
© www.soinside.com 2019 - 2024. All rights reserved.