Rectangle2D boundingBox = this.getBoundingBox();
double currentWidth = boundingBox.getWidth();
double currentHeight = boundingBox.getHeight();
double desiredWidth = Math.abs(lowerRight.x - upperLeft.x);
double desiredHeight = Math.abs(lowerRight.y - upperLeft.y);
Vector2D newUpperLeftCorner = new Vector2D(Math.min(upperLeft.x, lowerRight.x), Math.min(upperLeft.y, lowerRight.y));
Vector2D newLowerRightCorner = new Vector2D(Math.max(upperLeft.x, lowerRight.x), Math.max(upperLeft.y, lowerRight.y));
double scaleX = desiredWidth / currentWidth;
double scaleY = desiredHeight / currentHeight;
double scale = Math.min(scaleX, scaleY);
scaledScene.scaleScene(scale);
double transX, transY;
if (mode == Scene2D.FITTING_MODE_FIT_UPPER_LEFT) {
Vector2D currentUpperLeftCorner = scaledScene.getBoundingBox().upperLeftCorner();
transX = newUpperLeftCorner.x - currentUpperLeftCorner.x;
transY = newUpperLeftCorner.y - currentUpperLeftCorner.y;
} else if (mode == Scene2D.FITTING_MODE_FIT_LOWER_RIGHT) {
Vector2D currentLowerRightCorner = scaledScene.getBoundingBox().lowerRightCorner();
transX = newLowerRightCorner.x - currentLowerRightCorner.x;
transY = newLowerRightCorner.y - currentLowerRightCorner.y;
} else { // First case again - center mode not yet implemented!
Vector2D currentUpperLeftCorner = scaledScene.getBoundingBox().upperLeftCorner();
transX = newUpperLeftCorner.x - currentUpperLeftCorner.x;
transY = newUpperLeftCorner.y - currentUpperLeftCorner.y;
}
Vector2D translation = new Vector2D(transX, transY);
sceneCopy.translateScene(translation);
sceneCopy.scaleScene(scale);
this.clearScene();