caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +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 | */ |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | #include "CurveIntersection.h" |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 8 | #include "Intersections.h" |
| 9 | #include "LineUtilities.h" |
| 10 | #include "QuadraticUtilities.h" |
| 11 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 12 | /* |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 13 | Find the interection of a line and quadratic by solving for valid t values. |
| 14 | |
| 15 | From http://stackoverflow.com/questions/1853637/how-to-find-the-mathematical-function-defining-a-bezier-curve |
| 16 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 17 | "A Bezier curve is a parametric function. A quadratic Bezier curve (i.e. three |
| 18 | control points) can be expressed as: F(t) = A(1 - t)^2 + B(1 - t)t + Ct^2 where |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 19 | A, B and C are points and t goes from zero to one. |
| 20 | |
| 21 | This will give you two equations: |
| 22 | |
| 23 | x = a(1 - t)^2 + b(1 - t)t + ct^2 |
| 24 | y = d(1 - t)^2 + e(1 - t)t + ft^2 |
| 25 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 26 | If you add for instance the line equation (y = kx + m) to that, you'll end up |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 27 | with three equations and three unknowns (x, y and t)." |
| 28 | |
| 29 | Similar to above, the quadratic is represented as |
| 30 | x = a(1-t)^2 + 2b(1-t)t + ct^2 |
| 31 | y = d(1-t)^2 + 2e(1-t)t + ft^2 |
| 32 | and the line as |
| 33 | y = g*x + h |
| 34 | |
| 35 | Using Mathematica, solve for the values of t where the quadratic intersects the |
| 36 | line: |
| 37 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 38 | (in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x, |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 39 | d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x] |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 40 | (out) -d + h + 2 d t - 2 e t - d t^2 + 2 e t^2 - f t^2 + |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 41 | g (a - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2) |
| 42 | (in) Solve[t1 == 0, t] |
| 43 | (out) { |
| 44 | {t -> (-2 d + 2 e + 2 a g - 2 b g - |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 45 | Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 46 | 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) / |
| 47 | (2 (-d + 2 e - f + a g - 2 b g + c g)) |
| 48 | }, |
| 49 | {t -> (-2 d + 2 e + 2 a g - 2 b g + |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 50 | Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 51 | 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) / |
| 52 | (2 (-d + 2 e - f + a g - 2 b g + c g)) |
| 53 | } |
| 54 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 55 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 56 | Using the results above (when the line tends towards horizontal) |
| 57 | A = (-(d - 2*e + f) + g*(a - 2*b + c) ) |
| 58 | B = 2*( (d - e ) - g*(a - b ) ) |
| 59 | C = (-(d ) + g*(a ) + h ) |
| 60 | |
| 61 | If g goes to infinity, we can rewrite the line in terms of x. |
| 62 | x = g'*y + h' |
| 63 | |
| 64 | And solve accordingly in Mathematica: |
| 65 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 66 | (in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h', |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 67 | d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y] |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 68 | (out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 - |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 69 | g' (d - 2 d t + 2 e t + d t^2 - 2 e t^2 + f t^2) |
| 70 | (in) Solve[t2 == 0, t] |
| 71 | (out) { |
| 72 | {t -> (2 a - 2 b - 2 d g' + 2 e g' - |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 73 | Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 74 | 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')]) / |
| 75 | (2 (a - 2 b + c - d g' + 2 e g' - f g')) |
| 76 | }, |
| 77 | {t -> (2 a - 2 b - 2 d g' + 2 e g' + |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 78 | Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 79 | 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')])/ |
| 80 | (2 (a - 2 b + c - d g' + 2 e g' - f g')) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Thus, if the slope of the line tends towards vertical, we use: |
| 85 | A = ( (a - 2*b + c) - g'*(d - 2*e + f) ) |
| 86 | B = 2*(-(a - b ) + g'*(d - e ) ) |
| 87 | C = ( (a ) - g'*(d ) - h' ) |
| 88 | */ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 89 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 90 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 91 | class LineQuadraticIntersections { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 92 | public: |
| 93 | |
| 94 | LineQuadraticIntersections(const Quadratic& q, const _Line& l, Intersections& i) |
| 95 | : quad(q) |
| 96 | , line(l) |
| 97 | , intersections(i) { |
| 98 | } |
| 99 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 100 | int intersectRay(double roots[2]) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 101 | /* |
| 102 | solve by rotating line+quad so line is horizontal, then finding the roots |
| 103 | set up matrix to rotate quad to x-axis |
| 104 | |cos(a) -sin(a)| |
| 105 | |sin(a) cos(a)| |
| 106 | note that cos(a) = A(djacent) / Hypoteneuse |
| 107 | sin(a) = O(pposite) / Hypoteneuse |
| 108 | since we are computing Ts, we can ignore hypoteneuse, the scale factor: |
| 109 | | A -O | |
| 110 | | O A | |
| 111 | A = line[1].x - line[0].x (adjacent side of the right triangle) |
| 112 | O = line[1].y - line[0].y (opposite side of the right triangle) |
| 113 | for each of the three points (e.g. n = 0 to 2) |
| 114 | quad[n].y' = (quad[n].y - line[0].y) * A - (quad[n].x - line[0].x) * O |
| 115 | */ |
| 116 | double adj = line[1].x - line[0].x; |
| 117 | double opp = line[1].y - line[0].y; |
| 118 | double r[3]; |
| 119 | for (int n = 0; n < 3; ++n) { |
| 120 | r[n] = (quad[n].y - line[0].y) * adj - (quad[n].x - line[0].x) * opp; |
| 121 | } |
| 122 | double A = r[2]; |
| 123 | double B = r[1]; |
| 124 | double C = r[0]; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 125 | A += C - 2 * B; // A = a - 2*b + c |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 126 | B -= C; // B = -(b - c) |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 127 | return quadraticRoots(A, B, C, roots); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int intersect() { |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 131 | addEndPoints(); |
| 132 | double rootVals[2]; |
| 133 | int roots = intersectRay(rootVals); |
| 134 | for (int index = 0; index < roots; ++index) { |
| 135 | double quadT = rootVals[index]; |
| 136 | double lineT = findLineT(quadT); |
| 137 | if (pinTs(quadT, lineT)) { |
| 138 | intersections.insert(quadT, lineT); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 139 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 140 | } |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 141 | return intersections.fUsed; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 142 | } |
| 143 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 144 | int horizontalIntersect(double axisIntercept, double roots[2]) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 145 | double D = quad[2].y; // f |
| 146 | double E = quad[1].y; // e |
| 147 | double F = quad[0].y; // d |
| 148 | D += F - 2 * E; // D = d - 2*e + f |
| 149 | E -= F; // E = -(d - e) |
| 150 | F -= axisIntercept; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 151 | return quadraticRoots(D, E, F, roots); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 152 | } |
| 153 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 154 | int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) { |
| 155 | addHorizontalEndPoints(left, right, axisIntercept); |
| 156 | double rootVals[2]; |
| 157 | int roots = horizontalIntersect(axisIntercept, rootVals); |
| 158 | for (int index = 0; index < roots; ++index) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 159 | double x; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 160 | double quadT = rootVals[index]; |
| 161 | xy_at_t(quad, quadT, x, *(double*) NULL); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 162 | double lineT = (x - left) / (right - left); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 163 | if (pinTs(quadT, lineT)) { |
| 164 | intersections.insert(quadT, lineT); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 167 | if (flipped) { |
| 168 | flip(); |
| 169 | } |
| 170 | return intersections.fUsed; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 171 | } |
| 172 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 173 | int verticalIntersect(double axisIntercept, double roots[2]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 174 | double D = quad[2].x; // f |
| 175 | double E = quad[1].x; // e |
| 176 | double F = quad[0].x; // d |
| 177 | D += F - 2 * E; // D = d - 2*e + f |
| 178 | E -= F; // E = -(d - e) |
| 179 | F -= axisIntercept; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 180 | return quadraticRoots(D, E, F, roots); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 181 | } |
| 182 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 183 | int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) { |
| 184 | addVerticalEndPoints(top, bottom, axisIntercept); |
| 185 | double rootVals[2]; |
| 186 | int roots = verticalIntersect(axisIntercept, rootVals); |
| 187 | for (int index = 0; index < roots; ++index) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 188 | double y; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 189 | double quadT = rootVals[index]; |
| 190 | xy_at_t(quad, quadT, *(double*) NULL, y); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 191 | double lineT = (y - top) / (bottom - top); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 192 | if (pinTs(quadT, lineT)) { |
| 193 | intersections.insert(quadT, lineT); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 196 | if (flipped) { |
| 197 | flip(); |
| 198 | } |
| 199 | return intersections.fUsed; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 200 | } |
| 201 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 202 | protected: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 203 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 204 | // add endpoints first to get zero and one t values exactly |
| 205 | void addEndPoints() |
| 206 | { |
| 207 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
| 208 | for (int lIndex = 0; lIndex < 2; lIndex++) { |
| 209 | if (quad[qIndex] == line[lIndex]) { |
| 210 | intersections.insert(qIndex >> 1, lIndex); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void addHorizontalEndPoints(double left, double right, double y) |
| 217 | { |
| 218 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
| 219 | if (quad[qIndex].y != y) { |
| 220 | continue; |
| 221 | } |
| 222 | if (quad[qIndex].x == left) { |
| 223 | intersections.insert(qIndex >> 1, 0); |
| 224 | } |
| 225 | if (quad[qIndex].x == right) { |
| 226 | intersections.insert(qIndex >> 1, 1); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void addVerticalEndPoints(double top, double bottom, double x) |
| 232 | { |
| 233 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
| 234 | if (quad[qIndex].x != x) { |
| 235 | continue; |
| 236 | } |
| 237 | if (quad[qIndex].y == top) { |
| 238 | intersections.insert(qIndex >> 1, 0); |
| 239 | } |
| 240 | if (quad[qIndex].y == bottom) { |
| 241 | intersections.insert(qIndex >> 1, 1); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 246 | double findLineT(double t) { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 247 | double x, y; |
| 248 | xy_at_t(quad, t, x, y); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 249 | double dx = line[1].x - line[0].x; |
| 250 | double dy = line[1].y - line[0].y; |
| 251 | if (fabs(dx) > fabs(dy)) { |
| 252 | return (x - line[0].x) / dx; |
| 253 | } |
| 254 | return (y - line[0].y) / dy; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 255 | } |
| 256 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 257 | void flip() { |
| 258 | // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y |
| 259 | int roots = intersections.fUsed; |
| 260 | for (int index = 0; index < roots; ++index) { |
| 261 | intersections.fT[1][index] = 1 - intersections.fT[1][index]; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | bool pinTs(double& quadT, double& lineT) { |
| 266 | if (!approximately_one_or_less(lineT)) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 267 | return false; |
| 268 | } |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 269 | if (!approximately_zero_or_more(lineT)) { |
| 270 | return false; |
| 271 | } |
| 272 | if (quadT < 0) { |
| 273 | quadT = 0; |
| 274 | } else if (quadT > 1) { |
| 275 | quadT = 1; |
| 276 | } |
| 277 | if (lineT < 0) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 278 | lineT = 0; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 279 | } else if (lineT > 1) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 280 | lineT = 1; |
| 281 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 282 | return true; |
| 283 | } |
| 284 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 285 | private: |
| 286 | |
| 287 | const Quadratic& quad; |
| 288 | const _Line& line; |
| 289 | Intersections& intersections; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 290 | }; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 291 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 292 | // utility for pairs of coincident quads |
| 293 | static double horizontalIntersect(const Quadratic& quad, const _Point& pt) { |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 294 | LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0)); |
| 295 | double rootVals[2]; |
| 296 | int roots = q.horizontalIntersect(pt.y, rootVals); |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 297 | for (int index = 0; index < roots; ++index) { |
| 298 | double x; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 299 | double t = rootVals[index]; |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 300 | xy_at_t(quad, t, x, *(double*) 0); |
| 301 | if (approximately_equal(x, pt.x)) { |
| 302 | return t; |
| 303 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 304 | } |
| 305 | return -1; |
| 306 | } |
| 307 | |
| 308 | static double verticalIntersect(const Quadratic& quad, const _Point& pt) { |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 309 | LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0)); |
| 310 | double rootVals[2]; |
| 311 | int roots = q.verticalIntersect(pt.x, rootVals); |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 312 | for (int index = 0; index < roots; ++index) { |
| 313 | double y; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 314 | double t = rootVals[index]; |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 315 | xy_at_t(quad, t, *(double*) 0, y); |
| 316 | if (approximately_equal(y, pt.y)) { |
| 317 | return t; |
| 318 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 319 | } |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | double axialIntersect(const Quadratic& q1, const _Point& p, bool vertical) { |
| 324 | if (vertical) { |
| 325 | return verticalIntersect(q1, p); |
| 326 | } |
| 327 | return horizontalIntersect(q1, p); |
| 328 | } |
| 329 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 330 | int horizontalIntersect(const Quadratic& quad, double left, double right, |
| 331 | double y, double tRange[2]) { |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 332 | LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0)); |
| 333 | double rootVals[2]; |
| 334 | int result = q.horizontalIntersect(y, rootVals); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 335 | int tCount = 0; |
| 336 | for (int index = 0; index < result; ++index) { |
| 337 | double x, y; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 338 | xy_at_t(quad, rootVals[index], x, y); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 339 | if (x < left || x > right) { |
| 340 | continue; |
| 341 | } |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 342 | tRange[tCount++] = rootVals[index]; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 343 | } |
| 344 | return tCount; |
| 345 | } |
| 346 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 347 | int horizontalIntersect(const Quadratic& quad, double left, double right, double y, |
| 348 | bool flipped, Intersections& intersections) { |
| 349 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 350 | return q.horizontalIntersect(y, left, right, flipped); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | int verticalIntersect(const Quadratic& quad, double top, double bottom, double x, |
| 354 | bool flipped, Intersections& intersections) { |
| 355 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 356 | return q.verticalIntersect(x, top, bottom, flipped); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 357 | } |
| 358 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 359 | int intersect(const Quadratic& quad, const _Line& line, Intersections& i) { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 360 | LineQuadraticIntersections q(quad, line, i); |
| 361 | return q.intersect(); |
| 362 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 363 | |
| 364 | int intersectRay(const Quadratic& quad, const _Line& line, Intersections& i) { |
| 365 | LineQuadraticIntersections q(quad, line, i); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame^] | 366 | return q.intersectRay(i.fT[0]); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 367 | } |