blob: 7fd9feff6fa557490ba0868dc368ffc038874985 [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +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 */
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00007#include "CurveIntersection.h"
caryclark@google.com8dcf1142012-07-02 20:27:02 +00008#include "CurveUtilities.h"
caryclark@google.comfa0588f2012-04-26 21:01:06 +00009#include "Extrema.h"
caryclark@google.comc6825902012-02-03 22:07:47 +000010
caryclark@google.comfa0588f2012-04-26 21:01:06 +000011static int isBoundedByEndPoints(double a, double b, double c, double d)
12{
caryclark@google.com45a8fc62013-02-14 15:29:11 +000013 return between(a, b, d) && between(a, c, d);
caryclark@google.comfa0588f2012-04-26 21:01:06 +000014}
caryclark@google.comc6825902012-02-03 22:07:47 +000015
caryclark@google.comfa0588f2012-04-26 21:01:06 +000016double leftMostT(const Cubic& cubic, double startT, double endT) {
17 double leftTs[2];
18 _Point pt[2];
caryclark@google.com45a8fc62013-02-14 15:29:11 +000019 int results = findExtrema(cubic[0].x, cubic[1].x, cubic[2].x, cubic[3].x, leftTs);
caryclark@google.comfa0588f2012-04-26 21:01:06 +000020 int best = -1;
21 for (int index = 0; index < results; ++index) {
22 if (startT > leftTs[index] || leftTs[index] > endT) {
23 continue;
24 }
25 if (best < 0) {
26 best = index;
27 continue;
28 }
29 xy_at_t(cubic, leftTs[0], pt[0].x, pt[0].y);
30 xy_at_t(cubic, leftTs[1], pt[1].x, pt[1].y);
31 if (pt[0].x > pt[1].x) {
32 best = 1;
33 }
34 }
35 if (best >= 0) {
36 return leftTs[best];
37 }
38 xy_at_t(cubic, startT, pt[0].x, pt[0].y);
39 xy_at_t(cubic, endT, pt[1].x, pt[1].y);
40 return pt[0].x <= pt[1].x ? startT : endT;
41}
42
43void _Rect::setBounds(const Cubic& cubic) {
44 set(cubic[0]);
45 add(cubic[3]);
46 double tValues[4];
47 int roots = 0;
48 if (!isBoundedByEndPoints(cubic[0].x, cubic[1].x, cubic[2].x, cubic[3].x)) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000049 roots = findExtrema(cubic[0].x, cubic[1].x, cubic[2].x, cubic[3].x, tValues);
caryclark@google.comfa0588f2012-04-26 21:01:06 +000050 }
51 if (!isBoundedByEndPoints(cubic[0].y, cubic[1].y, cubic[2].y, cubic[3].y)) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000052 roots += findExtrema(cubic[0].y, cubic[1].y, cubic[2].y, cubic[3].y, &tValues[roots]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +000053 }
54 for (int x = 0; x < roots; ++x) {
55 _Point result;
56 xy_at_t(cubic, tValues[x], result.x, result.y);
57 add(result);
58 }
59}
60
61void _Rect::setRawBounds(const Cubic& cubic) {
62 set(cubic[0]);
63 for (int x = 1; x < 4; ++x) {
64 add(cubic[x]);
65 }
66}