public static RectangleLength2D_F32 boundBoxInside ( int srcWidth , int srcHeight , PixelTransform < Point2D_F32 > transform , Point2D_F32 work ) { List < Point2D_F32 > points = computeBoundingPoints ( srcWidth , srcHeight , transform , work ) ; Point2D_F32 center = new Point2D_F32 ( ) ; UtilPoint2D_F32 . mean ( points , center ) ; float x0 , x1 , y0 , y1 ; x0 = y0 = Float . MAX_VALUE ; x1 = y1 = - Float . MAX_VALUE ; for ( int i = 0 ; i < points . size ( ) ; i ++ ) { Point2D_F32 p = points . get ( i ) ; if ( p . x < x0 ) x0 = p . x ; if ( p . x > x1 ) x1 = p . x ; if ( p . y < y0 ) y0 = p . y ; if ( p . y > y1 ) y1 = p . y ; } x0 -= center . x ; x1 -= center . x ; y0 -= center . y ; y1 -= center . y ; float ox0 = x0 ; float oy0 = y0 ; float ox1 = x1 ; float oy1 = y1 ; for ( int i = 0 ; i < points . size ( ) ; i ++ ) { Point2D_F32 p = points . get ( i ) ; float dx = p . x - center . x ; float dy = p . y - center . y ; // see if the point is inside the box if ( dx > x0 && dy > y0 && dx < x1 && dy < y1 ) { // find smallest reduction in side length and closest to original rectangle float d0 = ( float ) ( float ) Math . abs ( dx - x0 ) + x0 - ox0 ; float d1 = ( float ) ( float ) Math . abs ( dx - x1 ) + ox1 - x1 ; float d2 = ( float ) ( float ) Math . abs ( dy - y0 ) + y0 - oy0 ; float d3 = ( float ) ( float ) Math . abs ( dy - y1 ) + oy1 - y1 ; if ( d0 <= d1 && d0 <= d2 && d0 <= d3 ) { x0 = dx ; } else if ( d1 <= d2 && d1 <= d3 ) { x1 = dx ; } else if ( d2 <= d3 ) { y0 = dy ; } else { y1 = dy ; } } } return new RectangleLength2D_F32 ( x0 + center . x , y0 + center . y , x1 - x0 , y1 - y0 ) ; }
Ensures that the entire box will be inside