4

I'd like to calculate the AABB (axis aligned bounding box) for a quadratic or bezier curve.

The only way I know how to do this is evaluating a high number of points on the bezier curve, and then use those points to calculate the AABB.

Is there a better way?

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
Spoonman
  • 101
  • 2

3 Answers3

2

Great resource on Bezier curves, and working example of AABB http://pomax.github.io/bezierinfo/#boundingbox For quadratic curve if you need it, also calculate this using derivatives.

Always avoid iterative methods when closed form is available.

Evil
  • 460
  • 1
  • 11
  • 25
1

It should be possible by looking for minimum and maximum thanks to the derivative of the curve in parametric form. have a look at this article: http://nishiohirokazu.blogspot.jp/2009/06/how-to-calculate-bezier-curves-bounding.html

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
0

The quadratic bezier curve consists of 2 coordinate functions — x(t) and y(t) where.

These functions may have maximum or minimum (the points where x'(t) = 0 and y'(t) = 0) and these points are the boundary points of the aabb.

So the algorithm is:

  1. Imagine x0, y0, x1, y1, x2, y2 are known and calculate the values t(x0, x1, x2) and t(y0, y1, y2) when x'(t) = 0 and y'(t) = 0 respectively.
  2. Calculate both values and check whether they are >= 0 and <= 1. If they are evaluate the points of the quadratic bezier.
  3. Take the first and the last points.
  4. Now you have 4 points (or maybe less), use them to calculate AABB.

By the way:

t(x0, x1, x2) = (x0 - x1) / (x2 - 2 * x1 + x0)

t(y0, y1, y2) = (y0 - y1) / (y2 - 2 * y1 + y0)

You can find the full code here: https://github.com/keyten/Graphics2D/blob/Delta/Core/Curve.Math.js#L295

Keyten
  • 9
  • 2