@@ -108,6 +108,20 @@ public static Point pixelToPoint(Drawable drawable, Point point, int zoom) {
108108 }
109109
110110 public static Rectangle pixelToPoint (Rectangle rect , int zoom ) {
111+ if (zoom == 100 || rect == null ) return rect ;
112+ if (rect instanceof Rectangle .OfFloat rectOfFloat ) return pixelToPoint (rectOfFloat , zoom );
113+ Rectangle scaledRect = new Rectangle .OfFloat (0 ,0 ,0 ,0 );
114+ Point scaledTopLeft = pixelToPoint (new Point (rect .x , rect .y ), zoom );
115+ Point scaledBottomRight = pixelToPoint (new Point (rect .x + rect .width , rect .y + rect .height ), zoom );
116+
117+ scaledRect .x = scaledTopLeft .x ;
118+ scaledRect .y = scaledTopLeft .y ;
119+ scaledRect .width = scaledBottomRight .x - scaledTopLeft .x ;
120+ scaledRect .height = scaledBottomRight .y - scaledTopLeft .y ;
121+ return scaledRect ;
122+ }
123+
124+ private static Rectangle pixelToPoint (Rectangle .OfFloat rect , int zoom ) {
111125 return scaleBounds (rect , 100 , zoom );
112126 }
113127
@@ -120,6 +134,21 @@ public static Rectangle pixelToPoint(Drawable drawable, Rectangle rect, int zoom
120134 * Returns a new rectangle as per the scaleFactor.
121135 */
122136 public static Rectangle scaleBounds (Rectangle rect , int targetZoom , int currentZoom ) {
137+ if (rect == null || targetZoom == currentZoom ) return rect ;
138+ if (rect instanceof Rectangle .OfFloat rectOfFloat ) return scaleBounds (rectOfFloat , targetZoom , currentZoom );
139+ float scaleFactor = ((float )targetZoom ) / (float )currentZoom ;
140+ Rectangle returnRect = new Rectangle .OfFloat (0 ,0 ,0 ,0 );
141+ returnRect .x = Math .round (rect .x * scaleFactor );
142+ returnRect .y = Math .round (rect .y * scaleFactor );
143+ returnRect .width = Math .round (rect .width * scaleFactor );
144+ returnRect .height = Math .round (rect .height * scaleFactor );
145+ return returnRect ;
146+ }
147+
148+ /**
149+ * Returns a new rectangle as per the scaleFactor.
150+ */
151+ private static Rectangle scaleBounds (Rectangle .OfFloat rect , int targetZoom , int currentZoom ) {
123152 if (rect == null || targetZoom == currentZoom ) return rect ;
124153 Rectangle .OfFloat fRect = FloatAwareGeometryFactory .createFrom (rect );
125154 float scaleFactor = DPIUtil .getScalingFactor (targetZoom , currentZoom );
@@ -185,6 +214,20 @@ public static Point pointToPixel(Drawable drawable, Point point, int zoom) {
185214 }
186215
187216 public static Rectangle pointToPixel (Rectangle rect , int zoom ) {
217+ if (zoom == 100 || rect == null ) return rect ;
218+ if (rect instanceof Rectangle .OfFloat rectOfFloat ) return pointToPixel (rectOfFloat , zoom );
219+ Rectangle scaledRect = new Rectangle .OfFloat (0 ,0 ,0 ,0 );
220+ Point scaledTopLeft = pointToPixel (new Point (rect .x , rect .y ), zoom );
221+ Point scaledBottomRight = pointToPixel (new Point (rect .x + rect .width , rect .y + rect .height ), zoom );
222+
223+ scaledRect .x = scaledTopLeft .x ;
224+ scaledRect .y = scaledTopLeft .y ;
225+ scaledRect .width = scaledBottomRight .x - scaledTopLeft .x ;
226+ scaledRect .height = scaledBottomRight .y - scaledTopLeft .y ;
227+ return scaledRect ;
228+ }
229+
230+ private static Rectangle pointToPixel (Rectangle .OfFloat rect , int zoom ) {
188231 return scaleBounds (rect , zoom , 100 );
189232 }
190233
0 commit comments