High School Math with Python: Quadratic Curve

Circles, Parabolas, Ellipses, and Hyperbolas as Conic Sections

Circles, parabolas, ellipses, and hyperbolas are curves that appear as cross sections of a cone.

When a cone is cut by a plane, the curve formed on the cutting surface depends on the angle between the plane and the cone.
By changing this angle, all four types of conic sections appear.

If we list their standard forms (equations), we get:

  • Circle
    ( x^2 + y^2 = r )
    where ( r ) is the radius

  • Parabola
    ( y^2 = 4px ), ( y = x^2/4p )

  • Ellipse
    (x/a)^2 + (y/b)^2 = 1 )

  • Hyperbola
    (x/a)^2 – (y/b)^2 = 1 )

What This Program Shows

In the program below, the equation of the cross section appears in the legend of the 2D graph.

Try moving the slider to interactively change the cutting angle and observe how the curve changes.

This program was generated with the help of AI.
It goes beyond standard high school mathematics, so the implementation itself is not meant as a manual exercise.

Key Point

Circles, parabolas, ellipses, and hyperbolas all belong to the same family: conic sections.

Executable Program (Google Colab)

https://colab.research.google.com/drive/1QFEermnXAl1-DDMgpzlZ-HCNSrIZr0H7

Python Code

import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact, FloatSlider

def draw_conic_with_equation(beta_deg):
    alpha_deg = 45
    alpha = np.radians(alpha_deg)
    beta = np.radians(beta_deg)
    d = 0.5

    fig = plt.figure(figsize=(14, 6))

    # --- Left: 3D cone and cutting plane ---
    ax1 = fig.add_subplot(121, projection='3d')
    r = np.linspace(0, 2, 40)
    theta = np.linspace(0, 2 * np.pi, 40)
    R, THETA = np.meshgrid(r, theta)
    X_cone = R * np.cos(THETA)
    Y_cone = R * np.sin(THETA)
    Z_cone_up = R / np.tan(alpha)
    Z_cone_down = -R / np.tan(alpha)

    X_p = np.linspace(-2, 2, 10)
    Y_p = np.linspace(-2, 2, 10)
    XP, YP = np.meshgrid(X_p, Y_p)
    ZP = XP * np.tan(beta) + d

    ax1.plot_surface(X_cone, Y_cone, Z_cone_up, alpha=0.15, color='cyan')
    ax1.plot_surface(X_cone, Y_cone, Z_cone_down, alpha=0.15, color='cyan')
    ax1.plot_surface(XP, YP, ZP, alpha=0.4, color='orange')
    ax1.set_title("3D View")

    # --- Right: 2D cross section ---
    ax2 = fig.add_subplot(122)

    # Expanded equation of the cross section:
    # y^2 = (tan^2(beta) - 1)x^2 + (2d tan(beta))x + d^2
    x_range = np.linspace(-4, 4, 1000)
    tan_b = np.tan(beta)
    y_sq = (x_range * tan_b + d)**2 - x_range**2

    valid = y_sq >= 0
    x_plot = x_range[valid]
    y_plot = np.sqrt(y_sq[valid])

    # Coefficients for legend display
    A = tan_b**2 - 1
    B = 2 * d * tan_b
    C = d**2

    # Equation label
    eq_label = rf'$y^2 = ({A:.2f})x^2 + ({B:.2f})x + {C:.2f}$'

    ax2.plot(x_plot, y_plot, color='blue', linewidth=2, label=eq_label)
    ax2.plot(x_plot, -y_plot, color='blue', linewidth=2)

    # Determine conic type
    if beta_deg == 0:
        shape_type = "Circle"
    elif beta_deg < alpha_deg:
        shape_type = "Ellipse"
    elif beta_deg == alpha_deg:
        shape_type = "Parabola"
    else:
        shape_type = "Hyperbola"

    ax2.set_title(f"2D Cross Section: {shape_type}")
    ax2.set_xlabel('x')
    ax2.set_ylabel('y')
    ax2.set_xlim(-3, 3)
    ax2.set_ylim(-3, 3)
    ax2.grid(True)
    ax2.axhline(0, color='black', lw=1)
    ax2.axvline(0, color='black', lw=1)

    # Legend in the lower-left corner
    ax2.legend(loc='lower left', fontsize=10)

    plt.tight_layout()
    plt.show()

interact(
    draw_conic_with_equation,
    beta_deg=FloatSlider(min=0, max=85, step=1, value=0, description='Angle β')
)

Leave a Reply