An algorithm to find bounding box of closed bezier curves?

后端 未结 7 869
半阙折子戏
半阙折子戏 2020-12-05 13:54

I\'m looking for an algorithm to find bounding box (max/min points) of a closed quadratic bezier curve in Cartesian axis:

input: C (a closed bezier curve)
out         


        
7条回答
  •  隐瞒了意图╮
    2020-12-05 14:27

    Use De Casteljau algorithm to approximate the curve of higher orders. Here is how it works for cubic curve http://jsfiddle.net/4VCVX/25/

    function getCurveBounds(ax, ay, bx, by, cx, cy, dx, dy)
    {
            var px, py, qx, qy, rx, ry, sx, sy, tx, ty,
                tobx, toby, tocx, tocy, todx, tody, toqx, toqy, 
                torx, tory, totx, toty;
            var x, y, minx, miny, maxx, maxy;
    
            minx = miny = Number.POSITIVE_INFINITY;
            maxx = maxy = Number.NEGATIVE_INFINITY;
    
            tobx = bx - ax;  toby = by - ay;  // directions
            tocx = cx - bx;  tocy = cy - by;
            todx = dx - cx;  tody = dy - cy;
            var step = 1/40;      // precision
            for(var d=0; d<1.001; d+=step)
            {
                px = ax +d*tobx;  py = ay +d*toby;
                qx = bx +d*tocx;  qy = by +d*tocy;
                rx = cx +d*todx;  ry = cy +d*tody;
                toqx = qx - px;      toqy = qy - py;
                torx = rx - qx;      tory = ry - qy;
    
                sx = px +d*toqx;  sy = py +d*toqy;
                tx = qx +d*torx;  ty = qy +d*tory;
                totx = tx - sx;   toty = ty - sy;
    
                x = sx + d*totx;  y = sy + d*toty;                
                minx = Math.min(minx, x); miny = Math.min(miny, y);
                maxx = Math.max(maxx, x); maxy = Math.max(maxy, y);
            }        
            return {x:minx, y:miny, width:maxx-minx, height:maxy-miny};
    }
    

提交回复
热议问题