JackWhiteIII
JackWhiteIII

Reputation: 1402

difference between getx and getminx

I have a question about Java's Rectangle class. I'm wondering what's the difference between rectangle.getX() and rectangle.getMinX(). When I google searched that, in the Java Documentation I read this for getX():

Returns the X coordinate of the bounding Rectangle in double precision.

For getMinX() I've read the following:

Returns the smallest X coordinate of the framing rectangle of the Shape in double precision.

Now I am wondering: How can there be different X coordinates if for a Rectangle, there should be only one:

new Rectangle(0,0,100,100) = p1(0,0)|p2(100,0)|p3(0,100)|p4(100,100).

I'd understand if e.g. getMaxX() would be something like getX()+getWidth(), but then what would getMinX() be? I'm very confused about this.

I just need a short explanation, I'd be very grateful.

Upvotes: 3

Views: 1094

Answers (2)

Steve P.
Steve P.

Reputation: 14709

A rectangle is defined by: (x1,y1),(x2,y2). For our purposes, let us denote (x1,y1) and the upper-left-corner of the the rectangle and (x2,y2) as the bottom-right-corner.

rectangle.getX() returns x1, whereas rectangle.getMinX() returns xk such that: for all xi in X(The set of x coordinates), xk <= xi. By construction, the rectangle class defines x1 such that x1 = xk, as can be seen in the source code:

public double getMinX() 
{
    return getX();
}

public double getX()
{
    return x;
}

and further explained in the constructor:

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;
 }

NB: The other constructors call the above one.

Upvotes: 7

Petr Janeček
Petr Janeček

Reputation: 38434

You are right. With a Rectangle class, getMinX() is the same thing as getX().

When you look at the source code for OpenJDK's Rectangle, you can see this:

public double getMinX() {
    return getX();
}

To be honest, I haven't found a override for this method in the OpenJDK. Maybe I was looking at thw wrong places, I'm not sure. But all the shapes (ellipse, arc etc.) use this implementation. Peculiar.

Right now, I think the only difference is the name and the recommended usage. They both do the same thing, but I'd use one or the other in different situations:

  • getX() when I need the location of the shape, a point
  • getMinX() when I need the left bound of the shape

Upvotes: 2

Related Questions