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