How to calculate width, height and position of bezier curve -
i have bezier curve defined start point, end point , 2 control points (parameters of this: http://www.w3schools.com/tags/canvas_beziercurveto.asp). first, need calculate width , height of curve. if make rectangle around curve, width , height need. need start point (x,y of left top corner) of rectangle.
how can calculate ? thanks.
i found approximate solution in other topic (i don't remember one) here simple js function calculate it:
function getcurveboundary(ax, ay, bx, by, cx, cy, dx, dy) { var tobx = bx - ax; var toby = - ay; var tocx = cx - bx; var tocy = cy - by; var todx = dx - cx; var tody = dy - cy; var step = 1 / 40; // precission var d, px, py, qx, qy, rx, ry, tx, ty, sx, sy, x, y, i, minx, miny, maxx, maxy; function min(num1, num2) { if (num1 > num2) return num2; if (num1 < num2) return num1; return num1; } function max(num1, num2) { if (num1 > num2) return num1; if (num1 < num2) return num2; return num1; } (var = 0; < 41; i++) { d = * step; px = ax + d * tobx; py = ay + d * toby; qx = bx + d * tocx; qy = + 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; if (i == 0) { minx = x; miny = y; maxx = x; maxy = y; } else { minx = min(minx, x); miny = min(miny, y); maxx = max(maxx, x); maxy = max(maxy, y); } } return {x: math.round(minx), y: math.round(miny), width: math.round(maxx - minx), height: math.round(maxy - miny)}; }
Comments
Post a Comment