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 | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 127 | return quadraticRootsValidT(A, 2 * 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)) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 138 | _Point pt; |
| 139 | xy_at_t(line, lineT, pt.x, pt.y); |
| 140 | intersections.insert(quadT, lineT, pt); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 141 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 142 | } |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 143 | return intersections.fUsed; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 144 | } |
| 145 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 146 | int horizontalIntersect(double axisIntercept, double roots[2]) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 147 | double D = quad[2].y; // f |
| 148 | double E = quad[1].y; // e |
| 149 | double F = quad[0].y; // d |
| 150 | D += F - 2 * E; // D = d - 2*e + f |
| 151 | E -= F; // E = -(d - e) |
| 152 | F -= axisIntercept; |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 153 | return quadraticRootsValidT(D, 2 * E, F, roots); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 154 | } |
| 155 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 156 | int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) { |
| 157 | addHorizontalEndPoints(left, right, axisIntercept); |
| 158 | double rootVals[2]; |
| 159 | int roots = horizontalIntersect(axisIntercept, rootVals); |
| 160 | for (int index = 0; index < roots; ++index) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 161 | _Point pt; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 162 | double quadT = rootVals[index]; |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 163 | xy_at_t(quad, quadT, pt.x, pt.y); |
| 164 | double lineT = (pt.x - left) / (right - left); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 165 | if (pinTs(quadT, lineT)) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 166 | intersections.insert(quadT, lineT, pt); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 169 | if (flipped) { |
| 170 | flip(); |
| 171 | } |
| 172 | return intersections.fUsed; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 173 | } |
| 174 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 175 | int verticalIntersect(double axisIntercept, double roots[2]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 176 | double D = quad[2].x; // f |
| 177 | double E = quad[1].x; // e |
| 178 | double F = quad[0].x; // d |
| 179 | D += F - 2 * E; // D = d - 2*e + f |
| 180 | E -= F; // E = -(d - e) |
| 181 | F -= axisIntercept; |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 182 | return quadraticRootsValidT(D, 2 * E, F, roots); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 183 | } |
| 184 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 185 | int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) { |
| 186 | addVerticalEndPoints(top, bottom, axisIntercept); |
| 187 | double rootVals[2]; |
| 188 | int roots = verticalIntersect(axisIntercept, rootVals); |
| 189 | for (int index = 0; index < roots; ++index) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 190 | _Point pt; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 191 | double quadT = rootVals[index]; |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 192 | xy_at_t(quad, quadT, pt.x, pt.y); |
| 193 | double lineT = (pt.y - top) / (bottom - top); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 194 | if (pinTs(quadT, lineT)) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 195 | intersections.insert(quadT, lineT, pt); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 198 | if (flipped) { |
| 199 | flip(); |
| 200 | } |
| 201 | return intersections.fUsed; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 202 | } |
| 203 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 204 | protected: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 205 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 206 | // add endpoints first to get zero and one t values exactly |
| 207 | void addEndPoints() |
| 208 | { |
| 209 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
| 210 | for (int lIndex = 0; lIndex < 2; lIndex++) { |
| 211 | if (quad[qIndex] == line[lIndex]) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 212 | intersections.insert(qIndex >> 1, lIndex, line[lIndex]); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void addHorizontalEndPoints(double left, double right, double y) |
| 219 | { |
| 220 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
| 221 | if (quad[qIndex].y != y) { |
| 222 | continue; |
| 223 | } |
| 224 | if (quad[qIndex].x == left) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 225 | intersections.insert(qIndex >> 1, 0, quad[qIndex]); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 226 | } |
| 227 | if (quad[qIndex].x == right) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 228 | intersections.insert(qIndex >> 1, 1, quad[qIndex]); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void addVerticalEndPoints(double top, double bottom, double x) |
| 234 | { |
| 235 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
| 236 | if (quad[qIndex].x != x) { |
| 237 | continue; |
| 238 | } |
| 239 | if (quad[qIndex].y == top) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 240 | intersections.insert(qIndex >> 1, 0, quad[qIndex]); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 241 | } |
| 242 | if (quad[qIndex].y == bottom) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame^] | 243 | intersections.insert(qIndex >> 1, 1, quad[qIndex]); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 248 | double findLineT(double t) { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 249 | double x, y; |
| 250 | xy_at_t(quad, t, x, y); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 251 | double dx = line[1].x - line[0].x; |
| 252 | double dy = line[1].y - line[0].y; |
| 253 | if (fabs(dx) > fabs(dy)) { |
| 254 | return (x - line[0].x) / dx; |
| 255 | } |
| 256 | return (y - line[0].y) / dy; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 257 | } |
| 258 | |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 259 | void flip() { |
| 260 | // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y |
| 261 | int roots = intersections.fUsed; |
| 262 | for (int index = 0; index < roots; ++index) { |
| 263 | intersections.fT[1][index] = 1 - intersections.fT[1][index]; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | bool pinTs(double& quadT, double& lineT) { |
| 268 | if (!approximately_one_or_less(lineT)) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 269 | return false; |
| 270 | } |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 271 | if (!approximately_zero_or_more(lineT)) { |
| 272 | return false; |
| 273 | } |
| 274 | if (quadT < 0) { |
| 275 | quadT = 0; |
| 276 | } else if (quadT > 1) { |
| 277 | quadT = 1; |
| 278 | } |
| 279 | if (lineT < 0) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 280 | lineT = 0; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 281 | } else if (lineT > 1) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 282 | lineT = 1; |
| 283 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 284 | return true; |
| 285 | } |
| 286 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 287 | private: |
| 288 | |
| 289 | const Quadratic& quad; |
| 290 | const _Line& line; |
| 291 | Intersections& intersections; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 292 | }; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 293 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 294 | // utility for pairs of coincident quads |
| 295 | static double horizontalIntersect(const Quadratic& quad, const _Point& pt) { |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 296 | LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0)); |
| 297 | double rootVals[2]; |
| 298 | int roots = q.horizontalIntersect(pt.y, rootVals); |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 299 | for (int index = 0; index < roots; ++index) { |
| 300 | double x; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 301 | double t = rootVals[index]; |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 302 | xy_at_t(quad, t, x, *(double*) 0); |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 303 | if (AlmostEqualUlps(x, pt.x)) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 304 | return t; |
| 305 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 306 | } |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | static double verticalIntersect(const Quadratic& quad, const _Point& pt) { |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 311 | LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0)); |
| 312 | double rootVals[2]; |
| 313 | int roots = q.verticalIntersect(pt.x, rootVals); |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 314 | for (int index = 0; index < roots; ++index) { |
| 315 | double y; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 316 | double t = rootVals[index]; |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 317 | xy_at_t(quad, t, *(double*) 0, y); |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 318 | if (AlmostEqualUlps(y, pt.y)) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 319 | return t; |
| 320 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 321 | } |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | double axialIntersect(const Quadratic& q1, const _Point& p, bool vertical) { |
| 326 | if (vertical) { |
| 327 | return verticalIntersect(q1, p); |
| 328 | } |
| 329 | return horizontalIntersect(q1, p); |
| 330 | } |
| 331 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 332 | int horizontalIntersect(const Quadratic& quad, double left, double right, |
| 333 | double y, double tRange[2]) { |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 334 | LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0)); |
| 335 | double rootVals[2]; |
| 336 | int result = q.horizontalIntersect(y, rootVals); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 337 | int tCount = 0; |
| 338 | for (int index = 0; index < result; ++index) { |
| 339 | double x, y; |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 340 | xy_at_t(quad, rootVals[index], x, y); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 341 | if (x < left || x > right) { |
| 342 | continue; |
| 343 | } |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 344 | tRange[tCount++] = rootVals[index]; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 345 | } |
| 346 | return tCount; |
| 347 | } |
| 348 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 349 | int horizontalIntersect(const Quadratic& quad, double left, double right, double y, |
| 350 | bool flipped, Intersections& intersections) { |
| 351 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 352 | return q.horizontalIntersect(y, left, right, flipped); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | int verticalIntersect(const Quadratic& quad, double top, double bottom, double x, |
| 356 | bool flipped, Intersections& intersections) { |
| 357 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 358 | return q.verticalIntersect(x, top, bottom, flipped); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 359 | } |
| 360 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 361 | int intersect(const Quadratic& quad, const _Line& line, Intersections& i) { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 362 | LineQuadraticIntersections q(quad, line, i); |
| 363 | return q.intersect(); |
| 364 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 365 | |
| 366 | int intersectRay(const Quadratic& quad, const _Line& line, Intersections& i) { |
| 367 | LineQuadraticIntersections q(quad, line, i); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 368 | return q.intersectRay(i.fT[0]); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 369 | } |