[gimp] app: add gimp_canvas_item_transform_distance() and transform_distance_square()



commit a4d2acac38ebc146a9ac999642f73e3011cbf7dd
Author: Michael Natterer <mitch gimp org>
Date:   Sat Jun 10 21:55:40 2017 +0200

    app: add gimp_canvas_item_transform_distance() and transform_distance_square()
    
    to allow items to measure distances, exactly like the identical
    GimpDrawTool API.

 app/display/gimpcanvasitem.c |   57 ++++++++++++++++++++++++++++++++++++++++++
 app/display/gimpcanvasitem.h |   12 +++++++++
 2 files changed, 69 insertions(+), 0 deletions(-)
---
diff --git a/app/display/gimpcanvasitem.c b/app/display/gimpcanvasitem.c
index 82e8fb6..8d9e2bc 100644
--- a/app/display/gimpcanvasitem.c
+++ b/app/display/gimpcanvasitem.c
@@ -585,6 +585,63 @@ gimp_canvas_item_transform_xy_f (GimpCanvasItem *item,
   gimp_display_shell_zoom_xy_f (item->private->shell, x, y, tx, ty);
 }
 
+/**
+ * gimp_canvas_item_transform_distance:
+ * @item: a #GimpCanvasItem
+ * @x1:   start point X in image coordinates
+ * @y1:   start point Y in image coordinates
+ * @x2:   end point X in image coordinates
+ * @y2:   end point Y in image coordinates
+ *
+ * If you just need to compare distances, consider to use
+ * gimp_canvas_item_transform_distance_square() instead.
+ *
+ * Returns: the distance between the given points in display coordinates
+ **/
+gdouble
+gimp_canvas_item_transform_distance (GimpCanvasItem *item,
+                                     gdouble         x1,
+                                     gdouble         y1,
+                                     gdouble         x2,
+                                     gdouble         y2)
+{
+  return sqrt (gimp_canvas_item_transform_distance_square (item,
+                                                           x1, y1, x2, y2));
+}
+
+/**
+ * gimp_canvas_item_transform_distance_square:
+ * @item: a #GimpCanvasItem
+ * @x1:   start point X in image coordinates
+ * @y1:   start point Y in image coordinates
+ * @x2:   end point X in image coordinates
+ * @y2:   end point Y in image coordinates
+ *
+ * This function is more effective than
+ * gimp_canvas_item_transform_distance() as it doesn't perform a
+ * sqrt(). Use this if you just need to compare distances.
+ *
+ * Returns: the square of the distance between the given points in
+ *          display coordinates
+ **/
+gdouble
+gimp_canvas_item_transform_distance_square (GimpCanvasItem   *item,
+                                            gdouble           x1,
+                                            gdouble           y1,
+                                            gdouble           x2,
+                                            gdouble           y2)
+{
+  gdouble tx1, ty1;
+  gdouble tx2, ty2;
+
+  g_return_val_if_fail (GIMP_IS_CANVAS_ITEM (item), 0.0);
+
+  gimp_canvas_item_transform_xy_f (item, x1, y1, &tx1, &ty1);
+  gimp_canvas_item_transform_xy_f (item, x2, y2, &tx2, &ty2);
+
+  return SQR (tx2 - tx1) + SQR (ty2 - ty1);
+}
+
 
 /*  protected functions  */
 
diff --git a/app/display/gimpcanvasitem.h b/app/display/gimpcanvasitem.h
index eb855c6..4011274 100644
--- a/app/display/gimpcanvasitem.h
+++ b/app/display/gimpcanvasitem.h
@@ -113,6 +113,18 @@ void             gimp_canvas_item_transform_xy_f   (GimpCanvasItem   *item,
                                                     gdouble           y,
                                                     gdouble          *tx,
                                                     gdouble          *ty);
+gdouble          gimp_canvas_item_transform_distance
+                                                   (GimpCanvasItem   *item,
+                                                    gdouble           x1,
+                                                    gdouble           y1,
+                                                    gdouble           x2,
+                                                    gdouble           y2);
+gdouble          gimp_canvas_item_transform_distance_square
+                                                   (GimpCanvasItem   *item,
+                                                    gdouble           x1,
+                                                    gdouble           y1,
+                                                    gdouble           x2,
+                                                    gdouble           y2);
 
 
 /*  protected  */


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