caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame^] | 1 | #include "CubicIntersection.h" |
| 2 | #include "IntersectionUtilities.h" |
| 3 | |
| 4 | /* |
| 5 | Given a quadratic q, t1, and t2, find a small quadratic segment. |
| 6 | |
| 7 | The new quadratic is defined by A, B, and C, where |
| 8 | A = c[0]*(1 - t1)*(1 - t1) + 2*c[1]*t1*(1 - t1) + c[2]*t1*t1 |
| 9 | C = c[3]*(1 - t1)*(1 - t1) + 2*c[2]*t1*(1 - t1) + c[1]*t1*t1 |
| 10 | |
| 11 | To find B, compute the point halfway between t1 and t2: |
| 12 | |
| 13 | q(at (t1 + t2)/2) == D |
| 14 | |
| 15 | Next, compute where D must be if we know the value of B: |
| 16 | |
| 17 | _12 = A/2 + B/2 |
| 18 | 12_ = B/2 + C/2 |
| 19 | 123 = A/4 + B/2 + C/4 |
| 20 | = D |
| 21 | |
| 22 | Group the known values on one side: |
| 23 | |
| 24 | B = D*2 - A/2 - C/2 |
| 25 | */ |
| 26 | |
| 27 | static double interp_quad_coords(const double* src, double t) |
| 28 | { |
| 29 | double ab = interp(src[0], src[2], t); |
| 30 | double bc = interp(src[2], src[4], t); |
| 31 | double abc = interp(ab, bc, t); |
| 32 | return abc; |
| 33 | } |
| 34 | |
| 35 | void sub_divide(const Quadratic& src, double t1, double t2, Quadratic& dst) { |
| 36 | double ax = dst[0].x = interp_quad_coords(&src[0].x, t1); |
| 37 | double ay = dst[0].y = interp_quad_coords(&src[0].y, t1); |
| 38 | double dx = interp_quad_coords(&src[0].x, (t1 + t2) / 2); |
| 39 | double dy = interp_quad_coords(&src[0].y, (t1 + t2) / 2); |
| 40 | double cx = dst[2].x = interp_quad_coords(&src[0].x, t2); |
| 41 | double cy = dst[2].y = interp_quad_coords(&src[0].y, t2); |
| 42 | /* bx = */ dst[1].x = 2*dx - (ax + cx)/2; |
| 43 | /* by = */ dst[1].y = 2*dy - (ay + cy)/2; |
| 44 | } |
| 45 | |
| 46 | /* classic one t subdivision */ |
| 47 | static void interp_quad_coords(const double* src, double* dst, double t) |
| 48 | { |
| 49 | double ab = interp(src[0], src[2], t); |
| 50 | double bc = interp(src[2], src[4], t); |
| 51 | |
| 52 | dst[0] = src[0]; |
| 53 | dst[2] = ab; |
| 54 | dst[4] = interp(ab, bc, t); |
| 55 | dst[6] = bc; |
| 56 | dst[8] = src[4]; |
| 57 | } |
| 58 | |
| 59 | void chop_at(const Quadratic& src, QuadraticPair& dst, double t) |
| 60 | { |
| 61 | interp_quad_coords(&src[0].x, &dst.pts[0].x, t); |
| 62 | interp_quad_coords(&src[0].y, &dst.pts[0].y, t); |
| 63 | } |