Test iOS quadratic Bézier bounds on a cloud Mac
The curve looks fine in the editor’s local preview, but the thumbnail exported by the pipeline clips the top of the arch. Before adding more crop padding, check whether the bounds use only the two endpoints—or whether the control-point bounds have been mistaken for the curve’s actual dimensions. A small geometry check written entirely in Swift can catch these errors in a cloud Mac build job before any screenshots are taken, without launching a simulator.
Define the bounds
This article covers quadratic Bézier curves only: a start point P0, a control point P1, and an endpoint P2, with the parameter t in the closed interval [0, 1]. Cubic curves require a different derivative-root calculation; this method does not apply directly.
The bounding box of all three control points always contains the curve, but it may include too much empty space. Using only the endpoints, on the other hand, can miss a bulge in the middle. The goal is to find the tight bounding box of the curve’s centerline for canvas sizing and cropping.
Path bounds are not the same as visible pixel bounds. Stroke width, line caps, joins, shadows, and antialiasing belong to the subsequent rendering contract. Passing geometry tests is no substitute for rendering acceptance checks.
Start by constraining the input: every coordinate must be finite, with an absolute value no greater than one million logical units. This limit is part of the example’s computational contract, not a system limitation. Adjust it to match your canvas size, and explicitly reject out-of-range input rather than silently clamping it.
Calculate the extrema
For either coordinate component, the curve is given by:
B(t) = (1-t)²P0 + 2(1-t)tP1 + t²P2
Setting the derivative to zero gives the candidate parameter:
t = (P0-P1) / (P0-2P1+P2)
Calculate the candidates for the horizontal and vertical coordinates separately. Keep only values strictly between zero and one, then add the two endpoints. Evaluate the curve at those parameters and take the minimum and maximum coordinates. Horizontal and vertical extrema generally occur at different positions, so calculating t just once is not enough.
A zero denominator means the derivative of that coordinate component is constant or identically zero. Neither case needs an additional candidate point. Do not introduce an arbitrary fixed epsilon to treat a “small denominator” as zero: doing so can misclassify valid curves after they have been scaled down.
Run the script
Save the following as BezierBounds.swift. The code evaluates curve points through successive linear interpolation and uses hand-calculated results as independent assertions. Running the same implementation twice does not count as validation.
struct Point {
let x: Double
let y: Double
}
struct Bounds {
let minX: Double
let minY: Double
let maxX: Double
let maxY: Double
}
func mix(_ a: Double, _ b: Double, _ t: Double) -> Double {
(1 - t) * a + t * b
}
func point(_ a: Point, _ b: Point, _ c: Point, _ t: Double) -> Point {
Point(
x: mix(mix(a.x, b.x, t), mix(b.x, c.x, t), t),
y: mix(mix(a.y, b.y, t), mix(b.y, c.y, t), t)
)
}
func bounds(_ a: Point, _ b: Point, _ c: Point) -> Bounds {
let values = [a.x, a.y, b.x, b.y, c.x, c.y]
precondition(values.allSatisfy {
$0.isFinite && abs($0) <= 1_000_000
})
var times = [0.0, 1.0]
for (p0, p1, p2) in [(a.x, b.x, c.x), (a.y, b.y, c.y)] {
let denominator = p0 - 2 * p1 + p2
if denominator != 0 {
let t = (p0 - p1) / denominator
if t > 0 && t < 1 {
times.append(t)
}
}
}
let points = times.map { point(a, b, c, $0) }
return Bounds(
minX: points.map(\.x).min()!,
minY: points.map(\.y).min()!,
maxX: points.map(\.x).max()!,
maxY: points.map(\.y).max()!
)
}
func check(_ actual: Bounds, _ expected: Bounds) {
let pairs = [
(actual.minX, expected.minX),
(actual.minY, expected.minY),
(actual.maxX, expected.maxX),
(actual.maxY, expected.maxY)
]
for (a, e) in pairs {
let tolerance = 1e-10 * max(1.0, abs(a), abs(e))
precondition(abs(a - e) <= tolerance)
}
}
let a = Point(x: 0, y: 0)
let b = Point(x: 10, y: 20)
let c = Point(x: 20, y: 0)
let arch = Bounds(minX: 0, minY: 0, maxX: 20, maxY: 10)
check(bounds(a, b, c), arch)
check(bounds(c, b, a), arch)
let same = Point(x: 3, y: -4)
check(
bounds(same, same, same),
Bounds(minX: 3, minY: -4, maxX: 3, maxY: -4)
)
check(
bounds(a, Point(x: 10, y: 10), Point(x: 20, y: 20)),
Bounds(minX: 0, minY: 0, maxX: 20, maxY: 20)
)
check(
bounds(a, Point(x: 20, y: 0), Point(x: 10, y: 0)),
Bounds(minX: 0, minY: 0, maxX: 40.0 / 3.0, maxY: 0)
)
On a node with the development tools configured, run xcrun swift BezierBounds.swift. A failed assertion returns a nonzero exit status. The example prints nothing on success, so use the exit status to determine whether the job passed. When integrating this into your application, extract the production function into a module and have the tests call it directly. This standalone script is only an initial check of the method.
Add counterexamples
The final test case is particularly important: three collinear points do not mean the curve stays between its two endpoints. When the control point extends past the endpoint, the curve can move forward and then double back. “Use the endpoints if the points are collinear” is therefore another invalid optimization.
Add the following inputs, keeping explicit expected values for every case:
- Swap the horizontal and vertical coordinates: verify that the implementation handles more than vertical bulges.
- Negate all coordinates: the minimum and maximum values should reverse accordingly.
- Apply a uniform translation: each of the four bounds should increase by the corresponding translation offset.
- Scale by a positive factor: the bounds should scale proportionally; check the tolerance as well.
- Place an extremum exactly at an endpoint: duplicate candidates must not cause errors.
Uniform sampling can help reveal obvious excursions outside the bounds, but it cannot prove that the bounding box is tight. No matter how many samples you take, an extremum can fall between two of them. Production acceptance checks still need analytic candidates and hand-calculated test fixtures.
Integrate with the pipeline
Run the geometry checks before expensive build steps. On failure, retain the input coordinates, expected bounds, actual bounds, and toolchain version. Do not save only a screenshot that “looks clipped”; that makes it difficult to distinguish a mathematical error from a rendering discrepancy.
The example tolerance is suitable for engineering regression tests within the current coordinate budget. It is not a rigorous proof of numerical error bounds. Extreme scales or severe cancellation require additional high-precision reference results. If clipping must never cut into the content, design a separate strategy for expanding the bounds outward.
Finally, validate the stroke. If the curve is rotated or skewed first, transform all three control points and recalculate the bounds rather than treating the old rectangle as the new tight bounding box. Calculate the stroked path’s extent separately using the final rendering parameters, then run pixel checks. That way, each failure record can identify whether the problem lies in geometry, transforms, or rendering.
Frequently asked questions
Can I use the bounding box of all three control points?
It is conservative but usually not tight. For clipping and sizing, include roots of both coordinate derivatives inside the zero-to-one interval and evaluate the curve there.
Can a stroke still clip when the path bounds are correct?
Yes. Path bounds cover the centerline only. Width, caps, joins and transforms affect the visible stroke, so test the stroked path separately.
Add a dedicated Mac to your build queue
An entire physical Mac dedicated to your tasks. Choose a model, billing period, and node to set up your team’s own macOS runner.
Rent now