Search code examples
math3dcurvebezier

Calculating the bounding box of cubic bezier curve


I am trying to find an algorithm to calculate the bounding box of a given cubic bezier curve. The curve is in 3D space.

Is there a mathematic way to do this except of sampling points on the curve and calculating the bounding box of these points?


Solution

  • Most of this is addressed in An algorithm to find bounding box of closed bezier curves? except here we have cubic Beziers and there they were dealing with quadratic Bezier curves.

    Essentially you need to take the derivatives of each of the coordinate functions. If the x-coord is given by

    x = A (1-t)^3 +3 B t (1-t)^2 + 3 C t^2 (1-t) + D t^3
    

    differentiating with respect to t.

    dx/dt =  3 (B - A) (1-t)^2 + 6 (C - B) (1-t) t + 3 (D - C) t^2
          =  [3 (D - C) - 6 (C - B) + 3 (B - A)] t^2
           + [ -6 (B - A) - 6 (C - B)] t
           + 3 (B - A) 
          =  (3 D - 9 C + 9 B - 3 A) t^2 + (6 A - 12 B + 6 C) t + 3 (B - A)
    

    this is a quadratic which we can write at a t^2 + b t + c. We want to solve dx/dt = 0 which you can do using the quadratic formula

    - b +/- sqrt(b^2-4 a c)
    -----------------------
            2 a
    

    Solving this will either gives two solutions t0, t1 say, no solutions or in rare case just one solution. We are only interest in solutions with 0 <= t <= 1. You will have a maximum of four candidate points, the two end points and the two solutions. Its a simple matter to find which of these give the extreme points.

    You can repeat the same process for each coordinate and then get the bounding box.

    I've put this for the 2D case in a js fiddle http://jsfiddle.net/SalixAlba/QQnvm/4/