Bezier curve through three points

社会主义新天地 提交于 2019-12-21 03:16:06

问题


I have read similar topics in order to find solution, but with no success. What I'm trying to do is make the tool same as can be found in CorelDraw, named "Pen Tool". I did it by connecting Bezier cubic curves, but still missing one feature, which is dragging curve (not control point) in order to edit its shape.

I can successfully determine the "t" parameter on the curve where dragging should begin, but don't know how to recalculate control points of that curve.

Here I want to higlight some things related to CorelDraw''s PenTool behaviour that may be used as constaints. I've noticed that when dragging curve strictly vertically, or horizontally, control points of that Bezier curve behave accordingly, i.e. they move on their verticals, or horizontals, respectively.

So, how can I recalculate positions of control points while curve dragging?


回答1:


Ive just look into Inkspace sources and found such code, may be it help you:

// Magic Bezier Drag Equations follow!
// "weight" describes how the influence of the drag should be distributed
// among the handles; 0 = front handle only, 1 = back handle only.
double weight, t = _t;
if (t <= 1.0 / 6.0) weight = 0;
else if (t <= 0.5) weight = (pow((6 * t - 1) / 2.0, 3)) / 2;
else if (t <= 5.0 / 6.0) weight = (1 - pow((6 * (1-t) - 1) / 2.0, 3)) / 2 + 0.5;
else weight = 1;

Geom::Point delta = new_pos - position();
Geom::Point offset0 = ((1-weight)/(3*t*(1-t)*(1-t))) * delta;
Geom::Point offset1 = (weight/(3*t*t*(1-t))) * delta;

first->front()->move(first->front()->position() + offset0);
second->back()->move(second->back()->position() + offset1);

In you case "first->front()" and "second->back()" would mean two control points




回答2:


The bezier curve is nothing more then two polynomials: X(t), Y(t).

The cubic one:

x = ax*t^3 + bx*t^2 + cx*t + dx
                               0 <= t <= 1
y = ay*t^3 + by*t^2 + cy*t + dy

So if you have a curve - you have the poly coefficients. If you move your point and you know it's t parameter - then you can simply recalculate the poly's coefficients - it will be a system of 6 linear equations for coefficients (for each of the point). The system is subdivided per two systems (x and y) and can be solved exactly or using some numerical methods - they are not hard too.

So your task now is to calculate control points of your curve when you know the explicit equation of your curve.

It can be also brought to the linear system. I don't know how to do it for generalized Bezier curve, but it is not hard for cubic or quadric curves.

The cubic curve via control points:

B(t) = (1-t)^3*P0 + 3(1-t)^2*t*P1 + 3(1-t)*t^2*P2 + t^3*P3

Everything you have to do is to produce the standard polynomial form (just open the brackets) and to equate the coefficients. That will provide the final system for control points!




回答3:


When you clicks on curve, you already know position of current control point. So you can calculate offset X and offset Y from that point to mouse position. In case of mouse move, you would be able to recalculate new control point with help of X/Y offsets.

Sorry for my english



来源:https://stackoverflow.com/questions/4838695/bezier-curve-through-three-points

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