OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
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 #ifndef SkPoint_DEFINED | 8 #ifndef SkPoint_DEFINED |
9 #define SkPoint_DEFINED | 9 #define SkPoint_DEFINED |
10 | 10 |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 | 209 |
210 /** Return the euclidian distance from (0,0) to the point | 210 /** Return the euclidian distance from (0,0) to the point |
211 */ | 211 */ |
212 SkScalar length() const { return SkPoint::Length(fX, fY); } | 212 SkScalar length() const { return SkPoint::Length(fX, fY); } |
213 SkScalar distanceToOrigin() const { return this->length(); } | 213 SkScalar distanceToOrigin() const { return this->length(); } |
214 | 214 |
215 /** | 215 /** |
216 * Return true if the computed length of the vector is >= the internal | 216 * Return true if the computed length of the vector is >= the internal |
217 * tolerance (used to avoid dividing by tiny values). | 217 * tolerance (used to avoid dividing by tiny values). |
218 */ | 218 */ |
219 static bool CanNormalize(SkScalar dx, SkScalar dy) | 219 static bool CanNormalize(SkScalar dx, SkScalar dy) { |
220 #ifdef SK_SCALAR_IS_FLOAT | 220 // Simple enough (and performance critical sometimes) so we inline it. |
221 // Simple enough (and performance critical sometimes) so we inline it. | 221 return (dx*dx + dy*dy) > (SK_ScalarNearlyZero * SK_ScalarNearlyZero); |
222 { return (dx*dx + dy*dy) > (SK_ScalarNearlyZero * SK_ScalarNearlyZero); } | 222 } |
223 #else | |
224 ; | |
225 #endif | |
226 | 223 |
227 bool canNormalize() const { | 224 bool canNormalize() const { |
228 return CanNormalize(fX, fY); | 225 return CanNormalize(fX, fY); |
229 } | 226 } |
230 | 227 |
231 /** Set the point (vector) to be unit-length in the same direction as it | 228 /** Set the point (vector) to be unit-length in the same direction as it |
232 already points. If the point has a degenerate length (i.e. nearly 0) | 229 already points. If the point has a degenerate length (i.e. nearly 0) |
233 then return false and do nothing; otherwise return true. | 230 then return false and do nothing; otherwise return true. |
234 */ | 231 */ |
235 bool normalize(); | 232 bool normalize(); |
236 | 233 |
237 /** Set the point (vector) to be unit-length in the same direction as the | 234 /** Set the point (vector) to be unit-length in the same direction as the |
238 x,y params. If the vector (x,y) has a degenerate length (i.e. nearly 0) | 235 x,y params. If the vector (x,y) has a degenerate length (i.e. nearly 0) |
239 then return false and do nothing, otherwise return true. | 236 then return false and do nothing, otherwise return true. |
240 */ | 237 */ |
241 bool setNormalize(SkScalar x, SkScalar y); | 238 bool setNormalize(SkScalar x, SkScalar y); |
242 | 239 |
243 /** Scale the point (vector) to have the specified length, and return that | 240 /** Scale the point (vector) to have the specified length, and return that |
244 length. If the original length is degenerately small (nearly zero), | 241 length. If the original length is degenerately small (nearly zero), |
245 do nothing and return false, otherwise return true. | 242 do nothing and return false, otherwise return true. |
246 */ | 243 */ |
247 bool setLength(SkScalar length); | 244 bool setLength(SkScalar length); |
248 | 245 |
249 /** Set the point (vector) to have the specified length in the same | 246 /** Set the point (vector) to have the specified length in the same |
250 direction as (x,y). If the vector (x,y) has a degenerate length | 247 direction as (x,y). If the vector (x,y) has a degenerate length |
251 (i.e. nearly 0) then return false and do nothing, otherwise return true. | 248 (i.e. nearly 0) then return false and do nothing, otherwise return true. |
252 */ | 249 */ |
253 bool setLength(SkScalar x, SkScalar y, SkScalar length); | 250 bool setLength(SkScalar x, SkScalar y, SkScalar length); |
254 | 251 |
| 252 /** Same as setLength, but favoring speed over accuracy. |
| 253 */ |
| 254 bool setLengthFast(SkScalar length); |
| 255 |
| 256 /** Same as setLength, but favoring speed over accuracy. |
| 257 */ |
| 258 bool setLengthFast(SkScalar x, SkScalar y, SkScalar length); |
| 259 |
255 /** Scale the point's coordinates by scale, writing the answer into dst. | 260 /** Scale the point's coordinates by scale, writing the answer into dst. |
256 It is legal for dst == this. | 261 It is legal for dst == this. |
257 */ | 262 */ |
258 void scale(SkScalar scale, SkPoint* dst) const; | 263 void scale(SkScalar scale, SkPoint* dst) const; |
259 | 264 |
260 /** Scale the point's coordinates by scale, writing the answer back into | 265 /** Scale the point's coordinates by scale, writing the answer back into |
261 the point. | 266 the point. |
262 */ | 267 */ |
263 void scale(SkScalar value) { this->scale(value, this); } | 268 void scale(SkScalar value) { this->scale(value, this); } |
264 | 269 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 */ | 314 */ |
310 void operator-=(const SkPoint& v) { | 315 void operator-=(const SkPoint& v) { |
311 fX -= v.fX; | 316 fX -= v.fX; |
312 fY -= v.fY; | 317 fY -= v.fY; |
313 } | 318 } |
314 | 319 |
315 /** | 320 /** |
316 * Returns true if both X and Y are finite (not infinity or NaN) | 321 * Returns true if both X and Y are finite (not infinity or NaN) |
317 */ | 322 */ |
318 bool isFinite() const { | 323 bool isFinite() const { |
319 #ifdef SK_SCALAR_IS_FLOAT | |
320 SkScalar accum = 0; | 324 SkScalar accum = 0; |
321 accum *= fX; | 325 accum *= fX; |
322 accum *= fY; | 326 accum *= fY; |
323 | 327 |
324 // accum is either NaN or it is finite (zero). | 328 // accum is either NaN or it is finite (zero). |
325 SkASSERT(0 == accum || !(accum == accum)); | 329 SkASSERT(0 == accum || !(accum == accum)); |
326 | 330 |
327 // value==value will be true iff value is not NaN | 331 // value==value will be true iff value is not NaN |
328 // TODO: is it faster to say !accum or accum==accum? | 332 // TODO: is it faster to say !accum or accum==accum? |
329 return accum == accum; | 333 return accum == accum; |
330 #else | |
331 // use bit-or for speed, since we don't care about short-circuting the | |
332 // tests, and we expect the common case will be that we need to check al
l. | |
333 int isNaN = (SK_FixedNaN == fX) | (SK_FixedNaN == fX)); | |
334 return !isNaN; | |
335 #endif | |
336 } | 334 } |
337 | 335 |
338 /** | 336 /** |
339 * Returns true if the point's coordinates equal (x,y) | 337 * Returns true if the point's coordinates equal (x,y) |
340 */ | 338 */ |
341 bool equals(SkScalar x, SkScalar y) const { | 339 bool equals(SkScalar x, SkScalar y) const { |
342 return fX == x && fY == y; | 340 return fX == x && fY == y; |
343 } | 341 } |
344 | 342 |
345 friend bool operator==(const SkPoint& a, const SkPoint& b) { | 343 friend bool operator==(const SkPoint& a, const SkPoint& b) { |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 | 502 |
505 /** | 503 /** |
506 * cast-safe way to treat the point as an array of (2) SkScalars. | 504 * cast-safe way to treat the point as an array of (2) SkScalars. |
507 */ | 505 */ |
508 const SkScalar* asScalars() const { return &fX; } | 506 const SkScalar* asScalars() const { return &fX; } |
509 }; | 507 }; |
510 | 508 |
511 typedef SkPoint SkVector; | 509 typedef SkPoint SkVector; |
512 | 510 |
513 #endif | 511 #endif |
OLD | NEW |