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 | |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 10 | void SkIntersections::cleanUpParallelLines(bool parallel) { |
| 11 | while (fUsed > 2) { |
| 12 | removeOne(1); |
| 13 | } |
| 14 | if (fUsed == 2 && !parallel) { |
caryclark | 94c902e | 2015-08-18 07:12:43 -0700 | [diff] [blame] | 15 | bool startMatch = fT[0][0] == 0 || zero_or_one(fT[1][0]); |
| 16 | bool endMatch = fT[0][1] == 1 || zero_or_one(fT[1][1]); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 17 | if ((!startMatch && !endMatch) || approximately_equal(fT[0][0], fT[0][1])) { |
| 18 | SkASSERT(startMatch || endMatch); |
caryclark | 94c902e | 2015-08-18 07:12:43 -0700 | [diff] [blame] | 19 | if (startMatch && endMatch && (fT[0][0] != 0 || !zero_or_one(fT[1][0])) |
| 20 | && fT[0][1] == 1 && zero_or_one(fT[1][1])) { |
| 21 | removeOne(0); |
| 22 | } else { |
| 23 | removeOne(endMatch); |
| 24 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 25 | } |
| 26 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 27 | if (fUsed == 2) { |
| 28 | fIsCoincident[0] = fIsCoincident[1] = 0x03; |
| 29 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void SkIntersections::computePoints(const SkDLine& line, int used) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 33 | fPt[0] = line.ptAtT(fT[0][0]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 34 | if ((fUsed = used) == 2) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 35 | fPt[1] = line.ptAtT(fT[0][1]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 36 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 37 | } |
| 38 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 39 | int SkIntersections::intersectRay(const SkDLine& a, const SkDLine& b) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 40 | fMax = 2; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 41 | SkDVector aLen = a[1] - a[0]; |
| 42 | SkDVector bLen = b[1] - b[0]; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 43 | /* Slopes match when denom goes to zero: |
| 44 | axLen / ayLen == bxLen / byLen |
| 45 | (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen |
| 46 | byLen * axLen == ayLen * bxLen |
| 47 | byLen * axLen - ayLen * bxLen == 0 ( == denom ) |
| 48 | */ |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 49 | double denom = bLen.fY * aLen.fX - aLen.fY * bLen.fX; |
| 50 | SkDVector ab0 = a[0] - b[0]; |
| 51 | double numerA = ab0.fY * bLen.fX - bLen.fY * ab0.fX; |
| 52 | double numerB = ab0.fY * aLen.fX - aLen.fY * ab0.fX; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 53 | numerA /= denom; |
| 54 | numerB /= denom; |
| 55 | int used; |
| 56 | if (!approximately_zero(denom)) { |
| 57 | fT[0][0] = numerA; |
| 58 | fT[1][0] = numerB; |
| 59 | used = 1; |
| 60 | } else { |
| 61 | /* See if the axis intercepts match: |
| 62 | ay - ax * ayLen / axLen == by - bx * ayLen / axLen |
| 63 | axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen) |
| 64 | axLen * ay - ax * ayLen == axLen * by - bx * ayLen |
| 65 | */ |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 66 | if (!AlmostEqualUlps(aLen.fX * a[0].fY - aLen.fY * a[0].fX, |
| 67 | aLen.fX * b[0].fY - aLen.fY * b[0].fX)) { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 68 | return fUsed = 0; |
| 69 | } |
| 70 | // there's no great answer for intersection points for coincident rays, but return something |
| 71 | fT[0][0] = fT[1][0] = 0; |
| 72 | fT[1][0] = fT[1][1] = 1; |
| 73 | used = 2; |
| 74 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 75 | computePoints(a, used); |
| 76 | return fUsed; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 77 | } |
| 78 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 79 | // 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] | 80 | int SkIntersections::intersect(const SkDLine& a, const SkDLine& b) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 81 | 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] | 82 | // see if end points intersect the opposite line |
| 83 | double t; |
| 84 | for (int iA = 0; iA < 2; ++iA) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 85 | if ((t = b.exactPoint(a[iA])) >= 0) { |
| 86 | insert(iA, t, a[iA]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 87 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 88 | } |
| 89 | for (int iB = 0; iB < 2; ++iB) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 90 | if ((t = a.exactPoint(b[iB])) >= 0) { |
| 91 | insert(t, iB, b[iB]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 92 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 93 | } |
| 94 | /* Determine the intersection point of two line segments |
| 95 | Return FALSE if the lines don't intersect |
| 96 | from: http://paulbourke.net/geometry/lineline2d/ */ |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 97 | double axLen = a[1].fX - a[0].fX; |
| 98 | double ayLen = a[1].fY - a[0].fY; |
| 99 | double bxLen = b[1].fX - b[0].fX; |
| 100 | double byLen = b[1].fY - b[0].fY; |
| 101 | /* Slopes match when denom goes to zero: |
| 102 | axLen / ayLen == bxLen / byLen |
| 103 | (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen |
| 104 | byLen * axLen == ayLen * bxLen |
| 105 | byLen * axLen - ayLen * bxLen == 0 ( == denom ) |
| 106 | */ |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 107 | double axByLen = axLen * byLen; |
| 108 | double ayBxLen = ayLen * bxLen; |
| 109 | // detect parallel lines the same way here and in SkOpAngle operator < |
| 110 | // so that non-parallel means they are also sortable |
caryclark | b669300 | 2015-12-16 12:28:35 -0800 | [diff] [blame] | 111 | bool unparallel = fAllowNear ? NotAlmostEqualUlps_Pin(axByLen, ayBxLen) |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 112 | : NotAlmostDequalUlps(axByLen, ayBxLen); |
| 113 | if (unparallel && fUsed == 0) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 114 | double ab0y = a[0].fY - b[0].fY; |
| 115 | double ab0x = a[0].fX - b[0].fX; |
| 116 | double numerA = ab0y * bxLen - byLen * ab0x; |
| 117 | double numerB = ab0y * axLen - ayLen * ab0x; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 118 | double denom = axByLen - ayBxLen; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 119 | if (between(0, numerA, denom) && between(0, numerB, denom)) { |
| 120 | fT[0][0] = numerA / denom; |
| 121 | fT[1][0] = numerB / denom; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 122 | computePoints(a, 1); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 123 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 124 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 125 | /* Allow tracking that both sets of end points are near each other -- the lines are entirely |
| 126 | coincident -- even when the end points are not exactly the same. |
| 127 | Mark this as a 'wild card' for the end points, so that either point is considered totally |
| 128 | coincident. Then, avoid folding the lines over each other, but allow either end to mate |
| 129 | to the next set of lines. |
| 130 | */ |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 131 | if (fAllowNear || !unparallel) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 132 | double aNearB[2]; |
| 133 | double bNearA[2]; |
| 134 | bool aNotB[2] = {false, false}; |
| 135 | bool bNotA[2] = {false, false}; |
| 136 | int nearCount = 0; |
| 137 | for (int index = 0; index < 2; ++index) { |
| 138 | aNearB[index] = t = b.nearPoint(a[index], &aNotB[index]); |
| 139 | nearCount += t >= 0; |
| 140 | bNearA[index] = t = a.nearPoint(b[index], &bNotA[index]); |
| 141 | nearCount += t >= 0; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 142 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 143 | if (nearCount > 0) { |
caryclark | 19eb3b2 | 2014-07-18 05:08:14 -0700 | [diff] [blame] | 144 | // Skip if each segment contributes to one end point. |
| 145 | if (nearCount != 2 || aNotB[0] == aNotB[1]) { |
| 146 | for (int iA = 0; iA < 2; ++iA) { |
| 147 | if (!aNotB[iA]) { |
| 148 | continue; |
| 149 | } |
| 150 | int nearer = aNearB[iA] > 0.5; |
| 151 | if (!bNotA[nearer]) { |
| 152 | continue; |
| 153 | } |
| 154 | SkASSERT(a[iA] != b[nearer]); |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 155 | SkOPASSERT(iA == (bNearA[nearer] > 0.5)); |
caryclark | 19eb3b2 | 2014-07-18 05:08:14 -0700 | [diff] [blame] | 156 | insertNear(iA, nearer, a[iA], b[nearer]); |
| 157 | aNearB[iA] = -1; |
| 158 | bNearA[nearer] = -1; |
| 159 | nearCount -= 2; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 160 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 161 | } |
| 162 | if (nearCount > 0) { |
| 163 | for (int iA = 0; iA < 2; ++iA) { |
| 164 | if (aNearB[iA] >= 0) { |
| 165 | insert(iA, aNearB[iA], a[iA]); |
| 166 | } |
| 167 | } |
| 168 | for (int iB = 0; iB < 2; ++iB) { |
| 169 | if (bNearA[iB] >= 0) { |
| 170 | insert(bNearA[iB], iB, b[iB]); |
| 171 | } |
| 172 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 176 | cleanUpParallelLines(!unparallel); |
| 177 | SkASSERT(fUsed <= 2); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 178 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 179 | } |
| 180 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 181 | static int horizontal_coincident(const SkDLine& line, double y) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 182 | double min = line[0].fY; |
| 183 | double max = line[1].fY; |
| 184 | if (min > max) { |
| 185 | SkTSwap(min, max); |
| 186 | } |
| 187 | if (min > y || max < y) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 188 | return 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 189 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 190 | 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] | 191 | return 2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 192 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 193 | return 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 194 | } |
| 195 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 196 | double SkIntersections::HorizontalIntercept(const SkDLine& line, double y) { |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 197 | 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] | 198 | } |
| 199 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 200 | int SkIntersections::horizontal(const SkDLine& line, double left, double right, |
| 201 | double y, bool flipped) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 202 | 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] | 203 | // see if end points intersect the opposite line |
| 204 | double t; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 205 | const SkDPoint leftPt = { left, y }; |
| 206 | if ((t = line.exactPoint(leftPt)) >= 0) { |
| 207 | insert(t, (double) flipped, leftPt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 208 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 209 | if (left != right) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 210 | const SkDPoint rightPt = { right, y }; |
| 211 | if ((t = line.exactPoint(rightPt)) >= 0) { |
| 212 | insert(t, (double) !flipped, rightPt); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 213 | } |
| 214 | for (int index = 0; index < 2; ++index) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 215 | if ((t = SkDLine::ExactPointH(line[index], left, right, y)) >= 0) { |
| 216 | insert((double) index, flipped ? 1 - t : t, line[index]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 217 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 220 | int result = horizontal_coincident(line, y); |
| 221 | if (result == 1 && fUsed == 0) { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 222 | fT[0][0] = HorizontalIntercept(line, y); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 223 | double xIntercept = line[0].fX + fT[0][0] * (line[1].fX - line[0].fX); |
| 224 | if (between(left, xIntercept, right)) { |
| 225 | fT[1][0] = (xIntercept - left) / (right - left); |
| 226 | if (flipped) { |
| 227 | // OPTIMIZATION: ? instead of swapping, pass original line, use [1].fX - [0].fX |
| 228 | for (int index = 0; index < result; ++index) { |
| 229 | fT[1][index] = 1 - fT[1][index]; |
| 230 | } |
| 231 | } |
caryclark@google.com | 1510726 | 2013-11-08 18:00:01 +0000 | [diff] [blame] | 232 | fPt[0].fX = xIntercept; |
| 233 | fPt[0].fY = y; |
| 234 | fUsed = 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 237 | if (fAllowNear || result == 2) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 238 | if ((t = line.nearPoint(leftPt, nullptr)) >= 0) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 239 | insert(t, (double) flipped, leftPt); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 240 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 241 | if (left != right) { |
| 242 | const SkDPoint rightPt = { right, y }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 243 | if ((t = line.nearPoint(rightPt, nullptr)) >= 0) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 244 | insert(t, (double) !flipped, rightPt); |
| 245 | } |
| 246 | for (int index = 0; index < 2; ++index) { |
| 247 | if ((t = SkDLine::NearPointH(line[index], left, right, y)) >= 0) { |
| 248 | insert((double) index, flipped ? 1 - t : t, line[index]); |
| 249 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 253 | cleanUpParallelLines(result == 2); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 254 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 255 | } |
| 256 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 257 | static int vertical_coincident(const SkDLine& line, double x) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 258 | double min = line[0].fX; |
| 259 | double max = line[1].fX; |
| 260 | if (min > max) { |
| 261 | SkTSwap(min, max); |
| 262 | } |
caryclark@google.com | 0361032 | 2013-04-18 15:58:21 +0000 | [diff] [blame] | 263 | if (!precisely_between(min, x, max)) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 264 | return 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 265 | } |
| 266 | if (AlmostEqualUlps(min, max)) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 267 | return 2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 268 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 269 | return 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 270 | } |
| 271 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 272 | double SkIntersections::VerticalIntercept(const SkDLine& line, double x) { |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 273 | 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] | 274 | } |
| 275 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 276 | int SkIntersections::vertical(const SkDLine& line, double top, double bottom, |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 277 | double x, bool flipped) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 278 | fMax = 3; // cleanup parallel lines will bring this back line |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 279 | // see if end points intersect the opposite line |
| 280 | double t; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 281 | SkDPoint topPt = { x, top }; |
| 282 | if ((t = line.exactPoint(topPt)) >= 0) { |
| 283 | insert(t, (double) flipped, topPt); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 284 | } |
| 285 | if (top != bottom) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 286 | SkDPoint bottomPt = { x, bottom }; |
| 287 | if ((t = line.exactPoint(bottomPt)) >= 0) { |
| 288 | insert(t, (double) !flipped, bottomPt); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 289 | } |
| 290 | for (int index = 0; index < 2; ++index) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 291 | if ((t = SkDLine::ExactPointV(line[index], top, bottom, x)) >= 0) { |
| 292 | insert((double) index, flipped ? 1 - t : t, line[index]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 293 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 296 | int result = vertical_coincident(line, x); |
| 297 | if (result == 1 && fUsed == 0) { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 298 | fT[0][0] = VerticalIntercept(line, x); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 299 | double yIntercept = line[0].fY + fT[0][0] * (line[1].fY - line[0].fY); |
| 300 | if (between(top, yIntercept, bottom)) { |
| 301 | fT[1][0] = (yIntercept - top) / (bottom - top); |
| 302 | if (flipped) { |
| 303 | // OPTIMIZATION: instead of swapping, pass original line, use [1].fY - [0].fY |
| 304 | for (int index = 0; index < result; ++index) { |
| 305 | fT[1][index] = 1 - fT[1][index]; |
| 306 | } |
| 307 | } |
caryclark@google.com | 1510726 | 2013-11-08 18:00:01 +0000 | [diff] [blame] | 308 | fPt[0].fX = x; |
| 309 | fPt[0].fY = yIntercept; |
| 310 | fUsed = 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 313 | if (fAllowNear || result == 2) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 314 | if ((t = line.nearPoint(topPt, nullptr)) >= 0) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 315 | insert(t, (double) flipped, topPt); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 316 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 317 | if (top != bottom) { |
| 318 | SkDPoint bottomPt = { x, bottom }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 319 | if ((t = line.nearPoint(bottomPt, nullptr)) >= 0) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 320 | insert(t, (double) !flipped, bottomPt); |
| 321 | } |
| 322 | for (int index = 0; index < 2; ++index) { |
| 323 | if ((t = SkDLine::NearPointV(line[index], top, bottom, x)) >= 0) { |
| 324 | insert((double) index, flipped ? 1 - t : t, line[index]); |
| 325 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 329 | cleanUpParallelLines(result == 2); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 330 | SkASSERT(fUsed <= 2); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 331 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 332 | } |
| 333 | |