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