blob: 9aee1dfa1ab5620c94df16ba39432ab8d8bc0199 [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "CurveIntersection.h"
caryclark@google.com639df892012-01-10 21:46:10 +00002#include "IntersectionUtilities.h"
3
4/*
5Given a quadratic q, t1, and t2, find a small quadratic segment.
6
7The 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
11To find B, compute the point halfway between t1 and t2:
12
13q(at (t1 + t2)/2) == D
14
15Next, compute where D must be if we know the value of B:
16
17_12 = A/2 + B/2
1812_ = B/2 + C/2
19123 = A/4 + B/2 + C/4
20 = D
rmistry@google.comd6176b02012-08-23 18:14:13 +000021
caryclark@google.com639df892012-01-10 21:46:10 +000022Group the known values on one side:
23
24B = D*2 - A/2 - C/2
25*/
26
27static 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
35void 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 */
47static 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
59void 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}