- #1
abeall
- 21
- 0
I'm programming some commands in a graphic application, and once again my extremely limited mathematical knowledge has found me rather stumped.
Here is my cubic bezier function, written in JavaScript syntax(this is not actually JavaScript, though):
function getBezier(percent,p1,cp1,cp2,p2) {
function b1(t) { return t*t*t }
function b2(t) { return 3*t*t*(1-t) }
function b3(t) { return 3*t*(1-t)*(1-t) }
function b4(t) { return (1-t)*(1-t)*(1-t) }
var pos = {x:0,y:0};
pos.x = p1.x*b1(percent) + cp1.x*b2(percent) + cp2.x*b3(percent) + p2.x*b4(percent);
pos.y = p1.y*b1(percent) + cp1.y*b2(percent) + cp2.y*b3(percent) + p2.y*b4(percent);
return pos;
}
You give it a cubic bezier curve defined by four points (two "anchors" which intersect the curve, and two "control points" which affect the curvature), and a time value(written as "percent" above) from 0 to 1, and it returns a point(x,y) at that time.
I'm now in a situation where I need just the opposite. I have a point which I know is on the bezier curve, and I need to find the time value.
Cubic bezier curves can self intersect, in which case I suppose there's a rare possibility that a point actually has two valid time values, but I'm not worried about that, I don't think it can occur in my environment.
After being completely befuddled by the formula for cubic beziers presented on wikipedia (http://upload.wikimedia.org/math/e/2/9/e29b31672536b36c91cb7be0ce8c2ff7.png) I'm left unable to solve for t. Any help in that direction, if it's even possible, would be appreciated.
Thanks!
- aaron
Here is my cubic bezier function, written in JavaScript syntax(this is not actually JavaScript, though):
function getBezier(percent,p1,cp1,cp2,p2) {
function b1(t) { return t*t*t }
function b2(t) { return 3*t*t*(1-t) }
function b3(t) { return 3*t*(1-t)*(1-t) }
function b4(t) { return (1-t)*(1-t)*(1-t) }
var pos = {x:0,y:0};
pos.x = p1.x*b1(percent) + cp1.x*b2(percent) + cp2.x*b3(percent) + p2.x*b4(percent);
pos.y = p1.y*b1(percent) + cp1.y*b2(percent) + cp2.y*b3(percent) + p2.y*b4(percent);
return pos;
}
You give it a cubic bezier curve defined by four points (two "anchors" which intersect the curve, and two "control points" which affect the curvature), and a time value(written as "percent" above) from 0 to 1, and it returns a point(x,y) at that time.
I'm now in a situation where I need just the opposite. I have a point which I know is on the bezier curve, and I need to find the time value.
Cubic bezier curves can self intersect, in which case I suppose there's a rare possibility that a point actually has two valid time values, but I'm not worried about that, I don't think it can occur in my environment.
After being completely befuddled by the formula for cubic beziers presented on wikipedia (http://upload.wikimedia.org/math/e/2/9/e29b31672536b36c91cb7be0ce8c2ff7.png) I'm left unable to solve for t. Any help in that direction, if it's even possible, would be appreciated.
Thanks!
- aaron
Last edited: