Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

If you want to implement rolfl's suggestion to have the bounding rectangle outside the points, then use Math.ceilMath.ceil for example:

If you want to implement rolfl's suggestion to have the bounding rectangle outside the points, then use Math.ceil for example:

If you want to implement rolfl's suggestion to have the bounding rectangle outside the points, then use Math.ceil for example:

added 290 characters in body
Source Link
ChrisW
  • 12.9k
  • 1
  • 34
  • 76

If you want to implement rolfl's suggestion to have the bounding rectangle outside the points, then use Math.ceil for example:

if (p1.x <= p2.x)
{
    int x = p1.x;
    int w = Math.ceil(p2.x) - x;
    ... etc ...

If you want to implement rolfl's suggestion to have the bounding rectangle outside the points, then use Math.ceil for example:

if (p1.x <= p2.x)
{
    int x = p1.x;
    int w = Math.ceil(p2.x) - x;
    ... etc ...
added 358 characters in body
Source Link
ChrisW
  • 12.9k
  • 1
  • 34
  • 76

Refactoring the above to make it more compactreduce duplication:

int x;
int w;
if (p1.x <= p2.x)
{
    x = p1.x;
    w = p2.x - p1.x;
}
else
{
    x = p2.x;
    w = p1.x - p2.x;
}
int y;
int h;
if (p1.y <= p2.y)
{
    y = p1.y;
    h = p2.y - p1.y;
}
else
{
    y = p2.y;
    h = p1.y - p2.y;
}
return new Rectangle(x, y, w, h);

Or refactoring to make it more compact:

if (p1.x <= p2.x)
{
    if (p1.y <= p2.y)
    {
        return new Rectangle(p1.x, p1.y, p2.x - p1.x, p2.y - p1.y);
    }
    else
    {
        return new Rectangle(p1.x, p2.y, p2.x - p1.x, p1.y - p2.y);
    }
}
else
{
    ... etc ...
}

Refactoring the above to make it more compact:

int x;
int w;
if (p1.x <= p2.x)
{
    x = p1.x;
    w = p2.x - p1.x;
}
else
{
    x = p2.x;
    w = p1.x - p2.x;
}
int y;
int h;
if (p1.y <= p2.y)
{
    y = p1.y;
    h = p2.y - p1.y;
}
else
{
    y = p2.y;
    h = p1.y - p2.y;
}
return new Rectangle(x, y, w, h);

Refactoring the above to reduce duplication:

int x;
int w;
if (p1.x <= p2.x)
{
    x = p1.x;
    w = p2.x - p1.x;
}
else
{
    x = p2.x;
    w = p1.x - p2.x;
}
int y;
int h;
if (p1.y <= p2.y)
{
    y = p1.y;
    h = p2.y - p1.y;
}
else
{
    y = p2.y;
    h = p1.y - p2.y;
}
return new Rectangle(x, y, w, h);

Or refactoring to make it more compact:

if (p1.x <= p2.x)
{
    if (p1.y <= p2.y)
    {
        return new Rectangle(p1.x, p1.y, p2.x - p1.x, p2.y - p1.y);
    }
    else
    {
        return new Rectangle(p1.x, p2.y, p2.x - p1.x, p1.y - p2.y);
    }
}
else
{
    ... etc ...
}
added 441 characters in body
Source Link
ChrisW
  • 12.9k
  • 1
  • 34
  • 76
Loading
Source Link
ChrisW
  • 12.9k
  • 1
  • 34
  • 76
Loading