| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkRRect.h" | 8 #include "SkRRect.h" |
| 9 | 9 |
| 10 /////////////////////////////////////////////////////////////////////////////// | 10 /////////////////////////////////////////////////////////////////////////////// |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 return false; | 127 return false; |
| 128 } | 128 } |
| 129 | 129 |
| 130 if (kRect_Type == this->type()) { | 130 if (kRect_Type == this->type()) { |
| 131 // the 'fRect' test above was sufficient | 131 // the 'fRect' test above was sufficient |
| 132 return true; | 132 return true; |
| 133 } | 133 } |
| 134 | 134 |
| 135 // We know the point is inside the RR's bounds. The only way it can | 135 // We know the point is inside the RR's bounds. The only way it can |
| 136 // be out is if it outside one of the corners | 136 // be out is if it outside one of the corners |
| 137 return checkCornerContainment(x, y); |
| 138 } |
| 139 |
| 140 // This method determines if a point known to be inside the RRect's bounds is |
| 141 // inside all the corners. |
| 142 bool SkRRect::checkCornerContainment(SkScalar x, SkScalar y) const { |
| 137 SkPoint canonicalPt; // (x,y) translated to one of the quadrants | 143 SkPoint canonicalPt; // (x,y) translated to one of the quadrants |
| 138 int index; | 144 int index; |
| 139 | 145 |
| 140 if (kOval_Type == this->type()) { | 146 if (kOval_Type == this->type()) { |
| 141 canonicalPt.set(x - fRect.centerX(), y - fRect.centerY()); | 147 canonicalPt.set(x - fRect.centerX(), y - fRect.centerY()); |
| 142 index = kUpperLeft_Corner; // any corner will do in this case | 148 index = kUpperLeft_Corner; // any corner will do in this case |
| 143 } else { | 149 } else { |
| 144 if (x < fRect.fLeft + fRadii[kUpperLeft_Corner].fX && | 150 if (x < fRect.fLeft + fRadii[kUpperLeft_Corner].fX && |
| 145 y < fRect.fTop + fRadii[kUpperLeft_Corner].fY) { | 151 y < fRect.fTop + fRadii[kUpperLeft_Corner].fY) { |
| 146 // UL corner | 152 // UL corner |
| (...skipping 25 matching lines...) Expand all Loading... |
| 172 } else { | 178 } else { |
| 173 // not in any of the corners | 179 // not in any of the corners |
| 174 return true; | 180 return true; |
| 175 } | 181 } |
| 176 } | 182 } |
| 177 | 183 |
| 178 // A point is in an ellipse (in standard position) if: | 184 // A point is in an ellipse (in standard position) if: |
| 179 // x^2 y^2 | 185 // x^2 y^2 |
| 180 // ----- + ----- <= 1 | 186 // ----- + ----- <= 1 |
| 181 // a^2 b^2 | 187 // a^2 b^2 |
| 182 SkScalar dist = SkScalarDiv(SkScalarSquare(canonicalPt.fX), SkScalarSquare(
fRadii[index].fX)) + | 188 // or : |
| 183 SkScalarDiv(SkScalarSquare(canonicalPt.fY), SkScalarSquare(
fRadii[index].fY)); | 189 // b^2*x^2 + a^2*y^2 <= (ab)^2 |
| 184 return dist <= SK_Scalar1; | 190 SkScalar dist = SkScalarMul(SkScalarSquare(canonicalPt.fX), SkScalarSquare(
fRadii[index].fY)) + |
| 191 SkScalarMul(SkScalarSquare(canonicalPt.fY), SkScalarSquare(
fRadii[index].fX)); |
| 192 return dist <= SkScalarSquare(SkScalarMul(fRadii[index].fX, fRadii[index].fY
)); |
| 193 } |
| 194 |
| 195 bool SkRRect::contains(const SkRect& rect) const { |
| 196 if (!this->getBounds().contains(rect)) { |
| 197 // If 'rect' isn't contained by the RR's bounds then the |
| 198 // RR definitely doesn't contain it |
| 199 return false; |
| 200 } |
| 201 |
| 202 if (this->isRect()) { |
| 203 // the prior test was sufficient |
| 204 return true; |
| 205 } |
| 206 |
| 207 // At this point we know all four corners of 'rect' are inside the |
| 208 // bounds of of this RR. Check to make sure all the corners are inside |
| 209 // all the curves |
| 210 return this->checkCornerContainment(rect.fLeft, rect.fTop) && |
| 211 this->checkCornerContainment(rect.fRight, rect.fTop) && |
| 212 this->checkCornerContainment(rect.fRight, rect.fBottom) && |
| 213 this->checkCornerContainment(rect.fLeft, rect.fBottom); |
| 185 } | 214 } |
| 186 | 215 |
| 187 // There is a simplified version of this method in setRectXY | 216 // There is a simplified version of this method in setRectXY |
| 188 void SkRRect::computeType() const { | 217 void SkRRect::computeType() const { |
| 189 SkDEBUGCODE(this->validate();) | 218 SkDEBUGCODE(this->validate();) |
| 190 | 219 |
| 191 if (fRect.isEmpty()) { | 220 if (fRect.isEmpty()) { |
| 192 fType = kEmpty_Type; | 221 fType = kEmpty_Type; |
| 193 return; | 222 return; |
| 194 } | 223 } |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 SkASSERT(!allRadiiZero && !allRadiiSame && !allCornersSquare); | 352 SkASSERT(!allRadiiZero && !allRadiiSame && !allCornersSquare); |
| 324 break; | 353 break; |
| 325 case kUnknown_Type: | 354 case kUnknown_Type: |
| 326 // no limits on this | 355 // no limits on this |
| 327 break; | 356 break; |
| 328 } | 357 } |
| 329 } | 358 } |
| 330 #endif // SK_DEBUG | 359 #endif // SK_DEBUG |
| 331 | 360 |
| 332 /////////////////////////////////////////////////////////////////////////////// | 361 /////////////////////////////////////////////////////////////////////////////// |
| OLD | NEW |