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