blob: 5e3596ec208e6b436ae13514cbf4e4acf5f021c0 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
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 */
7#include "SkIntersections.h"
8#include "SkPathOpsLine.h"
9#include "SkPathOpsQuad.h"
10
11/*
12Find the interection of a line and quadratic by solving for valid t values.
13
14From http://stackoverflow.com/questions/1853637/how-to-find-the-mathematical-function-defining-a-bezier-curve
15
16"A Bezier curve is a parametric function. A quadratic Bezier curve (i.e. three
17control points) can be expressed as: F(t) = A(1 - t)^2 + B(1 - t)t + Ct^2 where
18A, B and C are points and t goes from zero to one.
19
20This will give you two equations:
21
22 x = a(1 - t)^2 + b(1 - t)t + ct^2
23 y = d(1 - t)^2 + e(1 - t)t + ft^2
24
25If you add for instance the line equation (y = kx + m) to that, you'll end up
26with three equations and three unknowns (x, y and t)."
27
28Similar to above, the quadratic is represented as
29 x = a(1-t)^2 + 2b(1-t)t + ct^2
30 y = d(1-t)^2 + 2e(1-t)t + ft^2
31and the line as
32 y = g*x + h
33
34Using Mathematica, solve for the values of t where the quadratic intersects the
35line:
36
37 (in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x,
38 d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x]
39 (out) -d + h + 2 d t - 2 e t - d t^2 + 2 e t^2 - f t^2 +
40 g (a - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2)
41 (in) Solve[t1 == 0, t]
42 (out) {
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 {t -> (-2 d + 2 e + 2 a g - 2 b g +
49 Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 -
50 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) /
51 (2 (-d + 2 e - f + a g - 2 b g + c g))
52 }
53 }
54
55Using the results above (when the line tends towards horizontal)
56 A = (-(d - 2*e + f) + g*(a - 2*b + c) )
57 B = 2*( (d - e ) - g*(a - b ) )
58 C = (-(d ) + g*(a ) + h )
59
60If g goes to infinity, we can rewrite the line in terms of x.
61 x = g'*y + h'
62
63And solve accordingly in Mathematica:
64
65 (in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h',
66 d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y]
67 (out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 -
68 g' (d - 2 d t + 2 e t + d t^2 - 2 e t^2 + f t^2)
69 (in) Solve[t2 == 0, t]
70 (out) {
71 {t -> (2 a - 2 b - 2 d g' + 2 e g' -
72 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
73 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')]) /
74 (2 (a - 2 b + c - d g' + 2 e g' - f g'))
75 },
76 {t -> (2 a - 2 b - 2 d g' + 2 e g' +
77 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
78 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')])/
79 (2 (a - 2 b + c - d g' + 2 e g' - f g'))
80 }
81 }
82
83Thus, if the slope of the line tends towards vertical, we use:
84 A = ( (a - 2*b + c) - g'*(d - 2*e + f) )
85 B = 2*(-(a - b ) + g'*(d - e ) )
86 C = ( (a ) - g'*(d ) - h' )
87 */
88
caryclark@google.com07393ca2013-04-08 11:47:37 +000089class LineQuadraticIntersections {
90public:
caryclark@google.com4fdbb222013-07-23 15:27:41 +000091 enum PinTPoint {
92 kPointUninitialized,
93 kPointInitialized
94 };
95
caryclark@google.com07393ca2013-04-08 11:47:37 +000096 LineQuadraticIntersections(const SkDQuad& q, const SkDLine& l, SkIntersections* i)
caryclark@google.com4fdbb222013-07-23 15:27:41 +000097 : fQuad(q)
caryclark624637c2015-05-11 07:21:27 -070098 , fLine(&l)
caryclark@google.com4fdbb222013-07-23 15:27:41 +000099 , fIntersections(i)
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000100 , fAllowNear(true) {
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000101 i->setMax(3); // allow short partial coincidence plus discrete intersection
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000102 }
103
caryclark624637c2015-05-11 07:21:27 -0700104 LineQuadraticIntersections(const SkDQuad& q)
105 : fQuad(q)
106 SkDEBUGPARAMS(fLine(NULL))
107 SkDEBUGPARAMS(fIntersections(NULL))
108 SkDEBUGPARAMS(fAllowNear(false)) {
109 }
110
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000111 void allowNear(bool allow) {
112 fAllowNear = allow;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000113 }
114
caryclark54359292015-03-26 07:52:43 -0700115 void checkCoincident() {
116 int last = fIntersections->used() - 1;
117 for (int index = 0; index < last; ) {
118 double quadMidT = ((*fIntersections)[0][index] + (*fIntersections)[0][index + 1]) / 2;
119 SkDPoint quadMidPt = fQuad.ptAtT(quadMidT);
caryclark624637c2015-05-11 07:21:27 -0700120 double t = fLine->nearPoint(quadMidPt, NULL);
caryclark54359292015-03-26 07:52:43 -0700121 if (t < 0) {
122 ++index;
123 continue;
124 }
125 if (fIntersections->isCoincident(index)) {
126 fIntersections->removeOne(index);
127 --last;
128 } else if (fIntersections->isCoincident(index + 1)) {
129 fIntersections->removeOne(index + 1);
130 --last;
131 } else {
132 fIntersections->setCoincident(index++);
133 }
134 fIntersections->setCoincident(index);
135 }
136 }
137
caryclark@google.com07393ca2013-04-08 11:47:37 +0000138 int intersectRay(double roots[2]) {
139 /*
140 solve by rotating line+quad so line is horizontal, then finding the roots
141 set up matrix to rotate quad to x-axis
142 |cos(a) -sin(a)|
143 |sin(a) cos(a)|
144 note that cos(a) = A(djacent) / Hypoteneuse
145 sin(a) = O(pposite) / Hypoteneuse
146 since we are computing Ts, we can ignore hypoteneuse, the scale factor:
147 | A -O |
148 | O A |
149 A = line[1].fX - line[0].fX (adjacent side of the right triangle)
150 O = line[1].fY - line[0].fY (opposite side of the right triangle)
151 for each of the three points (e.g. n = 0 to 2)
152 quad[n].fY' = (quad[n].fY - line[0].fY) * A - (quad[n].fX - line[0].fX) * O
153 */
caryclark624637c2015-05-11 07:21:27 -0700154 double adj = (*fLine)[1].fX - (*fLine)[0].fX;
155 double opp = (*fLine)[1].fY - (*fLine)[0].fY;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000156 double r[3];
157 for (int n = 0; n < 3; ++n) {
caryclark624637c2015-05-11 07:21:27 -0700158 r[n] = (fQuad[n].fY - (*fLine)[0].fY) * adj - (fQuad[n].fX - (*fLine)[0].fX) * opp;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000159 }
160 double A = r[2];
161 double B = r[1];
162 double C = r[0];
163 A += C - 2 * B; // A = a - 2*b + c
164 B -= C; // B = -(b - c)
165 return SkDQuad::RootsValidT(A, 2 * B, C, roots);
166 }
167
168 int intersect() {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000169 addExactEndPoints();
caryclark@google.com570863f2013-09-16 15:55:01 +0000170 if (fAllowNear) {
171 addNearEndPoints();
172 }
caryclark54359292015-03-26 07:52:43 -0700173 double rootVals[2];
174 int roots = intersectRay(rootVals);
175 for (int index = 0; index < roots; ++index) {
176 double quadT = rootVals[index];
177 double lineT = findLineT(quadT);
178 SkDPoint pt;
179 if (pinTs(&quadT, &lineT, &pt, kPointUninitialized) && uniqueAnswer(quadT, pt)) {
180 fIntersections->insert(quadT, lineT, pt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000181 }
182 }
caryclark54359292015-03-26 07:52:43 -0700183 checkCoincident();
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000184 return fIntersections->used();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000185 }
186
187 int horizontalIntersect(double axisIntercept, double roots[2]) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000188 double D = fQuad[2].fY; // f
189 double E = fQuad[1].fY; // e
190 double F = fQuad[0].fY; // d
caryclark@google.com07393ca2013-04-08 11:47:37 +0000191 D += F - 2 * E; // D = d - 2*e + f
192 E -= F; // E = -(d - e)
193 F -= axisIntercept;
194 return SkDQuad::RootsValidT(D, 2 * E, F, roots);
195 }
196
197 int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000198 addExactHorizontalEndPoints(left, right, axisIntercept);
caryclark@google.com570863f2013-09-16 15:55:01 +0000199 if (fAllowNear) {
200 addNearHorizontalEndPoints(left, right, axisIntercept);
201 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000202 double rootVals[2];
203 int roots = horizontalIntersect(axisIntercept, rootVals);
204 for (int index = 0; index < roots; ++index) {
205 double quadT = rootVals[index];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000206 SkDPoint pt = fQuad.ptAtT(quadT);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000207 double lineT = (pt.fX - left) / (right - left);
caryclark54359292015-03-26 07:52:43 -0700208 if (pinTs(&quadT, &lineT, &pt, kPointInitialized) && uniqueAnswer(quadT, pt)) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000209 fIntersections->insert(quadT, lineT, pt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000210 }
211 }
212 if (flipped) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000213 fIntersections->flip();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000214 }
caryclark54359292015-03-26 07:52:43 -0700215 checkCoincident();
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000216 return fIntersections->used();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000217 }
218
caryclark54359292015-03-26 07:52:43 -0700219 bool uniqueAnswer(double quadT, const SkDPoint& pt) {
220 for (int inner = 0; inner < fIntersections->used(); ++inner) {
221 if (fIntersections->pt(inner) != pt) {
222 continue;
223 }
224 double existingQuadT = (*fIntersections)[0][inner];
225 if (quadT == existingQuadT) {
226 return false;
227 }
228 // check if midway on quad is also same point. If so, discard this
229 double quadMidT = (existingQuadT + quadT) / 2;
230 SkDPoint quadMidPt = fQuad.ptAtT(quadMidT);
231 if (quadMidPt.approximatelyEqual(pt)) {
232 return false;
233 }
234 }
235#if ONE_OFF_DEBUG
236 SkDPoint qPt = fQuad.ptAtT(quadT);
237 SkDebugf("%s pt=(%1.9g,%1.9g) cPt=(%1.9g,%1.9g)\n", __FUNCTION__, pt.fX, pt.fY,
238 qPt.fX, qPt.fY);
239#endif
240 return true;
241 }
242
caryclark@google.com07393ca2013-04-08 11:47:37 +0000243 int verticalIntersect(double axisIntercept, double roots[2]) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000244 double D = fQuad[2].fX; // f
245 double E = fQuad[1].fX; // e
246 double F = fQuad[0].fX; // d
caryclark@google.com07393ca2013-04-08 11:47:37 +0000247 D += F - 2 * E; // D = d - 2*e + f
248 E -= F; // E = -(d - e)
249 F -= axisIntercept;
250 return SkDQuad::RootsValidT(D, 2 * E, F, roots);
251 }
252
253 int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000254 addExactVerticalEndPoints(top, bottom, axisIntercept);
caryclark@google.com570863f2013-09-16 15:55:01 +0000255 if (fAllowNear) {
256 addNearVerticalEndPoints(top, bottom, axisIntercept);
257 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000258 double rootVals[2];
259 int roots = verticalIntersect(axisIntercept, rootVals);
260 for (int index = 0; index < roots; ++index) {
261 double quadT = rootVals[index];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000262 SkDPoint pt = fQuad.ptAtT(quadT);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000263 double lineT = (pt.fY - top) / (bottom - top);
caryclark54359292015-03-26 07:52:43 -0700264 if (pinTs(&quadT, &lineT, &pt, kPointInitialized) && uniqueAnswer(quadT, pt)) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000265 fIntersections->insert(quadT, lineT, pt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000266 }
267 }
268 if (flipped) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000269 fIntersections->flip();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000270 }
caryclark54359292015-03-26 07:52:43 -0700271 checkCoincident();
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000272 return fIntersections->used();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000273 }
274
275protected:
276 // add endpoints first to get zero and one t values exactly
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000277 void addExactEndPoints() {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000278 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
caryclark624637c2015-05-11 07:21:27 -0700279 double lineT = fLine->exactPoint(fQuad[qIndex]);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000280 if (lineT < 0) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000281 continue;
282 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000283 double quadT = (double) (qIndex >> 1);
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000284 fIntersections->insert(quadT, lineT, fQuad[qIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000285 }
286 }
287
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000288 void addNearEndPoints() {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000289 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000290 double quadT = (double) (qIndex >> 1);
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000291 if (fIntersections->hasT(quadT)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000292 continue;
293 }
caryclark624637c2015-05-11 07:21:27 -0700294 double lineT = fLine->nearPoint(fQuad[qIndex], NULL);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000295 if (lineT < 0) {
296 continue;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000297 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000298 fIntersections->insert(quadT, lineT, fQuad[qIndex]);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000299 }
300 // FIXME: see if line end is nearly on quad
301 }
302
303 void addExactHorizontalEndPoints(double left, double right, double y) {
304 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000305 double lineT = SkDLine::ExactPointH(fQuad[qIndex], left, right, y);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000306 if (lineT < 0) {
307 continue;
308 }
309 double quadT = (double) (qIndex >> 1);
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000310 fIntersections->insert(quadT, lineT, fQuad[qIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000311 }
312 }
313
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000314 void addNearHorizontalEndPoints(double left, double right, double y) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000315 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000316 double quadT = (double) (qIndex >> 1);
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000317 if (fIntersections->hasT(quadT)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000318 continue;
319 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000320 double lineT = SkDLine::NearPointH(fQuad[qIndex], left, right, y);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000321 if (lineT < 0) {
322 continue;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000323 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000324 fIntersections->insert(quadT, lineT, fQuad[qIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000325 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000326 // FIXME: see if line end is nearly on quad
327 }
328
329 void addExactVerticalEndPoints(double top, double bottom, double x) {
330 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000331 double lineT = SkDLine::ExactPointV(fQuad[qIndex], top, bottom, x);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000332 if (lineT < 0) {
333 continue;
334 }
335 double quadT = (double) (qIndex >> 1);
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000336 fIntersections->insert(quadT, lineT, fQuad[qIndex]);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000337 }
338 }
339
340 void addNearVerticalEndPoints(double top, double bottom, double x) {
341 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
342 double quadT = (double) (qIndex >> 1);
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000343 if (fIntersections->hasT(quadT)) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000344 continue;
345 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000346 double lineT = SkDLine::NearPointV(fQuad[qIndex], top, bottom, x);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000347 if (lineT < 0) {
348 continue;
349 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000350 fIntersections->insert(quadT, lineT, fQuad[qIndex]);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000351 }
352 // FIXME: see if line end is nearly on quad
caryclark@google.com07393ca2013-04-08 11:47:37 +0000353 }
354
355 double findLineT(double t) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000356 SkDPoint xy = fQuad.ptAtT(t);
caryclark624637c2015-05-11 07:21:27 -0700357 double dx = (*fLine)[1].fX - (*fLine)[0].fX;
358 double dy = (*fLine)[1].fY - (*fLine)[0].fY;
caryclark@google.com28d219c2013-11-25 13:39:12 +0000359 if (fabs(dx) > fabs(dy)) {
caryclark624637c2015-05-11 07:21:27 -0700360 return (xy.fX - (*fLine)[0].fX) / dx;
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000361 }
caryclark624637c2015-05-11 07:21:27 -0700362 return (xy.fY - (*fLine)[0].fY) / dy;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000363 }
364
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000365 bool pinTs(double* quadT, double* lineT, SkDPoint* pt, PinTPoint ptSet) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000366 if (!approximately_one_or_less_double(*lineT)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000367 return false;
368 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000369 if (!approximately_zero_or_more_double(*lineT)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000370 return false;
371 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000372 double qT = *quadT = SkPinT(*quadT);
373 double lT = *lineT = SkPinT(*lineT);
374 if (lT == 0 || lT == 1 || (ptSet == kPointUninitialized && qT != 0 && qT != 1)) {
caryclark624637c2015-05-11 07:21:27 -0700375 *pt = (*fLine).ptAtT(lT);
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000376 } else if (ptSet == kPointUninitialized) {
377 *pt = fQuad.ptAtT(qT);
378 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000379 SkPoint gridPt = pt->asSkPoint();
caryclark624637c2015-05-11 07:21:27 -0700380 if (SkDPoint::ApproximatelyEqual(gridPt, (*fLine)[0].asSkPoint())) {
381 *pt = (*fLine)[0];
caryclark@google.com570863f2013-09-16 15:55:01 +0000382 *lineT = 0;
caryclark624637c2015-05-11 07:21:27 -0700383 } else if (SkDPoint::ApproximatelyEqual(gridPt, (*fLine)[1].asSkPoint())) {
384 *pt = (*fLine)[1];
caryclark@google.com570863f2013-09-16 15:55:01 +0000385 *lineT = 1;
386 }
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000387 if (fIntersections->used() > 0 && approximately_equal((*fIntersections)[1][0], *lineT)) {
388 return false;
389 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000390 if (gridPt == fQuad[0].asSkPoint()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000391 *pt = fQuad[0];
caryclark@google.com570863f2013-09-16 15:55:01 +0000392 *quadT = 0;
393 } else if (gridPt == fQuad[2].asSkPoint()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000394 *pt = fQuad[2];
caryclark@google.com570863f2013-09-16 15:55:01 +0000395 *quadT = 1;
396 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000397 return true;
398 }
399
400private:
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000401 const SkDQuad& fQuad;
caryclark624637c2015-05-11 07:21:27 -0700402 const SkDLine* fLine;
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000403 SkIntersections* fIntersections;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000404 bool fAllowNear;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000405};
406
caryclark@google.com07393ca2013-04-08 11:47:37 +0000407int SkIntersections::horizontal(const SkDQuad& quad, double left, double right, double y,
408 bool flipped) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000409 SkDLine line = {{{ left, y }, { right, y }}};
410 LineQuadraticIntersections q(quad, line, this);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000411 return q.horizontalIntersect(y, left, right, flipped);
412}
413
414int SkIntersections::vertical(const SkDQuad& quad, double top, double bottom, double x,
415 bool flipped) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000416 SkDLine line = {{{ x, top }, { x, bottom }}};
417 LineQuadraticIntersections q(quad, line, this);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000418 return q.verticalIntersect(x, top, bottom, flipped);
419}
420
421int SkIntersections::intersect(const SkDQuad& quad, const SkDLine& line) {
422 LineQuadraticIntersections q(quad, line, this);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000423 q.allowNear(fAllowNear);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000424 return q.intersect();
425}
426
427int SkIntersections::intersectRay(const SkDQuad& quad, const SkDLine& line) {
428 LineQuadraticIntersections q(quad, line, this);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000429 fUsed = q.intersectRay(fT[0]);
430 for (int index = 0; index < fUsed; ++index) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000431 fPt[index] = quad.ptAtT(fT[0][index]);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000432 }
433 return fUsed;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000434}
caryclark624637c2015-05-11 07:21:27 -0700435
436int SkIntersections::HorizontalIntercept(const SkDQuad& quad, SkScalar y, double* roots) {
437 LineQuadraticIntersections q(quad);
438 return q.horizontalIntersect(y, roots);
439}
440
441int SkIntersections::VerticalIntercept(const SkDQuad& quad, SkScalar x, double* roots) {
442 LineQuadraticIntersections q(quad);
443 return q.verticalIntersect(x, roots);
444}