[librsvg/librsvg-2.44] gradient: do not mutate input focus point coordinates



commit a8b5197000c91354e30d0c3a5fe5850b64295a1f
Author: Paolo Borelli <pborelli gnome org>
Date:   Mon Sep 10 00:55:17 2018 +0200

    gradient: do not mutate input focus point coordinates
    
    The function returns a (x,y) no need to modify the input.

 rsvg_internals/src/gradient.rs | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)
---
diff --git a/rsvg_internals/src/gradient.rs b/rsvg_internals/src/gradient.rs
index ac1e3738..b1d1e699 100644
--- a/rsvg_internals/src/gradient.rs
+++ b/rsvg_internals/src/gradient.rs
@@ -502,7 +502,7 @@ fn set_common_on_pattern<P: cairo::Pattern + cairo::Gradient>(
 // defined by ‘cx’, ‘cy’ and ‘r’.
 //
 // So, let's do that!
-fn fix_focus_point(mut fx: f64, mut fy: f64, cx: f64, cy: f64, radius: f64) -> (f64, f64) {
+fn fix_focus_point(fx: f64, fy: f64, cx: f64, cy: f64, radius: f64) -> (f64, f64) {
     // Easy case first: the focus point is inside the circle
 
     if (fx - cx) * (fx - cx) + (fy - cy) * (fy - cy) <= radius * radius {
@@ -510,31 +510,23 @@ fn fix_focus_point(mut fx: f64, mut fy: f64, cx: f64, cy: f64, radius: f64) -> (
     }
 
     // Hard case: focus point is outside the circle.
-    // First, translate everything to the origin.
-
-    fx -= cx;
-    fy -= cy;
-
     // Find the vector from the origin to (fx, fy)
 
-    let mut vx = fx;
-    let mut vy = fy;
+    let mut dx = fx - cx;
+    let mut dy = fy - cy;
 
     // Find the vector's magnitude
+    let mag = (dx * dx + dy * dy).sqrt();
 
-    let mag = (vx * vx + vy * vy).sqrt();
-
-    // Normalize the vector to have a magnitude equal to radius; (vx, vy) will now be on the
-    // edge of the circle
-
+    // Normalize the vector to have a magnitude equal to radius
     let scale = mag / radius;
 
-    vx /= scale;
-    vy /= scale;
+    dx /= scale;
+    dy /= scale;
 
     // Translate back to (cx, cy) and we are done!
 
-    (vx + cx, vy + cy)
+    (cx + dx, cy + dy)
 }
 
 fn set_pattern_on_draw_context(


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