caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1 | #include "CurveIntersection.h" |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2 | #include "Extrema.h" |
| 3 | |
| 4 | static int isBoundedByEndPoints(double a, double b, double c) |
| 5 | { |
| 6 | return (a <= b && b <= c) || (a >= b && b >= c); |
| 7 | } |
| 8 | |
| 9 | double leftMostT(const Quadratic& quad, double startT, double endT) { |
| 10 | double leftT; |
| 11 | if (findExtrema(quad[0].x, quad[1].x, quad[2].x, &leftT) |
| 12 | && startT <= leftT && leftT <= endT) { |
| 13 | return leftT; |
| 14 | } |
| 15 | _Point startPt; |
| 16 | xy_at_t(quad, startT, startPt.x, startPt.y); |
| 17 | _Point endPt; |
| 18 | xy_at_t(quad, endT, endPt.x, endPt.y); |
| 19 | return startPt.x <= endPt.x ? startT : endT; |
| 20 | } |
| 21 | |
| 22 | void _Rect::setBounds(const Quadratic& quad) { |
| 23 | set(quad[0]); |
| 24 | add(quad[2]); |
| 25 | double tValues[2]; |
| 26 | int roots = 0; |
| 27 | if (!isBoundedByEndPoints(quad[0].x, quad[1].x, quad[2].x)) { |
| 28 | roots = findExtrema(quad[0].x, quad[1].x, quad[2].x, tValues); |
| 29 | } |
| 30 | if (!isBoundedByEndPoints(quad[0].y, quad[1].y, quad[2].y)) { |
| 31 | roots += findExtrema(quad[0].y, quad[1].y, quad[2].y, |
| 32 | &tValues[roots]); |
| 33 | } |
| 34 | for (int x = 0; x < roots; ++x) { |
| 35 | _Point result; |
| 36 | xy_at_t(quad, tValues[x], result.x, result.y); |
| 37 | add(result); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void _Rect::setRawBounds(const Quadratic& quad) { |
| 42 | set(quad[0]); |
| 43 | for (int x = 1; x < 3; ++x) { |
| 44 | add(quad[x]); |
| 45 | } |
| 46 | } |