∑≠∂†½√±θβλ Zone® ax² + bx + c xª¿™∞⅜
∑≠∂†½√±θβλ
Zone®
ax² + bx³y² + cxy + d
xª¿™∞⅜
-----------------------------------------------------------------------
OK, I was able to create a relatively simple example, all done with Excel. First, some preliminaries. The standard form of a quadratic Bezier is: B(t) = (1-t)2P0 + 2t(1-t)P1 + t2P2……….0 ≤ t ≤ 1
Expanding and collecting terms, the equation can be put in classic polynomial form: B(t) = at2 + bt + c, where:
a = P0 – 2 P1 + P2
b = 2P0 + 2 P1
c = P0
The above expressions really represent two equations, one for x and the other for y:
x(t) = axt2 + bxt + cx
y(t) = ayt2 + byt + cy
Calling the control points P0 = (p0x, p0y), P1 = (p1x, p1y), P2 = (p2x, p2y)
ax = p0x – 2 p1x + p2x
ay = p0y – 2 p1y + p2y
bx = 2p0x + 2 p1x
by = 2p0y + 2 p1y
cx = p0x
cy = p0y
Thus, we’ve established a relationship between the Bezier control points the polynomial coefficients.
Least Squares analysis involves fitting a desired function f to a set of points p = [p1, p2, ..pn] = [(x1, y1), (x2, y2), … (xn, yn)]. A standard means of determining ‘quality of fit’ is to sum the squares of the differences in vertical distance between the data points and the points generated by the function:
sum of squares = (f(x1) - y1)2 + (f(x2) - y2)2 … + (f(xn) – yn)2
The idea is to make the sum of squares as small as possible. The key is that the same value of x must be used for each point, so only vertical distance is being used). This is a challenge when using parametric curves because x is a function of the independent variable t. Thus, for each data point, the proper t needs to be determined such that the resulting x is identical to the x of the data point.
Here is a simple example. Suppose we wish to fit a quadratic Bezier to the following data points:
P = [p1, p2, p3, p4, p5]
P = [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x5, y5)]
P = [(0, 0), (0.2, 0.01), (0.7, 0.4), (0.9, 0.8), (1, 1)]
Here is the simple approach I used:
1. Make an initial guess at control point P1 (P0 = (0, 0) and P2 = (1, 1) are given). I used P1 = (0.7, 0) as an initial guess.
2. Calculate the polynomial coefficients:
ax = p0x – 2 p1x + p2x = 0 – 2(0.7) + 1 = -0.4
bx = 2p0x + 2 p1x = 2(0) + 2(0.7) = 1.4
cx = p0x = 0
3. Generate x, y points for t = 0 to 1 and plot the Bezier curve. Plot the original data points and see how close the fit is. (It is instructive to play around with various values for p1x and p1y and see how the shape of the curve changes in relation to the data points).
4. Calculate the sum of squares. Here’s the tricky part. To calculate (f-y)2, you need to find t that results in the same x as the data point. For example, x1 = axt2 + bxt + c. To find the t that produces x1, a quadratic equation needs to be solved (i.e.[ –b ± sqrt(b2-4ac)]/2a). This needs to be done for all interior points (the end points are already known). Thus, for the initial control points used:
t1 = 0...........==> x1 = 0, y1 = 0
t2 = 0.149..........==> x2 = 0.2, y2 = 0.022
t3 = 0.604..........==> x3 = 0.7, y3 = 0.365
t4 = 0.849……….==> x4 = 0.9, y4 = 0.72
t5 = 1……….==> x5 = 1, y5 = 1
Now we can calculate the sum of squares = (0 – 0)2 + (0.022 – 0.01)2 + (0.365 – 0.04)2 + (0.72 – 0.8)2 + (1 – 1)2 = 0.008
5. Try new values of P1 until the sum of squares is minimized. Each time that p1x is changed, three sets of quadratic equations must be solved to find t associated with each x. As an example, suppose we try P1 = (0.6, 0). Then:
ax = p0x – 2 p1x + p2x = 0 – 2(0.6) + 1 = -0.2
bx = 2p0x + 2 p1x = 2(0) + 2(0.6) = 1.2
cx = p0x = 0
t1 = 0...........==> x1 = 0, y1 = 0
t2 = 0.172..........==> x2 = 0.2, y2 = 0.029
t3 = 0.655..........==> x3 = 0.7, y3 = 0.429
t4 = 0.879……….==> x4 = 0.9, y4 = 0.772
t5 = 1……….==> x5 = 1, y5 = 1
sum of squares = (0 – 0)2 + (0.029 – 0.01)2 + (0.429 – 0.04)2 + (0.772 – 0.8)2 + (1 – 1)2 = 0.002
If Excel is used and the formulas are set up correctly, the quadratic equations will be automatically solved each time P1 is changed.
For this example, I just used the Excel built-in solver function to minimize the cell containing the sum of squares and allowing p1 (i.e. p1x and p1y) to be changed. The net result:
P1 = (0.543, -0.108)
(ax, ay) = (-0.087, 1.216)
(bx, by) = (1.087, -0.216)
(cx, cy) = (0, 0)
sum of squares = 0.001
It should be noted that forcing the end points of the Bezier curve to be coincident with the first and last data points is an artificial restriction that limits the quality of fit. If p0y and p2y are also allowed to be adjusted, a much better fit can be achieved: