blob: 02810ac2315ba0c2336d110c5be81277fb1561de [file] [log] [blame]
caryclark@google.com27accef2012-01-25 18:57:23 +00001#include "CubicIntersection.h"
2#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
134protected:
135
136double findLineT(double t) {
137 const double* qPtr;
138 const double* lPtr;
139 if (moreHorizontal) {
140 qPtr = &quad[0].x;
141 lPtr = &line[0].x;
142 } else {
143 qPtr = &quad[0].y;
144 lPtr = &line[0].y;
145 }
146 double s = 1 - t;
147 double quadVal = qPtr[0] * s * s + 2 * qPtr[2] * s * t + qPtr[4] * t * t;
148 return (quadVal - lPtr[0]) / (lPtr[2] - lPtr[0]);
149}
150
151private:
152
153const Quadratic& quad;
154const _Line& line;
155Intersections& intersections;
156bool moreHorizontal;
157
158};
159
160bool intersectStart(const Quadratic& quad, const _Line& line, Intersections& i) {
161 LineQuadraticIntersections q(quad, line, i);
162 return q.intersect();
163}