开发者

General formula to generate a cubic bezier elliptical arc?

How开发者_开发百科 could I implement in C a simple way to generate the 2 missing control points for an elliptic arc given a start and end point? I don't need fancy error estimation, just something that can take points A and D and generate the control points B and C for the elliptical arc where I can then use the cubic bezier interpolation algorithm to generate the curve.

something like

void GetArcControlPoints(Point a, Point &b, Point &c, Point d)
{

.....
b = ...
c = ....
}

Thanks


There are some flaws in the math behind your question:

  1. Bézier curves are polynomial functions of a parameter t in the unit interval [0;1]. Ellipses are defined using the trigonometrical functions, which are transcendental, thus, not algebraic, thus, not polynomial. You cannot generate an ellipse arc using Bézier curves (neither cubic nor of any degree n). But let's suppose you just want to approximate an ellipse arc. Since you have not specified how good the approximation has to be, there is no way to ensure the Bézier curve will be "ellipse enough" for you. In fewer terms: You need an error parameter.

  2. There are infinite ellipse arcs passing through two given points. Thus, the two points give not enough information to specify an ellipse, an arc of which which could then be approximated using a Bézier curve. To make things worse, since you want an ellipse arc, not the whole ellipse, you also have to specify how much of the ellipse has to be "covered" by the arc, in percentage (100% = the whole ellipse), radians (2*pi = the whole ellipse), whatever. In fewer terms: You need even more (input) parameters, just to specify a single arc of a single ellipse.

Until the math is done right, you cannot go to the next step (coding).


EDIT:

  1. Since you need a whole ellipse, I would recommend using two or four Bézier patches instead of a single Bézier curve.

  2. You can think of the ellipse as a circle that was "stretched" on one of the dimensions. Now, since "stretch" transforms are linear and Bézier functions are linear on the control points, you can calculate the control points for a Bézier curve approximating a 90 degree circle arc, then apply the "stretch" transform to the control points, and voilà, you get the control points for a Bézier curve approximating a "90 degree" ellipse arc. Getting the whole ellipse is just repeating the procedure four times.


Here is a derivation for the control points of a unit circle segment:

http://www.whizkidtech.redprince.net/bezier/circle/kappa/

Every possible ellipse can be generated by applying the appropriate affine transformation.


The kappa formula is just an approximation. This one is a good approximation for drawing a circle or ellipse with 4 cubic Bezier: it matches exactly on 8 points (those on the two axis and on the two main diagonals, the only points considered in the kappa formula), but there are differences everywhere else.

However, even if we can't use such splines to draw circles or ellipses with Beziers, there does exist an "exact" approximation (up to 1 half-pixel of difference) using only arithmetic operations to draw a circle incrementally pixel by pixel, based on the Bresenham algorithm (such approximation is possible because we can compute exactly, using only additions, the square distance of points to the center of the circle or the sum of distances to the two focal points of an ellipse and check if a pixel is "inside" or "outside" of it, and because we know the exact position of a starting point on the circle or ellipse, so we can just test which one of the two possible neighbours, in any one of the 8 sectors, are best approximating the circle or ellipse).

Such incremental algorithm approximating of elliptic arcs is completely different from those used to for Bezier splines but it is very efficient too (and in fact even faster than when drawing arbitrary Beziers splines).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜