blob: 27c170e808631b169ca873e1400fe9ac17e4eb4c [file] [log] [blame]
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001#include "CurveIntersection.h"
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002#include "CurveUtilities.h"
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003#include "Extrema.h"
4
5static int isBoundedByEndPoints(double a, double b, double c)
6{
7 return (a <= b && b <= c) || (a >= b && b >= c);
8}
9
10double leftMostT(const Quadratic& quad, double startT, double endT) {
11 double leftT;
12 if (findExtrema(quad[0].x, quad[1].x, quad[2].x, &leftT)
13 && startT <= leftT && leftT <= endT) {
14 return leftT;
15 }
16 _Point startPt;
17 xy_at_t(quad, startT, startPt.x, startPt.y);
18 _Point endPt;
19 xy_at_t(quad, endT, endPt.x, endPt.y);
20 return startPt.x <= endPt.x ? startT : endT;
21}
22
23void _Rect::setBounds(const Quadratic& quad) {
24 set(quad[0]);
25 add(quad[2]);
26 double tValues[2];
27 int roots = 0;
28 if (!isBoundedByEndPoints(quad[0].x, quad[1].x, quad[2].x)) {
29 roots = findExtrema(quad[0].x, quad[1].x, quad[2].x, tValues);
30 }
31 if (!isBoundedByEndPoints(quad[0].y, quad[1].y, quad[2].y)) {
32 roots += findExtrema(quad[0].y, quad[1].y, quad[2].y,
33 &tValues[roots]);
34 }
35 for (int x = 0; x < roots; ++x) {
36 _Point result;
37 xy_at_t(quad, tValues[x], result.x, result.y);
38 add(result);
39 }
40}
41
42void _Rect::setRawBounds(const Quadratic& quad) {
43 set(quad[0]);
44 for (int x = 1; x < 3; ++x) {
45 add(quad[x]);
46 }
47}