| caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 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.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 7 | #include "CurveIntersection.h" |
| caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 8 | #include "CurveUtilities.h" |
| caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 9 | #include "Extrema.h" |
| 10 | |
| 11 | static int isBoundedByEndPoints(double a, double b, double c) |
| 12 | { |
| 13 | return (a <= b && b <= c) || (a >= b && b >= c); |
| 14 | } |
| 15 | |
| 16 | double 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 | |
| 29 | void _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 | |
| 48 | void _Rect::setRawBounds(const Quadratic& quad) { |
| 49 | set(quad[0]); |
| 50 | for (int x = 1; x < 3; ++x) { |
| 51 | add(quad[x]); |
| 52 | } |
| 53 | } |