caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 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 | #include "SkPathOpsLine.h" |
| 8 | |
| 9 | SkDLine SkDLine::subDivide(double t1, double t2) const { |
| 10 | SkDVector delta = tangent(); |
| 11 | SkDLine dst = {{{ |
| 12 | fPts[0].fX - t1 * delta.fX, fPts[0].fY - t1 * delta.fY}, { |
| 13 | fPts[0].fX - t2 * delta.fX, fPts[0].fY - t2 * delta.fY}}}; |
| 14 | return dst; |
| 15 | } |
| 16 | |
| 17 | // may have this below somewhere else already: |
| 18 | // copying here because I thought it was clever |
| 19 | |
| 20 | // Copyright 2001, softSurfer (www.softsurfer.com) |
| 21 | // This code may be freely used and modified for any purpose |
| 22 | // providing that this copyright notice is included with it. |
| 23 | // SoftSurfer makes no warranty for this code, and cannot be held |
| 24 | // liable for any real or imagined damage resulting from its use. |
| 25 | // Users of this code must verify correctness for their application. |
| 26 | |
| 27 | // Assume that a class is already given for the object: |
| 28 | // Point with coordinates {float x, y;} |
| 29 | //=================================================================== |
| 30 | |
| 31 | // isLeft(): tests if a point is Left|On|Right of an infinite line. |
| 32 | // Input: three points P0, P1, and P2 |
| 33 | // Return: >0 for P2 left of the line through P0 and P1 |
| 34 | // =0 for P2 on the line |
| 35 | // <0 for P2 right of the line |
| 36 | // See: the January 2001 Algorithm on Area of Triangles |
| 37 | // return (float) ((P1.x - P0.x)*(P2.y - P0.y) - (P2.x - P0.x)*(P1.y - P0.y)); |
| 38 | double SkDLine::isLeft(const SkDPoint& pt) const { |
| 39 | SkDVector p0 = fPts[1] - fPts[0]; |
| 40 | SkDVector p2 = pt - fPts[0]; |
| 41 | return p0.cross(p2); |
| 42 | } |
| 43 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame^] | 44 | // OPTIMIZE: assert if t is 0 or 1 (caller shouldn't pass only 0/1) |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 45 | SkDPoint SkDLine::xyAtT(double t) const { |
| 46 | double one_t = 1 - t; |
| 47 | SkDPoint result = { one_t * fPts[0].fX + t * fPts[1].fX, one_t * fPts[0].fY + t * fPts[1].fY }; |
| 48 | return result; |
| 49 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame^] | 50 | |
| 51 | double SkDLine::exactPoint(const SkDPoint& xy) const { |
| 52 | if (xy == fPts[0]) { // do cheapest test first |
| 53 | return 0; |
| 54 | } |
| 55 | if (xy == fPts[1]) { |
| 56 | return 1; |
| 57 | } |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | double SkDLine::nearPoint(const SkDPoint& xy) const { |
| 62 | if (!AlmostBetweenUlps(fPts[0].fX, xy.fX, fPts[1].fX) |
| 63 | || !AlmostBetweenUlps(fPts[0].fY, xy.fY, fPts[1].fY)) { |
| 64 | return -1; |
| 65 | } |
| 66 | // project a perpendicular ray from the point to the line; find the T on the line |
| 67 | SkDVector len = fPts[1] - fPts[0]; // the x/y magnitudes of the line |
| 68 | double denom = len.fX * len.fX + len.fY * len.fY; // see DLine intersectRay |
| 69 | SkDVector ab0 = xy - fPts[0]; |
| 70 | double numer = len.fX * ab0.fX + ab0.fY * len.fY; |
| 71 | if (!between(0, numer, denom)) { |
| 72 | return -1; |
| 73 | } |
| 74 | double t = numer / denom; |
| 75 | SkDPoint realPt = xyAtT(t); |
| 76 | SkDVector distU = xy - realPt; |
| 77 | double distSq = distU.fX * distU.fX + distU.fY * distU.fY; |
| 78 | double dist = sqrt(distSq); // OPTIMIZATION: can we compare against distSq instead ? |
| 79 | // find the ordinal in the original line with the largest unsigned exponent |
| 80 | double tiniest = SkTMin(SkTMin(SkTMin(fPts[0].fX, fPts[0].fY), fPts[1].fX), fPts[1].fY); |
| 81 | double largest = SkTMax(SkTMax(SkTMax(fPts[0].fX, fPts[0].fY), fPts[1].fX), fPts[1].fY); |
| 82 | largest = SkTMax(largest, -tiniest); |
| 83 | if (!AlmostEqualUlps(largest, largest + dist)) { // is the dist within ULPS tolerance? |
| 84 | return -1; |
| 85 | } |
| 86 | t = SkPinT(t); |
| 87 | SkASSERT(between(0, t, 1)); |
| 88 | return t; |
| 89 | } |
| 90 | |
| 91 | double SkDLine::ExactPointH(const SkDPoint& xy, double left, double right, double y) { |
| 92 | if (xy.fY == y) { |
| 93 | if (xy.fX == left) { |
| 94 | return 0; |
| 95 | } |
| 96 | if (xy.fX == right) { |
| 97 | return 1; |
| 98 | } |
| 99 | } |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | double SkDLine::NearPointH(const SkDPoint& xy, double left, double right, double y) { |
| 104 | if (!AlmostEqualUlps(xy.fY, y)) { |
| 105 | return -1; |
| 106 | } |
| 107 | if (!AlmostBetweenUlps(left, xy.fX, right)) { |
| 108 | return -1; |
| 109 | } |
| 110 | double t = (xy.fX - left) / (right - left); |
| 111 | t = SkPinT(t); |
| 112 | SkASSERT(between(0, t, 1)); |
| 113 | return t; |
| 114 | } |
| 115 | |
| 116 | double SkDLine::ExactPointV(const SkDPoint& xy, double top, double bottom, double x) { |
| 117 | if (xy.fX == x) { |
| 118 | if (xy.fY == top) { |
| 119 | return 0; |
| 120 | } |
| 121 | if (xy.fY == bottom) { |
| 122 | return 1; |
| 123 | } |
| 124 | } |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | double SkDLine::NearPointV(const SkDPoint& xy, double top, double bottom, double x) { |
| 129 | if (!AlmostEqualUlps(xy.fX, x)) { |
| 130 | return -1; |
| 131 | } |
| 132 | if (!AlmostBetweenUlps(top, xy.fY, bottom)) { |
| 133 | return -1; |
| 134 | } |
| 135 | double t = (xy.fY - top) / (bottom - top); |
| 136 | t = SkPinT(t); |
| 137 | SkASSERT(between(0, t, 1)); |
| 138 | return t; |
| 139 | } |