caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1 | #include "CurveIntersection.h" |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 2 | #include "Intersections.h" |
| 3 | #include "LineUtilities.h" |
| 4 | #include "QuadraticUtilities.h" |
| 5 | |
| 6 | /* |
| 7 | Find the interection of a line and quadratic by solving for valid t values. |
| 8 | |
| 9 | From http://stackoverflow.com/questions/1853637/how-to-find-the-mathematical-function-defining-a-bezier-curve |
| 10 | |
| 11 | "A Bezier curve is a parametric function. A quadratic Bezier curve (i.e. three |
| 12 | control points) can be expressed as: F(t) = A(1 - t)^2 + B(1 - t)t + Ct^2 where |
| 13 | A, B and C are points and t goes from zero to one. |
| 14 | |
| 15 | This will give you two equations: |
| 16 | |
| 17 | x = a(1 - t)^2 + b(1 - t)t + ct^2 |
| 18 | y = d(1 - t)^2 + e(1 - t)t + ft^2 |
| 19 | |
| 20 | If you add for instance the line equation (y = kx + m) to that, you'll end up |
| 21 | with three equations and three unknowns (x, y and t)." |
| 22 | |
| 23 | Similar to above, the quadratic is represented as |
| 24 | x = a(1-t)^2 + 2b(1-t)t + ct^2 |
| 25 | y = d(1-t)^2 + 2e(1-t)t + ft^2 |
| 26 | and the line as |
| 27 | y = g*x + h |
| 28 | |
| 29 | Using Mathematica, solve for the values of t where the quadratic intersects the |
| 30 | line: |
| 31 | |
| 32 | (in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x, |
| 33 | d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x] |
| 34 | (out) -d + h + 2 d t - 2 e t - d t^2 + 2 e t^2 - f t^2 + |
| 35 | g (a - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2) |
| 36 | (in) Solve[t1 == 0, t] |
| 37 | (out) { |
| 38 | {t -> (-2 d + 2 e + 2 a g - 2 b g - |
| 39 | Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - |
| 40 | 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) / |
| 41 | (2 (-d + 2 e - f + a g - 2 b g + c g)) |
| 42 | }, |
| 43 | {t -> (-2 d + 2 e + 2 a g - 2 b g + |
| 44 | Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - |
| 45 | 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) / |
| 46 | (2 (-d + 2 e - f + a g - 2 b g + c g)) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | Numeric Solutions (5.6) suggests to solve the quadratic by computing |
| 51 | |
| 52 | Q = -1/2(B + sgn(B)Sqrt(B^2 - 4 A C)) |
| 53 | |
| 54 | and using the roots |
| 55 | |
| 56 | t1 = Q / A |
| 57 | t2 = C / Q |
| 58 | |
| 59 | Using the results above (when the line tends towards horizontal) |
| 60 | A = (-(d - 2*e + f) + g*(a - 2*b + c) ) |
| 61 | B = 2*( (d - e ) - g*(a - b ) ) |
| 62 | C = (-(d ) + g*(a ) + h ) |
| 63 | |
| 64 | If g goes to infinity, we can rewrite the line in terms of x. |
| 65 | x = g'*y + h' |
| 66 | |
| 67 | And solve accordingly in Mathematica: |
| 68 | |
| 69 | (in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h', |
| 70 | d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y] |
| 71 | (out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 - |
| 72 | g' (d - 2 d t + 2 e t + d t^2 - 2 e t^2 + f t^2) |
| 73 | (in) Solve[t2 == 0, t] |
| 74 | (out) { |
| 75 | {t -> (2 a - 2 b - 2 d g' + 2 e g' - |
| 76 | Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - |
| 77 | 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')]) / |
| 78 | (2 (a - 2 b + c - d g' + 2 e g' - f g')) |
| 79 | }, |
| 80 | {t -> (2 a - 2 b - 2 d g' + 2 e g' + |
| 81 | Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - |
| 82 | 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')])/ |
| 83 | (2 (a - 2 b + c - d g' + 2 e g' - f g')) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | Thus, if the slope of the line tends towards vertical, we use: |
| 88 | A = ( (a - 2*b + c) - g'*(d - 2*e + f) ) |
| 89 | B = 2*(-(a - b ) + g'*(d - e ) ) |
| 90 | C = ( (a ) - g'*(d ) - h' ) |
| 91 | */ |
| 92 | |
| 93 | |
| 94 | class LineQuadraticIntersections : public Intersections { |
| 95 | public: |
| 96 | |
| 97 | LineQuadraticIntersections(const Quadratic& q, const _Line& l, Intersections& i) |
| 98 | : quad(q) |
| 99 | , line(l) |
| 100 | , intersections(i) { |
| 101 | } |
| 102 | |
| 103 | bool intersect() { |
| 104 | double slope; |
| 105 | double axisIntercept; |
| 106 | moreHorizontal = implicitLine(line, slope, axisIntercept); |
| 107 | double A = quad[2].x; // c |
| 108 | double B = quad[1].x; // b |
| 109 | double C = quad[0].x; // a |
| 110 | A += C - 2 * B; // A = a - 2*b + c |
| 111 | B -= C; // B = -(a - b) |
| 112 | double D = quad[2].y; // f |
| 113 | double E = quad[1].y; // e |
| 114 | double F = quad[0].y; // d |
| 115 | D += F - 2 * E; // D = d - 2*e + f |
| 116 | E -= F; // E = -(d - e) |
| 117 | if (moreHorizontal) { |
| 118 | A = A * slope - D; |
| 119 | B = B * slope - E; |
| 120 | C = C * slope - F + axisIntercept; |
| 121 | } else { |
| 122 | A = A - D * slope; |
| 123 | B = B - E * slope; |
| 124 | C = C - F * slope - axisIntercept; |
| 125 | } |
| 126 | double t[2]; |
| 127 | int roots = quadraticRoots(A, B, C, t); |
| 128 | for (int x = 0; x < roots; ++x) { |
| 129 | intersections.add(t[x], findLineT(t[x])); |
| 130 | } |
| 131 | return roots > 0; |
| 132 | } |
| 133 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame^] | 134 | int horizontalIntersect(double axisIntercept) { |
| 135 | double D = quad[2].y; // f |
| 136 | double E = quad[1].y; // e |
| 137 | double F = quad[0].y; // d |
| 138 | D += F - 2 * E; // D = d - 2*e + f |
| 139 | E -= F; // E = -(d - e) |
| 140 | F -= axisIntercept; |
| 141 | return quadraticRoots(D, E, F, intersections.fT[0]); |
| 142 | } |
| 143 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 144 | protected: |
| 145 | |
| 146 | double findLineT(double t) { |
| 147 | const double* qPtr; |
| 148 | const double* lPtr; |
| 149 | if (moreHorizontal) { |
| 150 | qPtr = &quad[0].x; |
| 151 | lPtr = &line[0].x; |
| 152 | } else { |
| 153 | qPtr = &quad[0].y; |
| 154 | lPtr = &line[0].y; |
| 155 | } |
| 156 | double s = 1 - t; |
| 157 | double quadVal = qPtr[0] * s * s + 2 * qPtr[2] * s * t + qPtr[4] * t * t; |
| 158 | return (quadVal - lPtr[0]) / (lPtr[2] - lPtr[0]); |
| 159 | } |
| 160 | |
| 161 | private: |
| 162 | |
| 163 | const Quadratic& quad; |
| 164 | const _Line& line; |
| 165 | Intersections& intersections; |
| 166 | bool moreHorizontal; |
| 167 | |
| 168 | }; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame^] | 169 | |
| 170 | int horizontalIntersect(const Quadratic& quad, double left, double right, |
| 171 | double y, double tRange[2]) { |
| 172 | Intersections i; |
| 173 | LineQuadraticIntersections q(quad, *((_Line*) 0), i); |
| 174 | int result = q.horizontalIntersect(y); |
| 175 | int tCount = 0; |
| 176 | for (int index = 0; index < result; ++index) { |
| 177 | double x, y; |
| 178 | xy_at_t(quad, i.fT[0][index], x, y); |
| 179 | if (x < left || x > right) { |
| 180 | continue; |
| 181 | } |
| 182 | tRange[tCount++] = i.fT[0][index]; |
| 183 | } |
| 184 | return tCount; |
| 185 | } |
| 186 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 187 | bool intersect(const Quadratic& quad, const _Line& line, Intersections& i) { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 188 | LineQuadraticIntersections q(quad, line, i); |
| 189 | return q.intersect(); |
| 190 | } |