def calculateQuadraticBounds(pt1, pt2, pt3): """ Return the bounding rectangle for a qudratic bezier segment. pt1 and pt3 are the "anchor" points, pt2 is the "handle". >>> calculateQuadraticBounds((0, 0), (50, 100), (100, 0)) (0.0, 0.0, 100.0, 50.0) >>> calculateQuadraticBounds((0, 0), (100, 0), (100, 100)) (0.0, 0.0, 100.0, 100.0) """ a, b, c = calculateQuadraticParameters(pt1, pt2, pt3) ## calc first derivative ax, ay = a * 2 bx, by = b roots = [] if ax != 0: roots.append(-bx/ax) if ay != 0: roots.append(-by/ay) points = [a*t*t + b*t + c for t in roots if 0 <= t < 1] + [pt1, pt3] return array.calculateBounds(points)
def calculateCubicBounds(pt1, pt2, pt3, pt4): """ Return the bounding rectangle for a cubic bezier segment. pt1 and pt4 are the "anchor" points, pt2 and pt3 are the "handles". >>> calculateCubicBounds((0, 0), (25, 100), (75, 100), (100, 0)) (0.0, 0.0, 100.0, 75.0) >>> calculateCubicBounds((0, 0), (50, 0), (100, 50), (100, 100)) (0.0, 0.0, 100.0, 100.0) >>> calculateCubicBounds((50, 0), (0, 100), (100, 100), (50, 0)) (35.5662432703, 0.0, 64.4337567297, 75.0) """ a, b, c, d = calculateCubicParameters(pt1, pt2, pt3, pt4) # calc first derivative ax, ay = a * 3.0 bx, by = b * 2.0 cx, cy = c xRoots = [t for t in solveQuadratic(ax, bx, cx) if 0 <= t < 1] yRoots = [t for t in solveQuadratic(ay, by, cy) if 0 <= t < 1] roots = xRoots + yRoots points = [(a*t*t*t + b*t*t + c * t + d) for t in roots] + [pt1, pt4] return array.calculateBounds(points)