blob: 024fb9637f154219f092d9ac453c7e1fe2f835aa [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"
caryclark@google.comc6825902012-02-03 22:07:47 +00004
caryclark@google.comfa0588f2012-04-26 21:01:06 +00005static int isBoundedByEndPoints(double a, double b, double c, double d)
6{
7 return (a <= b && a <= c && b <= d && c <= d)
8 || (a >= b && a >= c && b >= d && c >= d);
9}
caryclark@google.comc6825902012-02-03 22:07:47 +000010
caryclark@google.comfa0588f2012-04-26 21:01:06 +000011double leftMostT(const Cubic& cubic, double startT, double endT) {
12 double leftTs[2];
13 _Point pt[2];
14 int results = findExtrema(cubic[0].x, cubic[1].x, cubic[2].x, cubic[3].x,
15 leftTs);
16 int best = -1;
17 for (int index = 0; index < results; ++index) {
18 if (startT > leftTs[index] || leftTs[index] > endT) {
19 continue;
20 }
21 if (best < 0) {
22 best = index;
23 continue;
24 }
25 xy_at_t(cubic, leftTs[0], pt[0].x, pt[0].y);
26 xy_at_t(cubic, leftTs[1], pt[1].x, pt[1].y);
27 if (pt[0].x > pt[1].x) {
28 best = 1;
29 }
30 }
31 if (best >= 0) {
32 return leftTs[best];
33 }
34 xy_at_t(cubic, startT, pt[0].x, pt[0].y);
35 xy_at_t(cubic, endT, pt[1].x, pt[1].y);
36 return pt[0].x <= pt[1].x ? startT : endT;
37}
38
39void _Rect::setBounds(const Cubic& cubic) {
40 set(cubic[0]);
41 add(cubic[3]);
42 double tValues[4];
43 int roots = 0;
44 if (!isBoundedByEndPoints(cubic[0].x, cubic[1].x, cubic[2].x, cubic[3].x)) {
45 roots = findExtrema(cubic[0].x, cubic[1].x, cubic[2].x,
46 cubic[3].x, tValues);
47 }
48 if (!isBoundedByEndPoints(cubic[0].y, cubic[1].y, cubic[2].y, cubic[3].y)) {
49 roots += findExtrema(cubic[0].y, cubic[1].y, cubic[2].y,
50 cubic[3].y, &tValues[roots]);
51 }
52 for (int x = 0; x < roots; ++x) {
53 _Point result;
54 xy_at_t(cubic, tValues[x], result.x, result.y);
55 add(result);
56 }
57}
58
59void _Rect::setRawBounds(const Cubic& cubic) {
60 set(cubic[0]);
61 for (int x = 1; x < 4; ++x) {
62 add(cubic[x]);
63 }
64}