OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2012 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 #ifndef SkPathOpsPoint_DEFINED |
| 8 #define SkPathOpsPoint_DEFINED |
| 9 |
| 10 #include "SkPathOpsTypes.h" |
| 11 #include "SkPoint.h" |
| 12 |
| 13 struct SkDVector { |
| 14 double fX, fY; |
| 15 |
| 16 friend SkDPoint operator+(const SkDPoint& a, const SkDVector& b); |
| 17 |
| 18 void operator+=(const SkDVector& v) { |
| 19 fX += v.fX; |
| 20 fY += v.fY; |
| 21 } |
| 22 |
| 23 void operator-=(const SkDVector& v) { |
| 24 fX -= v.fX; |
| 25 fY -= v.fY; |
| 26 } |
| 27 |
| 28 void operator/=(const double s) { |
| 29 fX /= s; |
| 30 fY /= s; |
| 31 } |
| 32 |
| 33 void operator*=(const double s) { |
| 34 fX *= s; |
| 35 fY *= s; |
| 36 } |
| 37 |
| 38 SkVector asSkVector() const { |
| 39 SkVector v = {SkDoubleToScalar(fX), SkDoubleToScalar(fY)}; |
| 40 return v; |
| 41 } |
| 42 |
| 43 double cross(const SkDVector& a) const { |
| 44 return fX * a.fY - fY * a.fX; |
| 45 } |
| 46 |
| 47 double dot(const SkDVector& a) const { |
| 48 return fX * a.fX + fY * a.fY; |
| 49 } |
| 50 |
| 51 double length() const { |
| 52 return sqrt(lengthSquared()); |
| 53 } |
| 54 |
| 55 double lengthSquared() const { |
| 56 return fX * fX + fY * fY; |
| 57 } |
| 58 }; |
| 59 |
| 60 struct SkDPoint { |
| 61 double fX; |
| 62 double fY; |
| 63 |
| 64 void set(const SkPoint& pt) { |
| 65 fX = pt.fX; |
| 66 fY = pt.fY; |
| 67 } |
| 68 |
| 69 friend SkDVector operator-(const SkDPoint& a, const SkDPoint& b); |
| 70 |
| 71 friend bool operator==(const SkDPoint& a, const SkDPoint& b) { |
| 72 return a.fX == b.fX && a.fY == b.fY; |
| 73 } |
| 74 |
| 75 friend bool operator!=(const SkDPoint& a, const SkDPoint& b) { |
| 76 return a.fX != b.fX || a.fY != b.fY; |
| 77 } |
| 78 |
| 79 void operator=(const SkPoint& pt) { |
| 80 fX = pt.fX; |
| 81 fY = pt.fY; |
| 82 } |
| 83 |
| 84 |
| 85 void operator+=(const SkDVector& v) { |
| 86 fX += v.fX; |
| 87 fY += v.fY; |
| 88 } |
| 89 |
| 90 void operator-=(const SkDVector& v) { |
| 91 fX -= v.fX; |
| 92 fY -= v.fY; |
| 93 } |
| 94 |
| 95 // note: this can not be implemented with |
| 96 // return approximately_equal(a.fY, fY) && approximately_equal(a.fX, fX); |
| 97 // because that will not take the magnitude of the values |
| 98 bool approximatelyEqual(const SkDPoint& a) const { |
| 99 double denom = SkTMax(fabs(fX), SkTMax(fabs(fY), SkTMax(fabs(a.fX), fabs
(a.fY)))); |
| 100 if (denom == 0) { |
| 101 return true; |
| 102 } |
| 103 double inv = 1 / denom; |
| 104 return approximately_equal(fX * inv, a.fX * inv) |
| 105 && approximately_equal(fY * inv, a.fY * inv); |
| 106 } |
| 107 |
| 108 bool approximatelyEqual(const SkPoint& a) const { |
| 109 double denom = SkTMax(fabs(fX), SkTMax(fabs(fY), SkTMax(fabs(a.fX), fabs
(a.fY)))); |
| 110 if (denom == 0) { |
| 111 return true; |
| 112 } |
| 113 double inv = 1 / denom; |
| 114 return approximately_equal(fX * inv, a.fX * inv) |
| 115 && approximately_equal(fY * inv, a.fY * inv); |
| 116 } |
| 117 |
| 118 bool approximatelyEqualHalf(const SkDPoint& a) const { |
| 119 double denom = SkTMax(fabs(fX), SkTMax(fabs(fY), SkTMax(fabs(a.fX), fabs
(a.fY)))); |
| 120 if (denom == 0) { |
| 121 return true; |
| 122 } |
| 123 double inv = 1 / denom; |
| 124 return approximately_equal_half(fX * inv, a.fX * inv) |
| 125 && approximately_equal_half(fY * inv, a.fY * inv); |
| 126 } |
| 127 |
| 128 bool approximatelyZero() const { |
| 129 return approximately_zero(fX) && approximately_zero(fY); |
| 130 } |
| 131 |
| 132 SkPoint asSkPoint() const { |
| 133 SkPoint pt = {SkDoubleToScalar(fX), SkDoubleToScalar(fY)}; |
| 134 return pt; |
| 135 } |
| 136 |
| 137 double distance(const SkDPoint& a) const { |
| 138 SkDVector temp = *this - a; |
| 139 return temp.length(); |
| 140 } |
| 141 |
| 142 double distanceSquared(const SkDPoint& a) const { |
| 143 SkDVector temp = *this - a; |
| 144 return temp.lengthSquared(); |
| 145 } |
| 146 |
| 147 double moreRoughlyEqual(const SkDPoint& a) const { |
| 148 return more_roughly_equal(a.fY, fY) && more_roughly_equal(a.fX, fX); |
| 149 } |
| 150 |
| 151 double roughlyEqual(const SkDPoint& a) const { |
| 152 return roughly_equal(a.fY, fY) && roughly_equal(a.fX, fX); |
| 153 } |
| 154 }; |
| 155 |
| 156 #endif |
OLD | NEW |