public class RectangleCenter {
static class Point {
double x, y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
}
static class Rectangle {
Point topLeft;
Point bottomRight;
Rectangle(Point topLeft, Point bottomRight) {
this.topLeft = topLeft;
this.bottomRight = bottomRight;
}
}
static Point getCenter(Rectangle rect) {
double centerX = (rect.topLeft.x + rect.bottomRight.x) / 2.0;
double centerY = (rect.topLeft.y + rect.bottomRight.y) / 2.0;
return new Point(centerX, centerY);
}
public static void main(String[] args) {
Rectangle rect = new Rectangle(
new Point(10.0, 20.0),
new Point(110.0, 70.0)
);
Point center = getCenter(rect);
System.out.printf("Center of the rectangle: (%.2f, %.2f)%n", center.x, center.y);
}
}
/*
run:
Center of the rectangle: (60.00, 45.00)
*/