change SkPoint::setLength to set itself to (0,0) if it starting length is degenerate.
authorreed <reed@google.com>
Tue, 9 Dec 2014 19:50:32 +0000 (11:50 -0800)
committerCommit bot <commit-bot@chromium.org>
Tue, 9 Dec 2014 19:50:33 +0000 (11:50 -0800)
BUG=skia:3203
TBR=caryclark

Review URL: https://codereview.chromium.org/785933003

include/core/SkPoint.h
src/core/SkPath.cpp
src/core/SkPoint.cpp
tests/PointTest.cpp

index 18b371f..323c824 100644 (file)
@@ -258,25 +258,25 @@ struct SK_API SkPoint {
 
     /** Set the point (vector) to be unit-length in the same direction as it
         already points.  If the point has a degenerate length (i.e. nearly 0)
-        then return false and do nothing; otherwise return true.
+        then set it to (0,0) and return false; otherwise return true.
     */
     bool normalize();
 
     /** Set the point (vector) to be unit-length in the same direction as the
         x,y params. If the vector (x,y) has a degenerate length (i.e. nearly 0)
-        then return false and do nothing, otherwise return true.
+        then set it to (0,0) and return false, otherwise return true.
     */
     bool setNormalize(SkScalar x, SkScalar y);
 
     /** Scale the point (vector) to have the specified length, and return that
         length. If the original length is degenerately small (nearly zero),
-        do nothing and return false, otherwise return true.
+        set it to (0,0) and return false, otherwise return true.
     */
     bool setLength(SkScalar length);
 
     /** Set the point (vector) to have the specified length in the same
      direction as (x,y). If the vector (x,y) has a degenerate length
-     (i.e. nearly 0) then return false and do nothing, otherwise return true.
+     (i.e. nearly 0) then set it to (0,0) and return false, otherwise return true.
     */
     bool setLength(SkScalar x, SkScalar y, SkScalar length);
 
@@ -423,7 +423,7 @@ struct SK_API SkPoint {
     static SkScalar Length(SkScalar x, SkScalar y);
 
     /** Normalize pt, returning its previous length. If the prev length is too
-        small (degenerate), return 0 and leave pt unchanged. This uses the same
+        small (degenerate), set pt to (0,0) and return 0. This uses the same
         tolerance as CanNormalize.
 
         Note that this method may be significantly more expensive than
index a08abbd..2cb33e2 100644 (file)
@@ -1287,7 +1287,12 @@ void SkPath::addArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle
 */
 void SkPath::arcTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
                    SkScalar radius) {
-    SkVector    before, after;
+    if (radius == 0) {
+        this->lineTo(x1, y1);
+        return;
+    }
+
+    SkVector before, after;
 
     // need to know our prev pt so we can construct tangent vectors
     {
@@ -1295,12 +1300,6 @@ void SkPath::arcTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
         this->getLastPt(&start);
         // Handle degenerate cases by adding a line to the first point and
         // bailing out.
-        if ((x1 == start.fX && y1 == start.fY) ||
-            (x1 == x2 && y1 == y2) ||
-            radius == 0) {
-            this->lineTo(x1, y1);
-            return;
-        }
         before.setNormalize(x1 - start.fX, y1 - start.fY);
         after.setNormalize(x2 - x1, y2 - y1);
     }
index 2970e35..20bc336 100644 (file)
@@ -100,6 +100,7 @@ SkScalar SkPoint::Normalize(SkPoint* pt) {
     float y = pt->fY;
     float mag2;
     if (isLengthNearlyZero(x, y, &mag2)) {
+        pt->set(0, 0);
         return 0;
     }
 
@@ -147,6 +148,7 @@ SkScalar SkPoint::Length(SkScalar dx, SkScalar dy) {
 bool SkPoint::setLength(float x, float y, float length) {
     float mag2;
     if (isLengthNearlyZero(x, y, &mag2)) {
+        this->set(0, 0);
         return false;
     }
 
@@ -183,6 +185,7 @@ bool SkPoint::setLengthFast(float length) {
 bool SkPoint::setLengthFast(float x, float y, float length) {
     float mag2;
     if (isLengthNearlyZero(x, y, &mag2)) {
+        this->set(0, 0);
         return false;
     }
 
index fed443a..08ce720 100644 (file)
@@ -108,13 +108,13 @@ static void test_overflow(skiatest::Reporter* reporter) {
 // report failure if we try to normalize them.
 static void test_underflow(skiatest::Reporter* reporter) {
     SkPoint pt = { 1.0e-37f, 1.0e-37f };
-    SkPoint copy = pt;
+    const SkPoint empty = { 0, 0 };
 
     REPORTER_ASSERT(reporter, 0 == SkPoint::Normalize(&pt));
-    REPORTER_ASSERT(reporter, pt == copy);  // pt is unchanged
+    REPORTER_ASSERT(reporter, pt == empty);
 
     REPORTER_ASSERT(reporter, !pt.setLength(SK_Scalar1));
-    REPORTER_ASSERT(reporter, pt == copy);  // pt is unchanged
+    REPORTER_ASSERT(reporter, pt == empty);
 }
 
 DEF_TEST(Point, reporter) {