Given the equation of a non-parabolic conic section, output its characteristics.


Some info on conic sections:

for more info visit Wikipedia

From an equation of the form \$ax^2+bx+cy^2+dy+E=0\$, it is possible to derive the type of conic section using a combination of square completion and simple arithmetic.

For the example \$x^2+6x+y^2+8y+16=0\$, here is how you would go about it.

\$(x^2+6x+9) + (y^2+8y+16) - 9=0\$

\$ => (x+3)^2 + (y+4)^2 = 9\$

\$ => \frac{(x+3)^2}{3^2}+\frac{(y+4)^2}{3^2}=1\$ (standard form)

=> this is an ellipse. The horizontal radius and vertical radius are the denominators of the first and second fractions, respectively. The center can be derived by calculating the \$x\$ and \$y\$ values such that each fraction evaluates to zero, in this case \$(-3,-4)\$ is the center. The foci can also be calculated at a distance of \$\sqrt{a^2-b^2}\$ for ellipses and \$\sqrt{a^2+b^2}\$ for hyperbolas, where a and b are the horizontal/vertical radii, respectively. In this case only one focus exists, which is the center (the above section is actually a circle, a special case with eccentricity of 0. This can be calculated via either \$\frac{\sqrt{a^2+b^2}}{a}\$ for hyperbolas or \$\frac{\sqrt{a^2-b^2}}{a}\$ for ellipses.

To determine if a section is an ellipse or a hyperbola, you can take the discriminant of the equation, which is defined as \$b^2-ac\$. If the discriminant is greater than 0, the equation represents a hyperbola. If less than 0, an ellipse and if equal to 0 a parabola. (We are not handling parabolas, to simplify things.)

No degenerate conic sections will be given as input.


A non-parabolic (to simplify things) conic section given in the standard equation form. To simplify things further (because the main point is not to perform linear algebra magic) there will be no xy term. This is an example of a valid equation:

x^2+6x+y^2-8y+15=0 // string form
[1,6,1,-8,15]      // array form

These are not:

x^3+5x^2+7x+4=0 // because the degree of the equation is 2
x^2+5xy+y^2-4=0 // is a hyperbola, but there should be no `xy` term
x^2+3x+7=0      // because there should be `x` and `y` terms.

Note that the conic section can also be taken as an array as shown above. If so, please specify the order of the array; I am flexible when it comes to this format, As long as there are 5 elements with nonexistent terms represented by zero (like no y term in x^2+5x+y^2+14=0) and that the terms they represent are x^2 x y^2 y c where c is a constant. The equation will always be <expression> = 0.


Output should be the type of section, center, horizontal radius, vertical radius, foci and eccentricity (in whatever desired order). This can be output as a string or an array as long as it is clear. A valid output for x^2+6x+y^2+8y+16=0 (or its array equivalent) would be:

["ellipse", [-3, -4], 3, 3, [[-3, -4]], 0]


-3 -4
-3 -4

or similar.

(no need to output "circle" because it is a special case of the ellipse)

Another case [assumes equation form]: Floating point errors for eccentricity are fine, but here shown in mathematical notation.

Input: 9x^2-4y^2+72x+32y+44=0
-4 4
  • 1
    \$\begingroup\$ Sandboxed: codegolf.meta.stackexchange.com/a/22405/100690 \$\endgroup\$
    – user100690
    Jun 2, 2021 at 5:38
  • 4
    \$\begingroup\$ What are all possible type of sections? And what does center, horizontal radius, vertical radius, foci and eccentricity mean? \$\endgroup\$
    – tsh
    Jun 2, 2021 at 5:45
  • \$\begingroup\$ What is expected output for \$ x^2-y^2=0 \$? Two... Lines? \$\endgroup\$
    – tsh
    Jun 2, 2021 at 6:18
  • \$\begingroup\$ @tsh for your first comment, it said that "the spec assumes prior knowledge of conic sections". The only sections considered here are the hyperbola and the ellipse (and the circle, but that's a special case of the ellipse). The rest are characteristics of a conic section. For your second comment, x^2-y^2=0 is a degenerate conic section, so that case need not be considered. I will edit in information about conics. \$\endgroup\$
    – user100690
    Jun 2, 2021 at 7:45
  • 1
    \$\begingroup\$ Prior knowledge shouldn't be simply assumed. A challenge should be self-contained as much as possible. It is recommended to include at least a brief description of each terminology, and it is also good to include some external links (e.g. Wikipedia) for more detailed information on the subject. \$\endgroup\$
    – Bubbler
    Jun 2, 2021 at 8:10


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.