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 "SkIntersections.h" |
| 8 | #include "SkPathOpsLine.h" |
| 9 | |
| 10 | /* Determine the intersection point of two lines. This assumes the lines are not parallel, |
| 11 | and that that the lines are infinite. |
| 12 | From http://en.wikipedia.org/wiki/Line-line_intersection |
| 13 | */ |
| 14 | SkDPoint SkIntersections::Line(const SkDLine& a, const SkDLine& b) { |
| 15 | double axLen = a[1].fX - a[0].fX; |
| 16 | double ayLen = a[1].fY - a[0].fY; |
| 17 | double bxLen = b[1].fX - b[0].fX; |
| 18 | double byLen = b[1].fY - b[0].fY; |
| 19 | double denom = byLen * axLen - ayLen * bxLen; |
| 20 | SkASSERT(denom); |
| 21 | double term1 = a[1].fX * a[0].fY - a[1].fY * a[0].fX; |
| 22 | double term2 = b[1].fX * b[0].fY - b[1].fY * b[0].fX; |
| 23 | SkDPoint p; |
| 24 | p.fX = (term1 * bxLen - axLen * term2) / denom; |
| 25 | p.fY = (term1 * byLen - ayLen * term2) / denom; |
| 26 | return p; |
| 27 | } |
| 28 | |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 29 | int SkIntersections::cleanUpCoincidence() { |
| 30 | do { |
| 31 | int last = fUsed - 1; |
| 32 | for (int index = 0; index < last; ++index) { |
| 33 | if (fT[0][index] == fT[0][index + 1]) { |
| 34 | removeOne(index + (int) (fT[1][index] == 0 || fT[1][index] == 1)); |
| 35 | goto tryAgain; |
| 36 | } |
| 37 | } |
| 38 | for (int index = 0; index < last; ++index) { |
| 39 | if (fT[1][index] == fT[1][index + 1]) { |
| 40 | removeOne(index + (int) (fT[0][index] == 0 || fT[0][index] == 1)); |
| 41 | goto tryAgain; |
| 42 | } |
| 43 | } |
| 44 | return fUsed; |
| 45 | tryAgain: ; |
| 46 | } while (true); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void SkIntersections::cleanUpParallelLines(bool parallel) { |
| 50 | while (fUsed > 2) { |
| 51 | removeOne(1); |
| 52 | } |
| 53 | if (fUsed == 2 && !parallel) { |
| 54 | bool startMatch = fT[0][0] == 0 || fT[1][0] == 0 || fT[1][0] == 1; |
| 55 | bool endMatch = fT[0][1] == 1 || fT[1][1] == 0 || fT[1][1] == 1; |
| 56 | if ((!startMatch && !endMatch) || approximately_equal(fT[0][0], fT[0][1])) { |
| 57 | SkASSERT(startMatch || endMatch); |
| 58 | removeOne(endMatch); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void SkIntersections::computePoints(const SkDLine& line, int used) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 64 | fPt[0] = line.ptAtT(fT[0][0]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 65 | if ((fUsed = used) == 2) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 66 | fPt[1] = line.ptAtT(fT[0][1]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 67 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 68 | } |
| 69 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 70 | int SkIntersections::intersectRay(const SkDLine& a, const SkDLine& b) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 71 | fMax = 2; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 72 | SkDVector aLen = a[1] - a[0]; |
| 73 | SkDVector bLen = b[1] - b[0]; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 74 | /* Slopes match when denom goes to zero: |
| 75 | axLen / ayLen == bxLen / byLen |
| 76 | (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen |
| 77 | byLen * axLen == ayLen * bxLen |
| 78 | byLen * axLen - ayLen * bxLen == 0 ( == denom ) |
| 79 | */ |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 80 | double denom = bLen.fY * aLen.fX - aLen.fY * bLen.fX; |
| 81 | SkDVector ab0 = a[0] - b[0]; |
| 82 | double numerA = ab0.fY * bLen.fX - bLen.fY * ab0.fX; |
| 83 | double numerB = ab0.fY * aLen.fX - aLen.fY * ab0.fX; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 84 | #if 0 |
| 85 | if (!between(0, numerA, denom) || !between(0, numerB, denom)) { |
| 86 | fUsed = 0; |
| 87 | return 0; |
| 88 | } |
| 89 | #endif |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 90 | numerA /= denom; |
| 91 | numerB /= denom; |
| 92 | int used; |
| 93 | if (!approximately_zero(denom)) { |
| 94 | fT[0][0] = numerA; |
| 95 | fT[1][0] = numerB; |
| 96 | used = 1; |
| 97 | } else { |
| 98 | /* See if the axis intercepts match: |
| 99 | ay - ax * ayLen / axLen == by - bx * ayLen / axLen |
| 100 | axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen) |
| 101 | axLen * ay - ax * ayLen == axLen * by - bx * ayLen |
| 102 | */ |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 103 | if (!AlmostEqualUlps(aLen.fX * a[0].fY - aLen.fY * a[0].fX, |
| 104 | aLen.fX * b[0].fY - aLen.fY * b[0].fX)) { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 105 | return fUsed = 0; |
| 106 | } |
| 107 | // there's no great answer for intersection points for coincident rays, but return something |
| 108 | fT[0][0] = fT[1][0] = 0; |
| 109 | fT[1][0] = fT[1][1] = 1; |
| 110 | used = 2; |
| 111 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 112 | computePoints(a, used); |
| 113 | return fUsed; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 114 | } |
| 115 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 116 | // note that this only works if both lines are neither horizontal nor vertical |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 117 | int SkIntersections::intersect(const SkDLine& a, const SkDLine& b) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 118 | fMax = 3; // note that we clean up so that there is no more than two in the end |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 119 | // see if end points intersect the opposite line |
| 120 | double t; |
| 121 | for (int iA = 0; iA < 2; ++iA) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 122 | if ((t = b.exactPoint(a[iA])) >= 0) { |
| 123 | insert(iA, t, a[iA]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 124 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 125 | } |
| 126 | for (int iB = 0; iB < 2; ++iB) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 127 | if ((t = a.exactPoint(b[iB])) >= 0) { |
| 128 | insert(t, iB, b[iB]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 129 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 130 | } |
| 131 | /* Determine the intersection point of two line segments |
| 132 | Return FALSE if the lines don't intersect |
| 133 | from: http://paulbourke.net/geometry/lineline2d/ */ |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 134 | double axLen = a[1].fX - a[0].fX; |
| 135 | double ayLen = a[1].fY - a[0].fY; |
| 136 | double bxLen = b[1].fX - b[0].fX; |
| 137 | double byLen = b[1].fY - b[0].fY; |
| 138 | /* Slopes match when denom goes to zero: |
| 139 | axLen / ayLen == bxLen / byLen |
| 140 | (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen |
| 141 | byLen * axLen == ayLen * bxLen |
| 142 | byLen * axLen - ayLen * bxLen == 0 ( == denom ) |
| 143 | */ |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 144 | double axByLen = axLen * byLen; |
| 145 | double ayBxLen = ayLen * bxLen; |
| 146 | // detect parallel lines the same way here and in SkOpAngle operator < |
| 147 | // so that non-parallel means they are also sortable |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 148 | bool unparallel = fAllowNear ? NotAlmostEqualUlps(axByLen, ayBxLen) |
| 149 | : NotAlmostDequalUlps(axByLen, ayBxLen); |
| 150 | if (unparallel && fUsed == 0) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 151 | double ab0y = a[0].fY - b[0].fY; |
| 152 | double ab0x = a[0].fX - b[0].fX; |
| 153 | double numerA = ab0y * bxLen - byLen * ab0x; |
| 154 | double numerB = ab0y * axLen - ayLen * ab0x; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 155 | double denom = axByLen - ayBxLen; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 156 | if (between(0, numerA, denom) && between(0, numerB, denom)) { |
| 157 | fT[0][0] = numerA / denom; |
| 158 | fT[1][0] = numerB / denom; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 159 | computePoints(a, 1); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 160 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 161 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 162 | /* Allow tracking that both sets of end points are near each other -- the lines are entirely |
| 163 | coincident -- even when the end points are not exactly the same. |
| 164 | Mark this as a 'wild card' for the end points, so that either point is considered totally |
| 165 | coincident. Then, avoid folding the lines over each other, but allow either end to mate |
| 166 | to the next set of lines. |
| 167 | */ |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 168 | if (fAllowNear || !unparallel) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 169 | double aNearB[2]; |
| 170 | double bNearA[2]; |
| 171 | bool aNotB[2] = {false, false}; |
| 172 | bool bNotA[2] = {false, false}; |
| 173 | int nearCount = 0; |
| 174 | for (int index = 0; index < 2; ++index) { |
| 175 | aNearB[index] = t = b.nearPoint(a[index], &aNotB[index]); |
| 176 | nearCount += t >= 0; |
| 177 | bNearA[index] = t = a.nearPoint(b[index], &bNotA[index]); |
| 178 | nearCount += t >= 0; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 179 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 180 | if (nearCount > 0) { |
caryclark | 19eb3b2 | 2014-07-18 05:08:14 -0700 | [diff] [blame] | 181 | // Skip if each segment contributes to one end point. |
| 182 | if (nearCount != 2 || aNotB[0] == aNotB[1]) { |
| 183 | for (int iA = 0; iA < 2; ++iA) { |
| 184 | if (!aNotB[iA]) { |
| 185 | continue; |
| 186 | } |
| 187 | int nearer = aNearB[iA] > 0.5; |
| 188 | if (!bNotA[nearer]) { |
| 189 | continue; |
| 190 | } |
| 191 | SkASSERT(a[iA] != b[nearer]); |
| 192 | SkASSERT(iA == (bNearA[nearer] > 0.5)); |
| 193 | fNearlySame[iA] = true; |
| 194 | insertNear(iA, nearer, a[iA], b[nearer]); |
| 195 | aNearB[iA] = -1; |
| 196 | bNearA[nearer] = -1; |
| 197 | nearCount -= 2; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 198 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 199 | } |
| 200 | if (nearCount > 0) { |
| 201 | for (int iA = 0; iA < 2; ++iA) { |
| 202 | if (aNearB[iA] >= 0) { |
| 203 | insert(iA, aNearB[iA], a[iA]); |
| 204 | } |
| 205 | } |
| 206 | for (int iB = 0; iB < 2; ++iB) { |
| 207 | if (bNearA[iB] >= 0) { |
| 208 | insert(bNearA[iB], iB, b[iB]); |
| 209 | } |
| 210 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 214 | cleanUpParallelLines(!unparallel); |
| 215 | SkASSERT(fUsed <= 2); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 216 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 217 | } |
| 218 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 219 | static int horizontal_coincident(const SkDLine& line, double y) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 220 | double min = line[0].fY; |
| 221 | double max = line[1].fY; |
| 222 | if (min > max) { |
| 223 | SkTSwap(min, max); |
| 224 | } |
| 225 | if (min > y || max < y) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 226 | return 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 227 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 228 | if (AlmostEqualUlps(min, max) && max - min < fabs(line[0].fX - line[1].fX)) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 229 | return 2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 230 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 231 | return 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 232 | } |
| 233 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 234 | static double horizontal_intercept(const SkDLine& line, double y) { |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 235 | return SkPinT((y - line[0].fY) / (line[1].fY - line[0].fY)); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | int SkIntersections::horizontal(const SkDLine& line, double y) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 239 | fMax = 2; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 240 | int horizontalType = horizontal_coincident(line, y); |
| 241 | if (horizontalType == 1) { |
| 242 | fT[0][0] = horizontal_intercept(line, y); |
| 243 | } else if (horizontalType == 2) { |
| 244 | fT[0][0] = 0; |
| 245 | fT[0][1] = 1; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 246 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 247 | return fUsed = horizontalType; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 248 | } |
| 249 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 250 | int SkIntersections::horizontal(const SkDLine& line, double left, double right, |
| 251 | double y, bool flipped) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 252 | fMax = 3; // clean up parallel at the end will limit the result to 2 at the most |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 253 | // see if end points intersect the opposite line |
| 254 | double t; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 255 | const SkDPoint leftPt = { left, y }; |
| 256 | if ((t = line.exactPoint(leftPt)) >= 0) { |
| 257 | insert(t, (double) flipped, leftPt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 258 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 259 | if (left != right) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 260 | const SkDPoint rightPt = { right, y }; |
| 261 | if ((t = line.exactPoint(rightPt)) >= 0) { |
| 262 | insert(t, (double) !flipped, rightPt); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 263 | } |
| 264 | for (int index = 0; index < 2; ++index) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 265 | if ((t = SkDLine::ExactPointH(line[index], left, right, y)) >= 0) { |
| 266 | insert((double) index, flipped ? 1 - t : t, line[index]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 267 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 270 | int result = horizontal_coincident(line, y); |
| 271 | if (result == 1 && fUsed == 0) { |
| 272 | fT[0][0] = horizontal_intercept(line, y); |
| 273 | double xIntercept = line[0].fX + fT[0][0] * (line[1].fX - line[0].fX); |
| 274 | if (between(left, xIntercept, right)) { |
| 275 | fT[1][0] = (xIntercept - left) / (right - left); |
| 276 | if (flipped) { |
| 277 | // OPTIMIZATION: ? instead of swapping, pass original line, use [1].fX - [0].fX |
| 278 | for (int index = 0; index < result; ++index) { |
| 279 | fT[1][index] = 1 - fT[1][index]; |
| 280 | } |
| 281 | } |
caryclark@google.com | 1510726 | 2013-11-08 18:00:01 +0000 | [diff] [blame] | 282 | fPt[0].fX = xIntercept; |
| 283 | fPt[0].fY = y; |
| 284 | fUsed = 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 287 | if (fAllowNear || result == 2) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 288 | if ((t = line.nearPoint(leftPt, NULL)) >= 0) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 289 | insert(t, (double) flipped, leftPt); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 290 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 291 | if (left != right) { |
| 292 | const SkDPoint rightPt = { right, y }; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 293 | if ((t = line.nearPoint(rightPt, NULL)) >= 0) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 294 | insert(t, (double) !flipped, rightPt); |
| 295 | } |
| 296 | for (int index = 0; index < 2; ++index) { |
| 297 | if ((t = SkDLine::NearPointH(line[index], left, right, y)) >= 0) { |
| 298 | insert((double) index, flipped ? 1 - t : t, line[index]); |
| 299 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 303 | cleanUpParallelLines(result == 2); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 304 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 305 | } |
| 306 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 307 | static int vertical_coincident(const SkDLine& line, double x) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 308 | double min = line[0].fX; |
| 309 | double max = line[1].fX; |
| 310 | if (min > max) { |
| 311 | SkTSwap(min, max); |
| 312 | } |
caryclark@google.com | 0361032 | 2013-04-18 15:58:21 +0000 | [diff] [blame] | 313 | if (!precisely_between(min, x, max)) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 314 | return 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 315 | } |
| 316 | if (AlmostEqualUlps(min, max)) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 317 | return 2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 318 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 319 | return 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 320 | } |
| 321 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 322 | static double vertical_intercept(const SkDLine& line, double x) { |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 323 | return SkPinT((x - line[0].fX) / (line[1].fX - line[0].fX)); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | int SkIntersections::vertical(const SkDLine& line, double x) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 327 | fMax = 2; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 328 | int verticalType = vertical_coincident(line, x); |
| 329 | if (verticalType == 1) { |
| 330 | fT[0][0] = vertical_intercept(line, x); |
| 331 | } else if (verticalType == 2) { |
| 332 | fT[0][0] = 0; |
| 333 | fT[0][1] = 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 334 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 335 | return fUsed = verticalType; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | int SkIntersections::vertical(const SkDLine& line, double top, double bottom, |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 339 | double x, bool flipped) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 340 | fMax = 3; // cleanup parallel lines will bring this back line |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 341 | // see if end points intersect the opposite line |
| 342 | double t; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 343 | SkDPoint topPt = { x, top }; |
| 344 | if ((t = line.exactPoint(topPt)) >= 0) { |
| 345 | insert(t, (double) flipped, topPt); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 346 | } |
| 347 | if (top != bottom) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 348 | SkDPoint bottomPt = { x, bottom }; |
| 349 | if ((t = line.exactPoint(bottomPt)) >= 0) { |
| 350 | insert(t, (double) !flipped, bottomPt); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 351 | } |
| 352 | for (int index = 0; index < 2; ++index) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 353 | if ((t = SkDLine::ExactPointV(line[index], top, bottom, x)) >= 0) { |
| 354 | insert((double) index, flipped ? 1 - t : t, line[index]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 355 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 358 | int result = vertical_coincident(line, x); |
| 359 | if (result == 1 && fUsed == 0) { |
| 360 | fT[0][0] = vertical_intercept(line, x); |
| 361 | double yIntercept = line[0].fY + fT[0][0] * (line[1].fY - line[0].fY); |
| 362 | if (between(top, yIntercept, bottom)) { |
| 363 | fT[1][0] = (yIntercept - top) / (bottom - top); |
| 364 | if (flipped) { |
| 365 | // OPTIMIZATION: instead of swapping, pass original line, use [1].fY - [0].fY |
| 366 | for (int index = 0; index < result; ++index) { |
| 367 | fT[1][index] = 1 - fT[1][index]; |
| 368 | } |
| 369 | } |
caryclark@google.com | 1510726 | 2013-11-08 18:00:01 +0000 | [diff] [blame] | 370 | fPt[0].fX = x; |
| 371 | fPt[0].fY = yIntercept; |
| 372 | fUsed = 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 375 | if (fAllowNear || result == 2) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 376 | if ((t = line.nearPoint(topPt, NULL)) >= 0) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 377 | insert(t, (double) flipped, topPt); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 378 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 379 | if (top != bottom) { |
| 380 | SkDPoint bottomPt = { x, bottom }; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 381 | if ((t = line.nearPoint(bottomPt, NULL)) >= 0) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 382 | insert(t, (double) !flipped, bottomPt); |
| 383 | } |
| 384 | for (int index = 0; index < 2; ++index) { |
| 385 | if ((t = SkDLine::NearPointV(line[index], top, bottom, x)) >= 0) { |
| 386 | insert((double) index, flipped ? 1 - t : t, line[index]); |
| 387 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 391 | cleanUpParallelLines(result == 2); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 392 | SkASSERT(fUsed <= 2); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 393 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py |
| 397 | // 4 subs, 2 muls, 1 cmp |
| 398 | static bool ccw(const SkDPoint& A, const SkDPoint& B, const SkDPoint& C) { |
| 399 | return (C.fY - A.fY) * (B.fX - A.fX) > (B.fY - A.fY) * (C.fX - A.fX); |
| 400 | } |
| 401 | |
| 402 | // 16 subs, 8 muls, 6 cmps |
| 403 | bool SkIntersections::Test(const SkDLine& a, const SkDLine& b) { |
| 404 | return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1]) |
| 405 | && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]); |
| 406 | } |