add centerX() and centerY() to SkIRect (because they made me)
authorreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 8 Nov 2012 22:36:19 +0000 (22:36 +0000)
committerreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 8 Nov 2012 22:36:19 +0000 (22:36 +0000)
Review URL: https://codereview.appspot.com/6826078

git-svn-id: http://skia.googlecode.com/svn/trunk@6360 2bbb7eff-a529-9590-31e7-b0007b416f81

include/core/SkRect.h
tests/InfRectTest.cpp

index 04096f3..a754719 100644 (file)
@@ -72,6 +72,24 @@ struct SK_API SkIRect {
     int height() const { return fBottom - fTop; }
 
     /**
+     *  Since the center of an integer rect may fall on a factional value, this
+     *  method is defined to return (right + left) >> 1.
+     *
+     *  This is a specific "truncation" of the average, which is different than
+     *  (right + left) / 2 when the sum is negative.
+     */
+    int centerX() const { return (fRight + fLeft) >> 1; }
+    
+    /**
+     *  Since the center of an integer rect may fall on a factional value, this
+     *  method is defined to return (bottom + top) >> 1
+     *
+     *  This is a specific "truncation" of the average, which is different than
+     *  (bottom + top) / 2 when the sum is negative.
+     */
+    int centerY() const { return (fBottom + fTop) >> 1; }
+    
+    /**
      *  Return true if the rectangle's width or height are <= 0
      */
     bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
index 1dc6ca7..f1b2677 100644 (file)
@@ -6,6 +6,7 @@
  * found in the LICENSE file.
  */
 #include "Test.h"
+#include "SkRandom.h"
 #include "SkRect.h"
 
 #ifdef SK_SCALAR_IS_FLOAT
@@ -14,6 +15,37 @@ static float make_zero() {
 }
 #endif
 
+static void test_center(skiatest::Reporter* reporter) {
+    static const struct {
+        SkIRect  fRect;
+        SkIPoint fCenter;
+    } data[] = {
+        { { 0, 0, 0, 0 }, { 0, 0 } },
+        { { 0, 0, 1, 1 }, { 0, 0 } },
+        { { -1, -1, 0, 0 }, { -1, -1 } },
+        { { 0, 0, 10, 7 }, { 5, 3 } },
+        { { 0, 0, 11, 6 }, { 5, 3 } },
+    };
+    for (size_t index = 0; index < SK_ARRAY_COUNT(data); ++index) {
+        REPORTER_ASSERT(reporter,
+                        data[index].fRect.centerX() == data[index].fCenter.x());
+        REPORTER_ASSERT(reporter,
+                        data[index].fRect.centerY() == data[index].fCenter.y());
+    }
+
+    SkRandom rand;
+    for (int i = 0; i < 10000; ++i) {
+        SkIRect r;
+        
+        r.set(rand.nextS() >> 2, rand.nextS() >> 2,
+              rand.nextS() >> 2, rand.nextS() >> 2);
+        int cx = r.centerX();
+        int cy = r.centerY();
+        REPORTER_ASSERT(reporter, (r.left() + r.right() >> 1) == cx);
+        REPORTER_ASSERT(reporter, (r.top() + r.bottom() >> 1) == cy);
+    }
+}
+
 static void check_invalid(skiatest::Reporter* reporter,
                           SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
     SkRect rect;
@@ -47,6 +79,8 @@ static void TestInfRect(skiatest::Reporter* reporter) {
         check_invalid(reporter, small, invalid[i], big, big);
         check_invalid(reporter, invalid[i], small, big, big);
     }
+    
+    test_center(reporter);
 }
 
 // need tests for SkStrSearch