我有一个关于 Java 的 Rectangle 类的问题。我想知道有什么区别
rectangle.getX()
和 rectangle.getMinX()
。
当我用谷歌搜索时,在 Java 文档中我为 getX()
读到了这个:
以 double 返回边界矩形的 X 坐标。
对于 getMinX()
,我阅读了以下内容:
以 double 返回 Shape 的框架矩形的最小 X 坐标。
现在我想知道:如果一个矩形应该只有一个,怎么会有不同的 X 坐标:
new Rectangle(0,0,100,100) = p1(0,0)|p2(100,0)|p3(0,100)|p4(100,100).
如果例如getMaxX()
类似于 getX()+getWidth()
,那么 getMinX()
会是什么?
我对此很困惑。
我只需要一个简短的解释,我将不胜感激。
最佳答案
矩形定义为:(x1,y1),(x2,y2>).出于我们的目的,让我们用 (x1,y1) 和矩形的左上角表示,并且 (x< sub>2,y2) 作为右下角。
rectangle.getX()
返回 x1,而 rectangle.getMinX()
返回 xk 这样那:对于X(x坐标的集合)中的所有xi,xk <= xi。通过构造,矩形类定义 x1 使得 x1 = xk,如源代码中所示:
public double getMinX()
{
return getX();
}
public double getX()
{
return x;
}
并在构造函数中进一步解释:
Rectangle(int x, int y, int width, int height) Constructs a new Rectangle whose upper-left corner is specified as (x,y) and whose width and height are specified by the arguments of the same name.
public Rectangle(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
注意:其他构造函数调用上面的构造函数。
关于java - getx 和 getminx 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17751277/