blob: a86b7edb5a735cd9e93da98670eb81477299726c [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"
10
11static int isBoundedByEndPoints(double a, double b, double c)
12{
13 return (a <= b && b <= c) || (a >= b && b >= c);
14}
15
16double leftMostT(const Quadratic& quad, double startT, double endT) {
17 double leftT;
18 if (findExtrema(quad[0].x, quad[1].x, quad[2].x, &leftT)
19 && startT <= leftT && leftT <= endT) {
20 return leftT;
21 }
22 _Point startPt;
23 xy_at_t(quad, startT, startPt.x, startPt.y);
24 _Point endPt;
25 xy_at_t(quad, endT, endPt.x, endPt.y);
26 return startPt.x <= endPt.x ? startT : endT;
27}
28
29void _Rect::setBounds(const Quadratic& quad) {
30 set(quad[0]);
31 add(quad[2]);
32 double tValues[2];
33 int roots = 0;
34 if (!isBoundedByEndPoints(quad[0].x, quad[1].x, quad[2].x)) {
35 roots = findExtrema(quad[0].x, quad[1].x, quad[2].x, tValues);
36 }
37 if (!isBoundedByEndPoints(quad[0].y, quad[1].y, quad[2].y)) {
38 roots += findExtrema(quad[0].y, quad[1].y, quad[2].y,
39 &tValues[roots]);
40 }
41 for (int x = 0; x < roots; ++x) {
42 _Point result;
43 xy_at_t(quad, tValues[x], result.x, result.y);
44 add(result);
45 }
46}
47
48void _Rect::setRawBounds(const Quadratic& quad) {
49 set(quad[0]);
50 for (int x = 1; x < 3; ++x) {
51 add(quad[x]);
52 }
53}