blob: 1bc831b643a068872bca08ab4a38060eee7d2c00 [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "CurveIntersection.h"
caryclark@google.com27accef2012-01-25 18:57:23 +00002#include "Intersections.h"
3#include "LineUtilities.h"
4#include "QuadraticUtilities.h"
5
6/*
7Find the interection of a line and quadratic by solving for valid t values.
8
9From 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
12control points) can be expressed as: F(t) = A(1 - t)^2 + B(1 - t)t + Ct^2 where
13A, B and C are points and t goes from zero to one.
14
15This 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
20If you add for instance the line equation (y = kx + m) to that, you'll end up
21with three equations and three unknowns (x, y and t)."
22
23Similar 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
26and the line as
27 y = g*x + h
28
29Using Mathematica, solve for the values of t where the quadratic intersects the
30line:
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
50Numeric 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
54and using the roots
55
56 t1 = Q / A
57 t2 = C / Q
58
59Using 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
64If g goes to infinity, we can rewrite the line in terms of x.
65 x = g'*y + h'
66
67And 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
87Thus, 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
94class LineQuadraticIntersections : public Intersections {
95public:
96
97LineQuadraticIntersections(const Quadratic& q, const _Line& l, Intersections& i)
98 : quad(q)
99 , line(l)
100 , intersections(i) {
101}
102
103bool 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.com198e0542012-03-30 18:47:02 +0000134int 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.comfa0588f2012-04-26 21:01:06 +0000144int verticalIntersect(double axisIntercept) {
145 double D = quad[2].x; // f
146 double E = quad[1].x; // e
147 double F = quad[0].x; // d
148 D += F - 2 * E; // D = d - 2*e + f
149 E -= F; // E = -(d - e)
150 F -= axisIntercept;
151 return quadraticRoots(D, E, F, intersections.fT[0]);
152}
153
caryclark@google.com27accef2012-01-25 18:57:23 +0000154protected:
155
156double findLineT(double t) {
157 const double* qPtr;
158 const double* lPtr;
159 if (moreHorizontal) {
160 qPtr = &quad[0].x;
161 lPtr = &line[0].x;
162 } else {
163 qPtr = &quad[0].y;
164 lPtr = &line[0].y;
165 }
166 double s = 1 - t;
167 double quadVal = qPtr[0] * s * s + 2 * qPtr[2] * s * t + qPtr[4] * t * t;
168 return (quadVal - lPtr[0]) / (lPtr[2] - lPtr[0]);
169}
170
171private:
172
173const Quadratic& quad;
174const _Line& line;
175Intersections& intersections;
176bool moreHorizontal;
177
178};
caryclark@google.com198e0542012-03-30 18:47:02 +0000179
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000180// utility for pairs of coincident quads
181static double horizontalIntersect(const Quadratic& quad, const _Point& pt) {
182 Intersections intersections;
183 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
184 int result = q.horizontalIntersect(pt.y);
185 if (result == 0) {
186 return -1;
187 }
188 assert(result == 1);
189 double x, y;
190 xy_at_t(quad, intersections.fT[0][0], x, y);
191 if (approximately_equal(x, pt.x)) {
192 return intersections.fT[0][0];
193 }
194 return -1;
195}
196
197static double verticalIntersect(const Quadratic& quad, const _Point& pt) {
198 Intersections intersections;
199 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
200 int result = q.horizontalIntersect(pt.x);
201 if (result == 0) {
202 return -1;
203 }
204 assert(result == 1);
205 double x, y;
206 xy_at_t(quad, intersections.fT[0][0], x, y);
207 if (approximately_equal(y, pt.y)) {
208 return intersections.fT[0][0];
209 }
210 return -1;
211}
212
213double axialIntersect(const Quadratic& q1, const _Point& p, bool vertical) {
214 if (vertical) {
215 return verticalIntersect(q1, p);
216 }
217 return horizontalIntersect(q1, p);
218}
219
caryclark@google.com198e0542012-03-30 18:47:02 +0000220int horizontalIntersect(const Quadratic& quad, double left, double right,
221 double y, double tRange[2]) {
222 Intersections i;
223 LineQuadraticIntersections q(quad, *((_Line*) 0), i);
224 int result = q.horizontalIntersect(y);
225 int tCount = 0;
226 for (int index = 0; index < result; ++index) {
227 double x, y;
228 xy_at_t(quad, i.fT[0][index], x, y);
229 if (x < left || x > right) {
230 continue;
231 }
232 tRange[tCount++] = i.fT[0][index];
233 }
234 return tCount;
235}
236
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000237int horizontalIntersect(const Quadratic& quad, double left, double right, double y,
238 bool flipped, Intersections& intersections) {
239 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
240 int result = q.horizontalIntersect(y);
241 for (int index = 0; index < result; ) {
242 double x, y;
243 xy_at_t(quad, intersections.fT[0][index], x, y);
244 if (x < left || x > right) {
245 if (--result > index) {
246 intersections.fT[0][index] = intersections.fT[0][result];
247 }
248 continue;
249 }
250 intersections.fT[0][index] = (x - left) / (right - left);
251 ++index;
252 }
253 if (flipped) {
254 // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x
255 for (int index = 0; index < result; ++index) {
256 intersections.fT[1][index] = 1 - intersections.fT[1][index];
257 }
258 }
259 return result;
260}
261
262int verticalIntersect(const Quadratic& quad, double top, double bottom, double x,
263 bool flipped, Intersections& intersections) {
264 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
265 int result = q.verticalIntersect(x);
266 for (int index = 0; index < result; ) {
267 double x, y;
268 xy_at_t(quad, intersections.fT[0][index], x, y);
269 if (y < top || y > bottom) {
270 if (--result > index) {
271 intersections.fT[0][index] = intersections.fT[0][result];
272 }
273 continue;
274 }
275 intersections.fT[0][index] = (y - top) / (bottom - top);
276 ++index;
277 }
278 if (flipped) {
279 // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x
280 for (int index = 0; index < result; ++index) {
281 intersections.fT[1][index] = 1 - intersections.fT[1][index];
282 }
283 }
284 return result;
285}
286
caryclark@google.comc6825902012-02-03 22:07:47 +0000287bool intersect(const Quadratic& quad, const _Line& line, Intersections& i) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000288 LineQuadraticIntersections q(quad, line, i);
289 return q.intersect();
290}