Finding min/max of quadratic bezier with CoreGraphics

百般思念 提交于 2019-12-08 17:39:58

问题


I am using CoreGraphics to draw a quadratic bezier but want to computer the min/max value of the curve. I am not from a mathematical background so this has become a bit troublesome. Does anyone have any articles or ideas about how to solve this?


回答1:


For a quadratic Bezier, this is actually quite simple.

Define your three control points as P0 = (x0,y0), P1 = (x1,y1) and P2 = (x2,y2). To find the extrema in x, solve this equation:

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

If 0 <= t <= 1, then evaluate your curve at t and store the location as Px. Do the same thing for y:

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

Again, if 0 <= t <= 1, evaluate your curve at t and store the location as Py. Finally, find the axis-aligned bounding box containing P0, P2, Px (if found) and Py (if found). This bounding box will also tightly bound your 2D quadratic Bezier curve.




回答2:


Calculus gives the standard box of tricks for finding the min/max of continuous, differentiable curves.

Here is a sample discussion:

http://newsgroups.derkeiler.com/Archive/Comp/comp.graphics.algorithms/2005-07/msg00334.html




回答3:


I have made a representation of this in javascript:

Jsfiddle link

function P(x,y){this.x = x;this.y = y; }
function pointOnCurve(P1,P2,P3,t){
    if(t<=0 || 1<=t || isNaN(t))return false;
    var c1 =  new P(P1.x+(P2.x-P1.x)*t,P1.y+(P2.y-P1.y)*t);
    var c2 =  new P(P2.x+(P3.x-P2.x)*t,P2.y+(P3.y-P2.y)*t);
    return new P(c1.x+(c2.x-c1.x)*t,c1.y+(c2.y-c1.y)*t);  
}
function getQCurveBounds(ax, ay, bx, by, cx, cy){
    var  P1 = new P(ax,ay);
    var  P2 = new P(bx,by);
    var  P3 = new P(cx,cy);
    var tx =  (P1.x - P2.x) / (P1.x - 2*P2.x + P3.x);
    var ty =  (P1.y - P2.y) / (P1.y - 2*P2.y + P3.y);
    var Ex = pointOnCurve(P1,P2,P3,tx);
    var xMin = Ex?Math.min(P1.x,P3.x,Ex.x):Math.min(P1.x,P3.x);
    var xMax = Ex?Math.max(P1.x,P3.x,Ex.x):Math.max(P1.x,P3.x);
    var Ey = pointOnCurve(P1,P2,P3,ty);
    var yMin = Ey?Math.min(P1.y,P3.y,Ey.y):Math.min(P1.y,P3.y);
    var yMax = Ey?Math.max(P1.y,P3.y,Ey.y):Math.max(P1.y,P3.y);
    return {x:xMin, y:yMin, width:xMax-xMin, height:yMax-yMin};
}


来源:https://stackoverflow.com/questions/999549/finding-min-max-of-quadratic-bezier-with-coregraphics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!