Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: src/core/SkRRect.cpp

Issue 14200044: RoundRect contains (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Fixed test Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 } 109 }
110 110
111 // At this point we're either oval, simple, or complex (not empty or rect) 111 // At this point we're either oval, simple, or complex (not empty or rect)
112 // but we lazily resolve the type to avoid the work if the information 112 // but we lazily resolve the type to avoid the work if the information
113 // isn't required. 113 // isn't required.
114 fType = (SkRRect::Type) kUnknown_Type; 114 fType = (SkRRect::Type) kUnknown_Type;
115 115
116 SkDEBUGCODE(this->validate();) 116 SkDEBUGCODE(this->validate();)
117 } 117 }
118 118
119 bool SkRRect::contains(SkScalar x, SkScalar y) const { 119 namespace {
120
121 bool loose_contains(const SkRect& rect, SkScalar x, SkScalar y) {
122 return !rect.isEmpty() &&
123 rect.fLeft <= x && x <= rect.fRight &&
124 rect.fTop <= y && y <= rect.fBottom;
125 }
126
127 };
128
129 bool SkRRect::contains(SkScalar x, SkScalar y, bool loose) const {
120 SkDEBUGCODE(this->validate();) 130 SkDEBUGCODE(this->validate();)
121 131
122 if (kEmpty_Type == this->type()) { 132 if (kEmpty_Type == this->type()) {
123 return false; 133 return false;
124 } 134 }
125 135
126 if (!fRect.contains(x, y)) { 136 if (loose) {
127 return false; 137 // points on the left and right edges of a rect have a slightly
138 // different containment definition (since they technically aren't
139 // "in" the rect)
140 if (!loose_contains(fRect, x, y)) {
141 return false;
142 }
143 } else {
144 if (!fRect.contains(x, y)) {
145 return false;
146 }
128 } 147 }
129 148
130 if (kRect_Type == this->type()) { 149 if (kRect_Type == this->type()) {
131 // the 'fRect' test above was sufficient 150 // the 'fRect' test above was sufficient
132 return true; 151 return true;
133 } 152 }
134 153
135 // We know the point is inside the RR's bounds. The only way it can 154 // 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 155 // be out is if it outside one of the corners
137 SkPoint canonicalPt; // (x,y) translated to one of the quadrants 156 SkPoint canonicalPt; // (x,y) translated to one of the quadrants
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 SkASSERT(canonicalPt.fX > 0 && canonicalPt.fY > 0); 190 SkASSERT(canonicalPt.fX > 0 && canonicalPt.fY > 0);
172 } else { 191 } else {
173 // not in any of the corners 192 // not in any of the corners
174 return true; 193 return true;
175 } 194 }
176 } 195 }
177 196
178 // A point is in an ellipse (in standard position) if: 197 // A point is in an ellipse (in standard position) if:
179 // x^2 y^2 198 // x^2 y^2
180 // ----- + ----- <= 1 199 // ----- + ----- <= 1
181 // a^2 b^2 200 // a^2 b^2
jvanverth1 2013/04/24 15:29:30 This can be rewritten as b^2*x^2 + a^2*y^2 <= (ab)
robertphillips 2013/04/24 16:11:57 Done.
182 SkScalar dist = SkScalarDiv(SkScalarSquare(canonicalPt.fX), SkScalarSquare( fRadii[index].fX)) + 201 SkScalar dist = SkScalarDiv(SkScalarSquare(canonicalPt.fX), SkScalarSquare( fRadii[index].fX)) +
183 SkScalarDiv(SkScalarSquare(canonicalPt.fY), SkScalarSquare( fRadii[index].fY)); 202 SkScalarDiv(SkScalarSquare(canonicalPt.fY), SkScalarSquare( fRadii[index].fY));
184 return dist <= SK_Scalar1; 203 return dist <= SK_Scalar1;
185 } 204 }
186 205
206 bool SkRRect::contains(const SkRect& rect) const {
207 if (!this->getBounds().contains(rect)) {
208 // If 'rect' isn't contained by the RR's bounds then the
209 // RR definitely doesn't contain it
210 return false;
211 }
212
213 if (this->isRect()) {
214 // the prior test was sufficient
215 return true;
216 }
217
jvanverth1 2013/04/24 15:29:30 You're checking to see if the corners are in the b
robertphillips 2013/04/24 16:11:57 Done.
218 return this->contains(rect.fLeft, rect.fTop, false) &&
219 this->contains(rect.fRight, rect.fTop, true) &&
220 this->contains(rect.fRight, rect.fBottom, true) &&
221 this->contains(rect.fLeft, rect.fBottom, true);
222 }
223
187 // There is a simplified version of this method in setRectXY 224 // There is a simplified version of this method in setRectXY
188 void SkRRect::computeType() const { 225 void SkRRect::computeType() const {
189 SkDEBUGCODE(this->validate();) 226 SkDEBUGCODE(this->validate();)
190 227
191 if (fRect.isEmpty()) { 228 if (fRect.isEmpty()) {
192 fType = kEmpty_Type; 229 fType = kEmpty_Type;
193 return; 230 return;
194 } 231 }
195 232
196 bool allRadiiEqual = true; // are all x radii equal and all y radii? 233 bool allRadiiEqual = true; // are all x radii equal and all y radii?
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 SkASSERT(!allRadiiZero && !allRadiiSame && !allCornersSquare); 360 SkASSERT(!allRadiiZero && !allRadiiSame && !allCornersSquare);
324 break; 361 break;
325 case kUnknown_Type: 362 case kUnknown_Type:
326 // no limits on this 363 // no limits on this
327 break; 364 break;
328 } 365 }
329 } 366 }
330 #endif // SK_DEBUG 367 #endif // SK_DEBUG
331 368
332 /////////////////////////////////////////////////////////////////////////////// 369 ///////////////////////////////////////////////////////////////////////////////
OLDNEW
« no previous file with comments | « include/core/SkRRect.h ('k') | tests/RoundRectTest.cpp » ('j') | tests/RoundRectTest.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698