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@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 29 | void SkIntersections::cleanUpCoincidence() { |
| 30 | SkASSERT(fUsed == 2); |
| 31 | // both t values are good |
| 32 | bool startMatch = fT[0][0] == 0 && (fT[1][0] == 0 || fT[1][0] == 1); |
| 33 | bool endMatch = fT[0][1] == 1 && (fT[1][1] == 0 || fT[1][1] == 1); |
| 34 | if (startMatch || endMatch) { |
| 35 | removeOne(startMatch); |
| 36 | return; |
| 37 | } |
| 38 | // either t value is good |
| 39 | bool pStartMatch = fT[0][0] == 0 || fT[1][0] == 0 || fT[1][0] == 1; |
| 40 | bool pEndMatch = fT[0][1] == 1 || fT[1][1] == 0 || fT[1][1] == 1; |
| 41 | removeOne(pStartMatch || !pEndMatch); |
| 42 | } |
| 43 | |
| 44 | void SkIntersections::cleanUpParallelLines(bool parallel) { |
| 45 | while (fUsed > 2) { |
| 46 | removeOne(1); |
| 47 | } |
| 48 | if (fUsed == 2 && !parallel) { |
| 49 | bool startMatch = fT[0][0] == 0 || fT[1][0] == 0 || fT[1][0] == 1; |
| 50 | bool endMatch = fT[0][1] == 1 || fT[1][1] == 0 || fT[1][1] == 1; |
| 51 | if ((!startMatch && !endMatch) || approximately_equal(fT[0][0], fT[0][1])) { |
| 52 | SkASSERT(startMatch || endMatch); |
| 53 | removeOne(endMatch); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void SkIntersections::computePoints(const SkDLine& line, int used) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 59 | fPt[0] = line.ptAtT(fT[0][0]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 60 | if ((fUsed = used) == 2) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 61 | fPt[1] = line.ptAtT(fT[0][1]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 62 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 63 | } |
| 64 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 65 | int SkIntersections::intersectRay(const SkDLine& a, const SkDLine& b) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 66 | fMax = 2; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 67 | SkDVector aLen = a[1] - a[0]; |
| 68 | SkDVector bLen = b[1] - b[0]; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 69 | /* Slopes match when denom goes to zero: |
| 70 | axLen / ayLen == bxLen / byLen |
| 71 | (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen |
| 72 | byLen * axLen == ayLen * bxLen |
| 73 | byLen * axLen - ayLen * bxLen == 0 ( == denom ) |
| 74 | */ |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 75 | double denom = bLen.fY * aLen.fX - aLen.fY * bLen.fX; |
| 76 | SkDVector ab0 = a[0] - b[0]; |
| 77 | double numerA = ab0.fY * bLen.fX - bLen.fY * ab0.fX; |
| 78 | double numerB = ab0.fY * aLen.fX - aLen.fY * ab0.fX; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 79 | #if 0 |
| 80 | if (!between(0, numerA, denom) || !between(0, numerB, denom)) { |
| 81 | fUsed = 0; |
| 82 | return 0; |
| 83 | } |
| 84 | #endif |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 85 | numerA /= denom; |
| 86 | numerB /= denom; |
| 87 | int used; |
| 88 | if (!approximately_zero(denom)) { |
| 89 | fT[0][0] = numerA; |
| 90 | fT[1][0] = numerB; |
| 91 | used = 1; |
| 92 | } else { |
| 93 | /* See if the axis intercepts match: |
| 94 | ay - ax * ayLen / axLen == by - bx * ayLen / axLen |
| 95 | axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen) |
| 96 | axLen * ay - ax * ayLen == axLen * by - bx * ayLen |
| 97 | */ |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 98 | if (!AlmostEqualUlps(aLen.fX * a[0].fY - aLen.fY * a[0].fX, |
| 99 | aLen.fX * b[0].fY - aLen.fY * b[0].fX)) { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 100 | return fUsed = 0; |
| 101 | } |
| 102 | // there's no great answer for intersection points for coincident rays, but return something |
| 103 | fT[0][0] = fT[1][0] = 0; |
| 104 | fT[1][0] = fT[1][1] = 1; |
| 105 | used = 2; |
| 106 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 107 | computePoints(a, used); |
| 108 | return fUsed; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 109 | } |
| 110 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 111 | // 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] | 112 | int SkIntersections::intersect(const SkDLine& a, const SkDLine& b) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 113 | 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] | 114 | // see if end points intersect the opposite line |
| 115 | double t; |
| 116 | for (int iA = 0; iA < 2; ++iA) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 117 | if ((t = b.exactPoint(a[iA])) >= 0) { |
| 118 | insert(iA, t, a[iA]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 119 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 120 | } |
| 121 | for (int iB = 0; iB < 2; ++iB) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 122 | if ((t = a.exactPoint(b[iB])) >= 0) { |
| 123 | insert(t, iB, b[iB]); |
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 | /* Determine the intersection point of two line segments |
| 127 | Return FALSE if the lines don't intersect |
| 128 | from: http://paulbourke.net/geometry/lineline2d/ */ |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 129 | double axLen = a[1].fX - a[0].fX; |
| 130 | double ayLen = a[1].fY - a[0].fY; |
| 131 | double bxLen = b[1].fX - b[0].fX; |
| 132 | double byLen = b[1].fY - b[0].fY; |
| 133 | /* Slopes match when denom goes to zero: |
| 134 | axLen / ayLen == bxLen / byLen |
| 135 | (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen |
| 136 | byLen * axLen == ayLen * bxLen |
| 137 | byLen * axLen - ayLen * bxLen == 0 ( == denom ) |
| 138 | */ |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 139 | double axByLen = axLen * byLen; |
| 140 | double ayBxLen = ayLen * bxLen; |
| 141 | // detect parallel lines the same way here and in SkOpAngle operator < |
| 142 | // so that non-parallel means they are also sortable |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 143 | bool unparallel = fAllowNear ? NotAlmostEqualUlps(axByLen, ayBxLen) |
| 144 | : NotAlmostDequalUlps(axByLen, ayBxLen); |
| 145 | if (unparallel && fUsed == 0) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 146 | double ab0y = a[0].fY - b[0].fY; |
| 147 | double ab0x = a[0].fX - b[0].fX; |
| 148 | double numerA = ab0y * bxLen - byLen * ab0x; |
| 149 | double numerB = ab0y * axLen - ayLen * ab0x; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 150 | double denom = axByLen - ayBxLen; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 151 | if (between(0, numerA, denom) && between(0, numerB, denom)) { |
| 152 | fT[0][0] = numerA / denom; |
| 153 | fT[1][0] = numerB / denom; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 154 | computePoints(a, 1); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 155 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 156 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 157 | if (fAllowNear || !unparallel) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 158 | for (int iA = 0; iA < 2; ++iA) { |
| 159 | if ((t = b.nearPoint(a[iA])) >= 0) { |
| 160 | insert(iA, t, a[iA]); |
| 161 | } |
| 162 | } |
| 163 | for (int iB = 0; iB < 2; ++iB) { |
| 164 | if ((t = a.nearPoint(b[iB])) >= 0) { |
| 165 | insert(t, iB, b[iB]); |
| 166 | } |
| 167 | } |
| 168 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 169 | cleanUpParallelLines(!unparallel); |
| 170 | SkASSERT(fUsed <= 2); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 171 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 172 | } |
| 173 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 174 | static int horizontal_coincident(const SkDLine& line, double y) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 175 | double min = line[0].fY; |
| 176 | double max = line[1].fY; |
| 177 | if (min > max) { |
| 178 | SkTSwap(min, max); |
| 179 | } |
| 180 | if (min > y || max < y) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 181 | return 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 182 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 183 | 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] | 184 | return 2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 185 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 186 | return 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 187 | } |
| 188 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 189 | static double horizontal_intercept(const SkDLine& line, double y) { |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 190 | 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] | 191 | } |
| 192 | |
| 193 | int SkIntersections::horizontal(const SkDLine& line, double y) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 194 | fMax = 2; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 195 | int horizontalType = horizontal_coincident(line, y); |
| 196 | if (horizontalType == 1) { |
| 197 | fT[0][0] = horizontal_intercept(line, y); |
| 198 | } else if (horizontalType == 2) { |
| 199 | fT[0][0] = 0; |
| 200 | fT[0][1] = 1; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 201 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 202 | return fUsed = horizontalType; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 203 | } |
| 204 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 205 | int SkIntersections::horizontal(const SkDLine& line, double left, double right, |
| 206 | double y, bool flipped) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 207 | 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] | 208 | // see if end points intersect the opposite line |
| 209 | double t; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 210 | const SkDPoint leftPt = { left, y }; |
| 211 | if ((t = line.exactPoint(leftPt)) >= 0) { |
| 212 | insert(t, (double) flipped, leftPt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 213 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 214 | if (left != right) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 215 | const SkDPoint rightPt = { right, y }; |
| 216 | if ((t = line.exactPoint(rightPt)) >= 0) { |
| 217 | insert(t, (double) !flipped, rightPt); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 218 | } |
| 219 | for (int index = 0; index < 2; ++index) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 220 | if ((t = SkDLine::ExactPointH(line[index], left, right, y)) >= 0) { |
| 221 | insert((double) index, flipped ? 1 - t : t, line[index]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 222 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 225 | int result = horizontal_coincident(line, y); |
| 226 | if (result == 1 && fUsed == 0) { |
| 227 | fT[0][0] = horizontal_intercept(line, y); |
| 228 | double xIntercept = line[0].fX + fT[0][0] * (line[1].fX - line[0].fX); |
| 229 | if (between(left, xIntercept, right)) { |
| 230 | fT[1][0] = (xIntercept - left) / (right - left); |
| 231 | if (flipped) { |
| 232 | // OPTIMIZATION: ? instead of swapping, pass original line, use [1].fX - [0].fX |
| 233 | for (int index = 0; index < result; ++index) { |
| 234 | fT[1][index] = 1 - fT[1][index]; |
| 235 | } |
| 236 | } |
caryclark@google.com | 1510726 | 2013-11-08 18:00:01 +0000 | [diff] [blame] | 237 | fPt[0].fX = xIntercept; |
| 238 | fPt[0].fY = y; |
| 239 | fUsed = 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 242 | if (fAllowNear || result == 2) { |
| 243 | if ((t = line.nearPoint(leftPt)) >= 0) { |
| 244 | insert(t, (double) flipped, leftPt); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 245 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 246 | if (left != right) { |
| 247 | const SkDPoint rightPt = { right, y }; |
| 248 | if ((t = line.nearPoint(rightPt)) >= 0) { |
| 249 | insert(t, (double) !flipped, rightPt); |
| 250 | } |
| 251 | for (int index = 0; index < 2; ++index) { |
| 252 | if ((t = SkDLine::NearPointH(line[index], left, right, y)) >= 0) { |
| 253 | insert((double) index, flipped ? 1 - t : t, line[index]); |
| 254 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 258 | cleanUpParallelLines(result == 2); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 259 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 260 | } |
| 261 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 262 | static int vertical_coincident(const SkDLine& line, double x) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 263 | double min = line[0].fX; |
| 264 | double max = line[1].fX; |
| 265 | if (min > max) { |
| 266 | SkTSwap(min, max); |
| 267 | } |
caryclark@google.com | 0361032 | 2013-04-18 15:58:21 +0000 | [diff] [blame] | 268 | if (!precisely_between(min, x, max)) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 269 | return 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 270 | } |
| 271 | if (AlmostEqualUlps(min, max)) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 272 | return 2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 273 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 274 | return 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 275 | } |
| 276 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 277 | static double vertical_intercept(const SkDLine& line, double x) { |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 278 | 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] | 279 | } |
| 280 | |
| 281 | int SkIntersections::vertical(const SkDLine& line, double x) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 282 | fMax = 2; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 283 | int verticalType = vertical_coincident(line, x); |
| 284 | if (verticalType == 1) { |
| 285 | fT[0][0] = vertical_intercept(line, x); |
| 286 | } else if (verticalType == 2) { |
| 287 | fT[0][0] = 0; |
| 288 | fT[0][1] = 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 289 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 290 | return fUsed = verticalType; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | int SkIntersections::vertical(const SkDLine& line, double top, double bottom, |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 294 | double x, bool flipped) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 295 | fMax = 2; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 296 | // see if end points intersect the opposite line |
| 297 | double t; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 298 | SkDPoint topPt = { x, top }; |
| 299 | if ((t = line.exactPoint(topPt)) >= 0) { |
| 300 | insert(t, (double) flipped, topPt); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 301 | } |
| 302 | if (top != bottom) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 303 | SkDPoint bottomPt = { x, bottom }; |
| 304 | if ((t = line.exactPoint(bottomPt)) >= 0) { |
| 305 | insert(t, (double) !flipped, bottomPt); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 306 | } |
| 307 | for (int index = 0; index < 2; ++index) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 308 | if ((t = SkDLine::ExactPointV(line[index], top, bottom, x)) >= 0) { |
| 309 | insert((double) index, flipped ? 1 - t : t, line[index]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 310 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 313 | int result = vertical_coincident(line, x); |
| 314 | if (result == 1 && fUsed == 0) { |
| 315 | fT[0][0] = vertical_intercept(line, x); |
| 316 | double yIntercept = line[0].fY + fT[0][0] * (line[1].fY - line[0].fY); |
| 317 | if (between(top, yIntercept, bottom)) { |
| 318 | fT[1][0] = (yIntercept - top) / (bottom - top); |
| 319 | if (flipped) { |
| 320 | // OPTIMIZATION: instead of swapping, pass original line, use [1].fY - [0].fY |
| 321 | for (int index = 0; index < result; ++index) { |
| 322 | fT[1][index] = 1 - fT[1][index]; |
| 323 | } |
| 324 | } |
caryclark@google.com | 1510726 | 2013-11-08 18:00:01 +0000 | [diff] [blame] | 325 | fPt[0].fX = x; |
| 326 | fPt[0].fY = yIntercept; |
| 327 | fUsed = 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 330 | if (fAllowNear || result == 2) { |
| 331 | if ((t = line.nearPoint(topPt)) >= 0) { |
| 332 | insert(t, (double) flipped, topPt); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 333 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 334 | if (top != bottom) { |
| 335 | SkDPoint bottomPt = { x, bottom }; |
| 336 | if ((t = line.nearPoint(bottomPt)) >= 0) { |
| 337 | insert(t, (double) !flipped, bottomPt); |
| 338 | } |
| 339 | for (int index = 0; index < 2; ++index) { |
| 340 | if ((t = SkDLine::NearPointV(line[index], top, bottom, x)) >= 0) { |
| 341 | insert((double) index, flipped ? 1 - t : t, line[index]); |
| 342 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 346 | cleanUpParallelLines(result == 2); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 347 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py |
| 351 | // 4 subs, 2 muls, 1 cmp |
| 352 | static bool ccw(const SkDPoint& A, const SkDPoint& B, const SkDPoint& C) { |
| 353 | return (C.fY - A.fY) * (B.fX - A.fX) > (B.fY - A.fY) * (C.fX - A.fX); |
| 354 | } |
| 355 | |
| 356 | // 16 subs, 8 muls, 6 cmps |
| 357 | bool SkIntersections::Test(const SkDLine& a, const SkDLine& b) { |
| 358 | return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1]) |
| 359 | && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]); |
| 360 | } |