[gimp] app: Unify direction calculation for painting and stroking



commit 20afb328a7aeec1d6c9ec349092bb94df7b1e5c0
Author: Alexia Death <alexiadeath gmail com>
Date:   Wed Nov 17 00:03:07 2010 +0200

    app: Unify direction calculation for painting and stroking

 app/core/gimpcoords.c                 |   46 +++++++++++++++++++++++++++++++++
 app/core/gimpcoords.h                 |    3 ++
 app/display/gimpdisplayshell-coords.c |   20 ++++++--------
 app/paint/gimppaintcore-stroke.c      |   26 ++----------------
 4 files changed, 61 insertions(+), 34 deletions(-)
---
diff --git a/app/core/gimpcoords.c b/app/core/gimpcoords.c
index ee8876d..febb57c 100644
--- a/app/core/gimpcoords.c
+++ b/app/core/gimpcoords.c
@@ -196,3 +196,49 @@ gimp_coords_equal (const GimpCoords *a,
           a->velocity  == b->velocity &&
           a->direction == b->direction);
 }
+
+/* helper for calculating direction of two gimpcoords. */
+
+gdouble
+gimp_coords_direction (const GimpCoords *a,
+                       const GimpCoords *b)
+{
+  gdouble direction;
+  gdouble delta_x, delta_y;
+
+  delta_x = a->x - b->x;
+  delta_y = a->y - b->y;
+
+  if ((delta_x == 0) && (delta_y == 0))
+    {
+      direction = a->direction;
+    }
+  else if (delta_x == 0)
+    {
+       if (delta_y > 0)
+          direction = 0.25;
+       else
+          direction = 0.75;
+     }
+   else if (delta_y == 0)
+     {
+       if (delta_x < 0)
+         direction = 0.0;
+       else
+         direction = 0.5;
+     }
+    else
+     {
+        direction = atan ((- 1.0 * delta_y) / delta_x) / (2 * G_PI);
+
+        if (delta_x > 0.0)
+          direction = direction + 0.5;
+
+        if (direction < 0.0)
+           direction = direction + 1.0;
+
+
+      }
+
+  return direction;
+}
diff --git a/app/core/gimpcoords.h b/app/core/gimpcoords.h
index aa5dc0d..5784945 100644
--- a/app/core/gimpcoords.h
+++ b/app/core/gimpcoords.h
@@ -50,5 +50,8 @@ gdouble  gimp_coords_manhattan_dist (const GimpCoords *a,
 gboolean gimp_coords_equal          (const GimpCoords *a,
                                      const GimpCoords *b);
 
+gdouble  gimp_coords_direction      (const GimpCoords *a,
+                                     const GimpCoords *b);
+
 
 #endif /* __GIMP_COORDS_H__ */
diff --git a/app/display/gimpdisplayshell-coords.c b/app/display/gimpdisplayshell-coords.c
index c92e784..00e3d6e 100644
--- a/app/display/gimpdisplayshell-coords.c
+++ b/app/display/gimpdisplayshell-coords.c
@@ -26,6 +26,7 @@
 #include "gimpdisplayshell.h"
 #include "gimpdisplayshell-coords.h"
 
+#include "core/gimpcoords.h"
 #include "core/gimpcoords-interpolate.h"
 
 /* Velocity unit is screen pixels per millisecond we pass to tools as 1. */
@@ -118,9 +119,10 @@ gimp_display_shell_eval_event (GimpDisplayShell *shell,
       gdouble filter;
       gdouble dist;
       gdouble delta_dir;
+      GimpCoords last_dir_event = shell->last_coords;
 
-      delta_x = shell->last_coords.x - coords->x;
-      delta_y = shell->last_coords.y - coords->y;
+      delta_x = last_dir_event.x - coords->x;
+      delta_y = last_dir_event.y - coords->y;
 
       /* Events with distances less than the screen resolution are not
        * worth handling.
@@ -178,11 +180,11 @@ gimp_display_shell_eval_event (GimpDisplayShell *shell,
                   (fabs (dir_delta_y) < DIRECTION_RADIUS)) &&
                   (x >= 0))
             {
-              const GimpCoords old_event = g_array_index (shell->event_history,
-                                                          GimpCoords, x);
+              last_dir_event = g_array_index (shell->event_history,
+                                                        GimpCoords, x);
 
-              dir_delta_x = old_event.x - coords->x;
-              dir_delta_y = old_event.y - coords->y;
+              dir_delta_x = last_dir_event.x - coords->x;
+              dir_delta_y = last_dir_event.y - coords->y;
 
               x--;
             }
@@ -195,11 +197,7 @@ gimp_display_shell_eval_event (GimpDisplayShell *shell,
         }
       else
         {
-          coords->direction = atan ((- 1.0 * dir_delta_y) /
-                                    dir_delta_x) / (2 * G_PI);
-
-          if (dir_delta_x > 0.0)
-            coords->direction = coords->direction + 0.5;
+          coords->direction = gimp_coords_direction (&last_dir_event, coords);
         }
 
       coords->direction = coords->direction - floor (coords->direction);
diff --git a/app/paint/gimppaintcore-stroke.c b/app/paint/gimppaintcore-stroke.c
index 3d7fc6c..ff396d5 100644
--- a/app/paint/gimppaintcore-stroke.c
+++ b/app/paint/gimppaintcore-stroke.c
@@ -27,6 +27,7 @@
 
 #include "core/gimpdrawable.h"
 #include "core/gimperror.h"
+#include "core/gimpcoords.h"
 
 #include "vectors/gimpstroke.h"
 #include "vectors/gimpvectors.h"
@@ -379,34 +380,13 @@ gimp_paint_core_stroke_emulate_dynamics (GimpCoords *coords,
   if (length > 1)
     {
       gint i;
-      gdouble delta_x;
-      gdouble delta_y;
-
       /* Fill in direction */
       for (i = 1; i < length; i++)
         {
-          delta_x = coords[i - 1].x - coords[i].x;
-          delta_y = coords[i - 1].y - coords[i].y;
-
-          if ((delta_x == 0) && (delta_y == 0))
-            {
-              coords[i].direction = coords[i - 1].direction;
-            }
-          else
-            {
-              coords[i].direction = atan (delta_y / delta_x) / (2 * G_PI);
-
-              if (coords[i].direction < 0)
-                coords[i].direction += 1.0;
-
-              coords[i].direction = 1.0 - coords[i].direction;
-            }
+           coords[i].direction = gimp_coords_direction (&coords[i-1], &coords[i]);
 
         }
 
-      if (length > 1)
-        {
-          coords[0].direction = coords[1].direction;
-        }
+      coords[0].direction = coords[1].direction;
     }
 }



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