eina: add api for assessing the relative position of two rectangles
authorMarcel Hollerbach <marcel-hollerbach@t-online.de>
Wed, 21 Dec 2016 00:04:29 +0000 (16:04 -0800)
committerCedric BAIL <cedric@osg.samsung.com>
Wed, 21 Dec 2016 00:39:30 +0000 (16:39 -0800)
Summary:
the api returns if a rectangle is positioned above/below/right or left
of a other rectangle.

Code which does simular things are part of verne and e, i think its a good idea to provide api for that.

Test Plan: Just run the test suite

Reviewers: raster, jpeg, cedric

Reviewed By: cedric

Differential Revision: https://phab.enlightenment.org/D4489

Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
src/lib/eina/eina_rectangle.c
src/lib/eina/eina_rectangle.h
src/tests/eina/eina_test_rectangle.c

index 4a235da..a3e8bff 100644 (file)
@@ -894,3 +894,20 @@ eina_rectangle_pool_geometry_get(Eina_Rectangle_Pool *pool, int *w, int *h)
 
    return EINA_TRUE;
 }
+
+EAPI Eina_Rectangle_Outside
+eina_rectangle_outside_position(Eina_Rectangle *rect1, Eina_Rectangle *rect2)
+{
+   Eina_Rectangle_Outside ret = 0;
+
+   if (rect1->y > rect2->y)
+     ret |= EINA_RECTANGLE_OUTSIDE_TOP;
+   if (rect1->x > rect2->x)
+     ret |= EINA_RECTANGLE_OUTSIDE_LEFT;
+   if (rect1->y + rect1->h < rect2->y + rect2->h)
+     ret |= EINA_RECTANGLE_OUTSIDE_BOTTOM;
+   if (rect1->x + rect1->w < rect2->x + rect2->w)
+     ret |= EINA_RECTANGLE_OUTSIDE_RIGHT;
+
+   return ret;
+}
index 2b600c6..fce0470 100644 (file)
@@ -75,6 +75,19 @@ typedef enum {
 } Eina_Rectangle_Packing;
 
 /**
+ * @typedef Eina_Rectangle_Outside
+ * Enumeration gives the positions where a rectangle can be outside a other rectangle
+ * @since 1.19
+ */
+typedef enum {
+    EINA_RECTANGLE_OUTSIDE_TOP = 1,
+    EINA_RECTANGLE_OUTSIDE_LEFT = 2,
+    EINA_RECTANGLE_OUTSIDE_BOTTOM = 4,
+    EINA_RECTANGLE_OUTSIDE_RIGHT = 8
+} Eina_Rectangle_Outside;
+
+
+/**
  * @brief Check if the given spans intersect.
  *
  * @param c1 The column of the first span.
@@ -507,6 +520,17 @@ EAPI void            eina_rectangle_free(Eina_Rectangle *rect) EINA_ARG_NONNULL(
  */
 EAPI void            eina_rectangle_pool_packing_set(Eina_Rectangle_Pool *pool,Eina_Rectangle_Packing type) EINA_ARG_NONNULL(1);
 
+/**
+ * @brief calculate where rect2 is outside of rect1
+ *
+ * @param rect1 the rect to use as anchor
+ * @param rect2 the rect to look for outside positions
+ *
+ * @return A or'ed map of Eina_Rectangle_Outside values
+ * @since 1.19
+ */
+EAPI Eina_Rectangle_Outside eina_rectangle_outside_position(Eina_Rectangle *rect1, Eina_Rectangle *rect2);
+
 #include "eina_inline_rectangle.x"
 
 /**
index fe21bf7..a6891f3 100644 (file)
@@ -196,10 +196,28 @@ START_TEST(eina_rectangle_union_intersect)
 }
 END_TEST
 
+START_TEST(eina_rectangle_position_test)
+{
+    Eina_Rectangle middle, top, down, right, left;
+    EINA_RECTANGLE_SET(&middle, -1, -1, 2.0, 2.0);
+    EINA_RECTANGLE_SET(&top,    -1, -2, 2.0, 2.0);
+    EINA_RECTANGLE_SET(&right,   0, -1, 2.0, 2.0);
+    EINA_RECTANGLE_SET(&left,   -2, -1, 2.0, 2.0);
+    EINA_RECTANGLE_SET(&down,   -1,  0, 2.0, 2.0);
+
+    ck_assert_int_eq(eina_rectangle_outside_position(&middle, &top), EINA_RECTANGLE_OUTSIDE_TOP) ;
+    ck_assert_int_eq(eina_rectangle_outside_position(&middle, &down), EINA_RECTANGLE_OUTSIDE_BOTTOM) ;
+    ck_assert_int_eq(eina_rectangle_outside_position(&middle, &right), EINA_RECTANGLE_OUTSIDE_RIGHT) ;
+    ck_assert_int_eq(eina_rectangle_outside_position(&middle, &left), EINA_RECTANGLE_OUTSIDE_LEFT) ;
+
+}
+END_TEST
+
 void
 eina_test_rectangle(TCase *tc)
 {
    tcase_add_test(tc, eina_rectangle_pool);
    tcase_add_test(tc, eina_rectangle_pool_skyline);
    tcase_add_test(tc, eina_rectangle_union_intersect);
+   tcase_add_test(tc, eina_rectangle_position_test);
 }