java.awt.矩形。路口()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16771435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:58:37  来源:igfitidea点击:

java.awt.Rectangle. intersection()

javaawtrectangles

提问by muhamadto

I was developing an task when I decided to use java.awt.Rectangle to calculate the intersection between two rectangles.

当我决定使用 java.awt.Rectangle 来计算两个矩形之间的交集时,我正在开发一项任务。

I realised that the output is different from what I expected. I'm not sure if I understood how this method works or not.

我意识到输出与我的预期不同。我不确定我是否理解这种方法的工作原理。

For the values in the example here java.awt.Rectangle[x=0,y=10,width=5,height=8]java.awt.Rectangle[x=3,y=15,width=17,height=14]

对于此处示例中的值 java.awt.Rectangle[x=0,y=10,width=5,height=8]java.awt.Rectangle[x=3,y=15,width=17,height=14]

I expect the intersection to be java.awt.Rectangle[x=3,y=10,width=2,height=8]but the program prints java.awt.Rectangle[x=3,y=15,width=2,height=3]instead!

我希望交集是java.awt.Rectangle[x=3,y=10,width=2,height=8]但是程序打印出来的java.awt.Rectangle[x=3,y=15,width=2,height=3]

here is my code:

这是我的代码:

public void printIntersection(){ 
    Rectangle r1 = new Rectangle(0, 10, 5, 8);
    Rectangle r2 = new Rectangle(3, 15, 17, 14);
    Rectangle r3 = r1.intersection(r2);

    System.out.println(r1);
    System.out.println(r2);
    System.out.println(r3);

}

Can anyone help me by pointing out what am I missing here?

任何人都可以通过指出我在这里缺少什么来帮助我吗?

UPDATE: The source of my confusion is that the code treats the (x,y) values in the constructor as the bottom-left corner, while the class doc suggests that they are the upper-left corner!

更新:我困惑的根源在于代码将构造函数中的 (x,y) 值视为左下角,而类文档建议它们是左上角!

采纳答案by Pablo Lozano

The opposite corners of your rectangles are (0,10),(5,18)and (3,15),(20,29), so the intersection is (3,15),(5,18), so I think the result is the expected one. Notice the opposite corners of the resultant one are the bottom-right of the first one and the top-left of the second one.

矩形的对角是(0,10),(5,18)(3,15),(20,29),所以交点是(3,15),(5,18),所以我认为结果是预期的。请注意结果的对角是第一个的右下角和第二个的左上角。

Edit: The way it works is: the starting point is (x,y), and the sides are calculated adding the widthand height to the starting point, so the opposite corner will be (x+width,y+height)

编辑:它的工作方式是:起点为(x,y),边计算加上起点的宽度和高度,所以对角将是(x+width,y+height)

Final note: (0,0) is the upper-left corner of the canvas: Here is an example: (0,0,4,4) and (2,2,4,4) intersection is (2,2,2,2): (2,2) is the upper-left one and (2+2,2+2) is opposite corner

最后注意:(0,0) 是画布的左上角:这里是一个例子:(0,0,4,4) 和 (2,2,4,4) 交点是 (2,2,2) ,2): (2,2) 是左上角,(2+2,2+2) 是对角

enter image description here

在此处输入图片说明

回答by Ananth

The answer you are getting is correct. The method works like this.

你得到的答案是正确的。该方法是这样工作的。

1st Rectangle:

第一个矩形:

  • X co-ordinates: 0
  • Y co-ordinates: 10
  • Width: 5
  • Height: 8
  • X 坐标:0
  • Y坐标:10
  • 宽度:5
  • 高度:8

2nd Rectangle:

第二个矩形:

  • X co-ordinates: 3
  • Y co-ordinates: 15
  • Width: 17
  • Height: 14
  • X 坐标:3
  • Y坐标:15
  • 宽度:17
  • 身高:14

For the intersection the X and Y co-ordinates are same as 2nd rectangle. Width is 5-3=2 and Height is 18-15=3

对于交点,X 和 Y 坐标与第二个矩形相同。宽度为 5-3=2 高度为 18-15=3

回答by Abelgo

I also had trouble with this. The way I think about it is that the grid used is inverted on the y axis. Because point 0.0 is at the top left of the screen with point 0,1 being below rather than above that point you can get the answer you are expecting by inverting the the y axis in your original code.

我也遇到了这个问题。我的想法是使用的网格在 y 轴上反转。因为点 0.0 位于屏幕的左上角,而点 0,1 在该点下方而不是上方,所以您可以通过反转原始代码中的 y 轴来获得您期望的答案。

For example.

例如。

public void printIntersection(){ 
Rectangle r1 = new Rectangle(0, 10 * -1 , 5, 8);
Rectangle r2 = new Rectangle(3, 15 * -1, 17, 14);
Rectangle r3 = r1.intersection(r2);

System.out.println(r1);
System.out.println(r2);
System.out.println(r3);

}

}

This should give you the answer you are expecting

这应该给你你期待的答案