Skip to content
scipython.com

P6.5.6Conic sections

The implicit formula for a conic section may be written as the second-degree polynomial,

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

or in matrix form using the homogeneous coordinate vector,:

\[\mathbf{x} = \begin{pmatrix}x \\ y \\ 1\end{pmatrix},\]

as \(\mathbf{x}^T\mathbf{Q}\mathbf{x} = 0\), where

\[\mathbf{Q} = \begin{pmatrix} A & B/2 & D/2 \\ B/2 & C & E/2\\ D/2 & E/2 & F \end{pmatrix}.\]

Conic sections may be classified according to the following properties of \(\mathbf{Q}\), where the submatrix \(\mathbf{Q}_{33}\) is

\[\mathbf{Q}_{33} = \begin{pmatrix}A & B/2\\B/2 & C\end{pmatrix}.\]
  • If \(\mathrm{det} \mathbf{Q} = 0\), the conic is degenerate in one of the following forms:
    • if \(\mathrm{det} \mathbf{Q}_{33} < 0\), the equation represents two intersecting lines,
    • if \(\mathrm{det} \mathbf{Q}_{33} = 0\), the equation represents two parallel lines,
    • if \(\mathrm{det} \mathbf{Q}_{33} > 0\), the equation represents a single point.
  • If \(\mathrm{det} \mathbf{Q} \ne 0\):
    • if \(\mathrm{det} \mathbf{Q_{33}} < 0\) the conic is a hyperbola,
    • if \(\mathrm{det} \mathbf{Q_{33}} = 0\) the conic is a parabola,
    • if \(\mathrm{det} \mathbf{Q_{33}} > 0\), the conic is an ellipse:
      • if \(A=C\) and \(B=0\), the ellipse is a circle.

Write a program to classify the conic section represented by the 6 coefficients \(A, B, C, D, E\) and \(F\).

Some test-cases (coefficients not given are zero):

  • Hyperbola: \(B = 1, F = -9\).
  • Parabola: \(A = \frac{1}{2}, D = 2, E = -\frac{1}{2}\).
  • Circle: \(A = \frac{1}{2}, C = \frac{1}{2}, D = -2, E = -3, F = 2\).
  • Ellipse: \(A = 9, C = 4, F = -36\).
  • Two parallel lines: \(A = 1, F = -1\).
  • A single point: \(A = 1, C = 1\).
Show solution

The code below classifies conic sections, given the coefficients \(A, B, C, D, E\) and \(F\) on the command line.

conicsections
import sys
import numpy as np


def classify_conic(A, B, C, D, E, F):
    Q = np.array(((A, B / 2, D / 2), (B / 2, C, E / 2), (D / 2, E / 2, F)))
    Q = np.matrix(Q)
    Q33 = Q[:2, :2]

    detQ = np.linalg.det(Q)
    detQ33 = np.linalg.det(Q33)
    if detQ == 0:
        if detQ33 < 0:
            return "two intersecting lines"
        if detQ33 == 0:
            return "two parallel lines"
        return "a single point"

    if detQ33 < 0:
        return "a hyperbola"
    if detQ33 == 0:
        return "a parabola"
    if A == C and B == 0:
        return "a circle"
    return "an ellipse"


if __name__ == "__main__":
    A, B, C, D, E, F = [float(arg) for arg in sys.argv[1:]]
    print(classify_conic(A, B, C, D, E, F))

For example,

$ python classify.py 9 0 4 0 0 -36
an ellipse