AdjustBounds { public static void squareBounds(@NonNull Bounds b, int width, int height, @NonNull IntBounds padding) { final int activeWidth = width - padding.right - padding.left; final int activeHeight = height - padding.bottom - padding.top; final double aspectCanvas = ((double) activeWidth) / activeHeight; final double boundsWidth = b.x.max - b.x.min; final double boundsHeight = b.y.max - b.y.min; final double aspectBounds = boundsWidth / boundsHeight; if (aspectCanvas < aspectBounds) { final double delta = boundsWidth / aspectCanvas - boundsHeight; b.set(b.x.min, b.y.max, b.x.max, b.y.min - delta); } else { final double delta = (boundsHeight * aspectCanvas - boundsWidth); b.set(b.x.min, b.y.max, b.x.max + delta, b.y.min); } }  static void clean(@NonNull Bounds b, @NonNull Bounds inner, @NonNull Bounds outer); static void squareBounds(@NonNull Bounds b, int width, int height, @NonNull IntBounds padding);  }
@Test public void squareBoundsLandscape() { Bounds bounds = new Bounds(); bounds.set(0, 1, 1, 0); IntBounds padding = new IntBounds(); AdjustBounds.squareBounds(bounds, 400, 300, padding); assertEquals(0, bounds.x.min, 0.01); assertEquals(1, bounds.y.max, 0.01); assertEquals(1.33, bounds.x.max, 0.01); assertEquals(0, bounds.y.min, 0.01); }
@Test public void squareBoundsPortrait() { Bounds bounds = new Bounds(); bounds.set(0, 1, 1, 0); IntBounds padding = new IntBounds(); AdjustBounds.squareBounds(bounds, 300, 400, padding); assertEquals(0, bounds.x.min, 0.01); assertEquals(1, bounds.y.max, 0.01); assertEquals(1, bounds.x.max, 0.01); assertEquals(-0.33, bounds.y.min, 0.01); }