[librsvg/rect: 6/10] Add trait extension to apply transform to a rect



commit 1a609462ebd3d14a9980b2e69abd88bd99857402
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Dec 8 15:27:55 2019 +0100

    Add trait extension to apply transform to a rect
    
    This is the same api used by euclid

 rsvg_internals/src/rect.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
---
diff --git a/rsvg_internals/src/rect.rs b/rsvg_internals/src/rect.rs
index 5b0d5bb6..a97f3606 100644
--- a/rsvg_internals/src/rect.rs
+++ b/rsvg_internals/src/rect.rs
@@ -227,6 +227,52 @@ impl From<IRect> for cairo::Rectangle {
     }
 }
 
+pub trait TransformRect {
+    fn transform_rect(&self, rect: &Rect) -> Rect;
+}
+
+impl TransformRect for cairo::Matrix {
+    fn transform_rect(&self, rect: &Rect) -> Rect {
+        let points = vec![
+            self.transform_point(rect.x0, rect.y0),
+            self.transform_point(rect.x1, rect.y0),
+            self.transform_point(rect.x0, rect.y1),
+            self.transform_point(rect.x1, rect.y1),
+        ];
+
+        let (mut xmin, mut ymin, mut xmax, mut ymax) = {
+            let (x, y) = points[0];
+
+            (x, y, x, y)
+        };
+
+        for &(x, y) in points.iter().take(4).skip(1) {
+            if x < xmin {
+                xmin = x;
+            }
+
+            if x > xmax {
+                xmax = x;
+            }
+
+            if y < ymin {
+                ymin = y;
+            }
+
+            if y > ymax {
+                ymax = y;
+            }
+        }
+
+        Rect {
+            x0: xmin,
+            y0: ymin,
+            x1: xmax,
+            y1: ymax,
+        }
+    }
+}
+
 pub trait RectangleExt {
     fn new(x: f64, y: f64, width: f64, height: f64) -> cairo::Rectangle;
     fn from_size(width: f64, height: f64) -> cairo::Rectangle;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]