diff options
-rw-r--r-- | include/core/SkPoint.h | 19 | ||||
-rw-r--r-- | src/core/SkPoint.cpp | 11 |
2 files changed, 29 insertions, 1 deletions
diff --git a/include/core/SkPoint.h b/include/core/SkPoint.h index d23f696334..bdb30d99ee 100644 --- a/include/core/SkPoint.h +++ b/include/core/SkPoint.h @@ -26,6 +26,12 @@ */ struct SkIPoint { int32_t fX, fY; + + static SkIPoint Make(int32_t x, int32_t y) { + SkIPoint pt; + pt.set(x, y); + return pt; + } /** Set the x and y values of the point. */ void set(int32_t x, int32_t y) { fX = x; fY = y; } @@ -122,6 +128,12 @@ struct SkIPoint { struct SkPoint { SkScalar fX, fY; + static SkPoint Make(SkScalar x, SkScalar y) { + SkPoint pt; + pt.set(x, y); + return pt; + } + /** Set the point's X and Y coordinates */ void set(SkScalar x, SkScalar y) { fX = x; fY = y; } @@ -262,7 +274,12 @@ struct SkPoint { /** Returns the euclidian distance from (0,0) to (x,y) */ 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. + */ + static SkScalar Normalize(SkPoint* pt); + /** Returns the euclidian distance between a and b */ static SkScalar Distance(const SkPoint& a, const SkPoint& b) { diff --git a/src/core/SkPoint.cpp b/src/core/SkPoint.cpp index 704c2baa63..feca12f7ff 100644 --- a/src/core/SkPoint.cpp +++ b/src/core/SkPoint.cpp @@ -79,6 +79,17 @@ SkScalar SkPoint::Length(SkScalar dx, SkScalar dy) { return sk_float_sqrt(dx * dx + dy * dy); } +SkScalar SkPoint::Normalize(SkPoint* pt) { + float mag = SkPoint::Length(pt->fX, pt->fY); + if (mag > kNearlyZero) { + float scale = 1 / mag; + pt->fX *= scale; + pt->fY *= scale; + return mag; + } + return 0; +} + bool SkPoint::setLength(float x, float y, float length) { float mag = sk_float_sqrt(x * x + y * y); if (mag > kNearlyZero) { |