Conic Fit

<- Numerical Methods

Source inspiration: (Mathew 2000-2019).

Determinant-Based Conic Fitting

Given data points \((x_i,y_i)\) in the plane, we fit an implicit curve

\[ F(x,y)=0 \]

so every data point satisfies the equation exactly.

NoteDefinition - General Conic

A planar conic can be written as \[ Ax^2+Bxy+Cy^2+Dx+Ey+F=0, \tag{1} \] with \(A\), \(B\), \(C\) not all zero.

TipTheorem - Uniqueness Through Five Points

Through any five distinct points passes at least one conic. It is unique (up to a nonzero scale factor) exactly when no four of the points are collinear, which is when the \(5\times 6\) fitting system has rank 5. If in addition no three are collinear, the conic is nondegenerate.

Core Determinant Formulas

For a symbolic point \((x,y)\) and data points \((x_i,y_i)\), the legacy module uses determinants so that replacing the first row by a point row forces that point onto the curve.

ImportantKey Implicit Forms

Line through two points: \[ \det\begin{pmatrix} x & y & 1\\ x_1 & y_1 & 1\\ x_2 & y_2 & 1 \end{pmatrix}=0. \tag{2} \]

Circle through three points: \[ \det\begin{pmatrix} x^2+y^2 & x & y & 1\\ x_1^2+y_1^2 & x_1 & y_1 & 1\\ x_2^2+y_2^2 & x_2 & y_2 & 1\\ x_3^2+y_3^2 & x_3 & y_3 & 1 \end{pmatrix}=0. \tag{3} \]

Standard parabola model \(y=ax^2+bx+c\): \[ \det\begin{pmatrix} x^2 & x & 1 & y\\ x_1^2 & x_1 & 1 & y_1\\ x_2^2 & x_2 & 1 & y_2\\ x_3^2 & x_3 & 1 & y_3 \end{pmatrix}=0. \tag{4} \]

Alternate parabola model \(x=ay^2+by+c\): \[ \det\begin{pmatrix} y^2 & y & 1 & x\\ y_1^2 & y_1 & 1 & x_1\\ y_2^2 & y_2 & 1 & x_2\\ y_3^2 & y_3 & 1 & x_3 \end{pmatrix}=0. \tag{5} \]

Standard ellipse model \(Ax^2+Cy^2+Dx+Ey+F=0\): \[ \det\begin{pmatrix} x^2 & y^2 & x & y & 1\\ x_1^2 & y_1^2 & x_1 & y_1 & 1\\ x_2^2 & y_2^2 & x_2 & y_2 & 1\\ x_3^2 & y_3^2 & x_3 & y_3 & 1\\ x_4^2 & y_4^2 & x_4 & y_4 & 1 \end{pmatrix}=0. \tag{6} \]

General five-point conic: \[ \det\begin{pmatrix} x^2 & xy & y^2 & x & y & 1\\ x_1^2 & x_1y_1 & y_1^2 & x_1 & y_1 & 1\\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\ x_5^2 & x_5y_5 & y_5^2 & x_5 & y_5 & 1 \end{pmatrix}=0. \tag{7} \]

Example 1 - Line Through Two Points

Find the line through \((1,4)\) and \((5,3)\).

Step 1: Use the two-point form.

\[ \frac{y-4}{x-1}=\frac{3-4}{5-1}=-\frac14. \]

Step 2: Rearrange to implicit form.

\[ y=-\frac14x+\frac{17}{4} \quad\Longrightarrow\quad x+4y-17=0. \]

The fitted line is

\[ \boxed{x+4y-17=0}. \]

Verification:

Point Left-hand side \(x+4y-17\) Result
\((1,4)\) \(1+16-17=0\) \(0=0\;\checkmark\)
\((5,3)\) \(5+12-17=0\) \(0=0\;\checkmark\)

Expanding the determinant (2) with these two points gives the same line, \(x+4y-17=0\).

Vertical and horizontal lines (legacy Examples 2 and 3)

The two-point slope form fails for the line through \((3,1)\) and \((3,4)\), because its slope \(\tfrac{4-1}{3-3}\) divides by zero. The determinant (2) has no division.

Step 1: Expand (2) along the first row for \((3,1)\) and \((3,4)\).

\[ \det\begin{pmatrix} x & y & 1\\ 3 & 1 & 1\\ 3 & 4 & 1 \end{pmatrix} =x(1-4)-y(3-3)+(12-3)=-3x+9=0 \quad\Longrightarrow\quad \boxed{x=3}. \]

Step 2: Do the same for the horizontal line through \((1,4)\) and \((5,4)\).

\[ \det\begin{pmatrix} x & y & 1\\ 1 & 4 & 1\\ 5 & 4 & 1 \end{pmatrix} =x(4-4)-y(1-5)+(4-20)=4y-16=0 \quad\Longrightarrow\quad \boxed{y=4}. \]

Verification: \(-3x+9\) is \(0\) at \((3,1)\) and at \((3,4)\) (both have \(x=3\)), and \(4y-16\) is \(0\) at \((1,4)\) and at \((5,4)\) (both have \(y=4\)).

Example 2 - Circle Through Three Points

Find the circle through \((6,1)\), \((2,2)\), and \((1,4)\).

Assume

\[ x^2+y^2+Dx+Ey+F=0. \]

Step 1: Substitute each point.

\[ \begin{aligned} 37+6D+E+F&=0,\\ 8+2D+2E+F&=0,\\ 17+D+4E+F&=0. \end{aligned} \]

Step 2: Solve for \((D,E,F)\).

\[ D=-\frac{67}{7}, \quad E=-\frac{65}{7}, \quad F=\frac{208}{7}. \]

Hence

\[ x^2+y^2-\frac{67}{7}x-\frac{65}{7}y+\frac{208}{7}=0, \]

or equivalently

\[ \boxed{7x^2+7y^2-67x-65y+208=0}. \]

Verification:

Point Left-hand side \(7x^2+7y^2-67x-65y+208\) Result
\((6,1)\) \(252+7-402-65+208=0\) \(0=0\;\checkmark\)
\((2,2)\) \(28+28-134-130+208=0\) \(0=0\;\checkmark\)
\((1,4)\) \(7+112-67-260+208=0\) \(0=0\;\checkmark\)

Example 3 - Same Three Points, Two Parabola Models

Use \((6,1)\), \((2,2)\), and \((1,4)\).

(a) Standard form \(y=ax^2+bx+c\)

Step 1: Build the linear system.

\[ \begin{aligned} 36a+6b+c&=1,\\ 4a+2b+c&=2,\\ a+b+c&=4. \end{aligned} \]

Step 2: Solve.

\[ a=\frac{7}{20}, \quad b=-\frac{61}{20}, \quad c=\frac{67}{10}. \]

So

\[ \boxed{7x^2-61x-20y+134=0}. \]

(b) Alternate form \(x=ay^2+by+c\)

Step 1: Build the linear system.

\[ \begin{aligned} a+b+c&=6,\\ 4a+2b+c&=2,\\ 16a+4b+c&=1. \end{aligned} \]

Step 2: Solve.

\[ a=\frac{7}{6}, \quad b=-\frac{15}{2}, \quad c=\frac{37}{3}. \]

So

\[ \boxed{7y^2-45y-6x+74=0}. \]

Verification:

Point \(7x^2-61x-20y+134\) \(7y^2-45y-6x+74\)
\((6,1)\) \(252-366-20+134=0\) \(7-45-36+74=0\)
\((2,2)\) \(28-122-40+134=0\) \(28-90-12+74=0\)
\((1,4)\) \(7-61-80+134=0\) \(112-180-6+74=0\)

Example 4 - Standard Ellipse Model Through Four Points

Fit the model

\[ Ax^2+Cy^2+Dx+Ey+F=0 \]

through \((6,1)\), \((2,2)\), \((1,4)\), \((9,2)\).

Step 1: Substitute the four points.

\[ \begin{aligned} 36A+C+6D+E+F&=0,\\ 4A+4C+2D+2E+F&=0,\\ A+16C+D+4E+F&=0,\\ 81A+4C+9D+2E+F&=0. \end{aligned} \]

Because conics are scale-invariant, set \(A=3\) and solve for the remaining coefficients.

Step 2: Solve.

\[ C=8, \quad D=-33, \quad E=-60, \quad F=142. \]

Hence

\[ \boxed{3x^2+8y^2-33x-60y+142=0}. \]

Verification:

Point Left-hand side \(3x^2+8y^2-33x-60y+142\) Result
\((6,1)\) \(108+8-198-60+142=0\) \(0=0\;\checkmark\)
\((2,2)\) \(12+32-66-120+142=0\) \(0=0\;\checkmark\)
\((1,4)\) \(3+128-33-240+142=0\) \(0=0\;\checkmark\)
\((9,2)\) \(243+32-297-120+142=0\) \(0=0\;\checkmark\)

Example 5 - Legacy Five-Point Conic (Example 8)

Legacy data points:

\[ \left(\tfrac72,\tfrac52\right), \ (2,2), \ \left(\tfrac52,\tfrac72\right), \ (7,5), \ \left(\tfrac{19}{2},7\right). \]

Assume the general form

\[ Ax^2+Bxy+Cy^2+Dx+Ey+F=0. \]

Step 1: Substitute the five points to get a homogeneous linear system for \(A,B,C,D,E,F\).

Step 2: Solve up to scale.

\[ (A,B,C,D,E,F)=(2,-4,2,-1,-1,4). \]

Therefore the fitted conic is

\[ \boxed{2x^2-4xy+2y^2-x-y+4=0}. \]

Here \(\Delta = B^2 - 4AC = 16 - 16 = 0\) and the determinant (9) equals \(-2 \ne 0\), so the conic is a parabola.

Verification:

Point Left-hand side \(2x^2-4xy+2y^2-x-y+4\) Result
\(\left(\tfrac72,\tfrac52\right)\) \(0\) \(0=0\;\checkmark\)
\((2,2)\) \(0\) \(0=0\;\checkmark\)
\(\left(\tfrac52,\tfrac72\right)\) \(0\) \(0=0\;\checkmark\)
\((7,5)\) \(0\) \(0=0\;\checkmark\)
\(\left(\tfrac{19}{2},7\right)\) \(0\) \(0=0\;\checkmark\)

Classification by Discriminant

For

\[ Ax^2+Bxy+Cy^2+Dx+Ey+F=0, \]

the discriminant is

\[ \Delta=B^2-4AC. \tag{8} \]

ImportantQuick Classification Rule

Call the conic nondegenerate when \[ \det\begin{pmatrix} A & B/2 & D/2\\ B/2 & C & E/2\\ D/2 & E/2 & F \end{pmatrix}\ne 0 \tag{9} \] and the curve has real points. For a nondegenerate conic:

  • If \(\Delta<0\): ellipse (or circle as a special case).
  • If \(\Delta=0\): parabola.
  • If \(\Delta>0\): hyperbola.

When the determinant (9) is zero or the curve has no real points, the conic is degenerate: a pair of intersecting lines (\(\Delta>0\)); two parallel lines, one double line, or no real points (\(\Delta=0\)); a single point or no real points (\(\Delta<0\)).

Additional Legacy Five-Point Results

The remaining legacy five-point examples follow exactly the same workflow as Example 5.

Legacy example Point set Conic equation Type by \(\Delta\)
9 \(\left(\tfrac{14}{5},\tfrac{22}{5}\right),(2,2),\left(\tfrac{22}{5},\tfrac{14}{5}\right),(7,5),(10,10)\) \(17x^2-30xy+17y^2-24x-24y+80=0\) Ellipse (\(\Delta=-256\))
10 \(\left(\tfrac74,\tfrac{13}{4}\right),(2,2),\left(\tfrac{31}{12},\tfrac74\right),\left(\tfrac{13}{4},\tfrac74\right),\left(\tfrac{38}{5},\tfrac{14}{5}\right)\) \(3x^2-10xy+3y^2+16=0\) Hyperbola (\(\Delta=64\))
11 \((1,1),(3,5),(5,4),(7,3),(9,7)\) \(3x^2+2xy-8y^2-38x+54y-13=0\) Degenerate: intersecting lines \((x+2y-13)(3x-4y+1)=0\) (\(\Delta=100\))
12 \((1,8),(2,1),(8,9),(9,2),(10,5)\) \(x^2+y^2-10x-10y+25=0\) Ellipse (\(\Delta=-4\))

Each equation above was checked by substitution at all five listed points. For legacy Examples 9, 10 and 12 the determinant (9) is nonzero (\(-4096\), \(-256\) and \(-25\)) and the curves pass through the listed real points, so their types follow from \(\Delta\). In legacy Example 11 it is zero: the fitting system still has rank 5, so the conic is unique, but \((3,5),(5,4),(7,3)\) are collinear, as are \((1,1),(5,4),(9,7)\), so the conic must contain both lines, which cross at \((5,4)\).

References

Mathew, John H. 2000-2019. Numerical Analysis - Numerical Methods Modules. https://web.archive.org/web/20190808102217/http://mathfaculty.fullerton.edu/mathews/n2003/NumericalUndergradMod.html.