public static CropBounds sizeToUnderlay(
CropBounds oldCrop,
Rectangle2D underlay,
double limitW, double limitH
) {
CropBounds newCrop = new CropBounds(
oldCrop.getCenter(), limitW, limitH, oldCrop.getAngle()
);
if (underlayContains(newCrop, underlay)) {
return newCrop;
}
double ult = ultHeight(oldCrop, underlay);
double ull = ullHeight(oldCrop, underlay);
double ulb = ulbHeight(oldCrop, underlay);
double ulr = ulrHeight(oldCrop, underlay);
double urt = urtHeight(oldCrop, underlay);
double url = urlHeight(oldCrop, underlay);
double urb = urbHeight(oldCrop, underlay);
double urr = urrHeight(oldCrop, underlay);
double llt = lltHeight(oldCrop, underlay);
double lll = lllHeight(oldCrop, underlay);
double llb = llbHeight(oldCrop, underlay);
double llr = llrHeight(oldCrop, underlay);
double lrt = lrtHeight(oldCrop, underlay);
double lrl = lrlHeight(oldCrop, underlay);
double lrb = lrbHeight(oldCrop, underlay);
double lrr = lrrHeight(oldCrop, underlay);
double aspect = oldCrop.getWidth() / oldCrop.getHeight();
double h = limitH;
h = minIgnoreNegative(h, ult);
h = minIgnoreNegative(h, ull);
h = minIgnoreNegative(h, ulb);
h = minIgnoreNegative(h, ulr);
h = minIgnoreNegative(h, urt);
h = minIgnoreNegative(h, url);
h = minIgnoreNegative(h, urb);
h = minIgnoreNegative(h, urr);
h = minIgnoreNegative(h, llt);
h = minIgnoreNegative(h, lll);
h = minIgnoreNegative(h, llb);
h = minIgnoreNegative(h, llr);
h = minIgnoreNegative(h, lrt);
h = minIgnoreNegative(h, lrl);
h = minIgnoreNegative(h, lrb);
h = minIgnoreNegative(h, lrr);
double w = h * aspect;
newCrop = new CropBounds(
oldCrop.getCenter(), w, h, oldCrop.getAngle()
);
return newCrop;
}