caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1 | #include "CurveIntersection.h" |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame^] | 2 | #include "CurveUtilities.h" |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3 | #include "Extrema.h" |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 4 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 5 | static 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.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 10 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 11 | double 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 | |
| 39 | void _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 | |
| 59 | void _Rect::setRawBounds(const Cubic& cubic) { |
| 60 | set(cubic[0]); |
| 61 | for (int x = 1; x < 4; ++x) { |
| 62 | add(cubic[x]); |
| 63 | } |
| 64 | } |