You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求满足过两点且与两条直线相切的标准椭圆参数a、b

Solving for the Standard Ellipse Passing Through Two Points and Tangent to Two Lines

Let's break down how to find the coefficients (a) and (b) for the standard ellipse (\frac{x2}{a2} + \frac{y2}{b2} = 1) that satisfies your conditions. We'll use algebraic derivations and provide a Python implementation for matplotlib.

Key Background

First, let's simplify calculations by substituting (u = \frac{1}{a^2}) and (v = \frac{1}{b^2}). The ellipse equation becomes (ux^2 + vy^2 = 1). We need to satisfy four conditions:

  1. Ellipse passes through (P_2=(x_2,y_2)): (ux_2^2 + vy_2^2 = 1)
  2. Ellipse passes through (P_3=(x_3,y_3)): (ux_3^2 + vy_3^2 = 1)
  3. Ellipse is tangent to line (L_{12}) (through (P_1,P_2))
  4. Ellipse is tangent to line (L_{34}) (through (P_3,P_4))

For a line (lx + my + n = 0) to be tangent to the ellipse, the condition is (l2a2 + m2b2 = n^2), which translates to (\frac{l^2}{u} + \frac{m^2}{v} = n^2) (or (l^2v + m^2u = n^2uv) after multiplying by (uv)).

Step-by-Step Solution

1. Line Equations in General Form

For any line through points ((x_a,y_a)) and ((x_b,y_b)), the general form (lx + my + n = 0) has coefficients:

  • (l = y_b - y_a)
  • (m = -(x_b - x_a))
  • (n = (x_b - x_a)y_a - (y_b - y_a)x_a)

Apply this to get equations for (L_{12}) (coefficients (l_1,m_1,n_1)) and (L_{34}) (coefficients (l_2,m_2,n_2)).

2. Derive Quadratic Equations

Substitute the linear relationship between (u) and (v) (from the two point conditions) into the tangent conditions to get quadratic equations in (v):

  • For (L_{12}): (n_12y_22v^2 + (l_12x_22 - m_12y_22 - n_1^2)v + m_1^2 = 0)
  • For (L_{34}): (n_22y_22v^2 + (l_22x_22 - m_22y_22 - n_2^2)v + m_2^2 = 0)

3. Solve for (v) and (u)

Subtract the scaled quadratics to get a linear equation in (v), then solve for (v). Use (u = \frac{1 - vy_22}{x_22}) (from the point (P_2) condition) to find (u).

4. Compute (a) and (b)

If (u > 0) and (v > 0) (to ensure it's an ellipse), calculate (a = \sqrt{\frac{1}{u}}) and (b = \sqrt{\frac{1}{v}}).

Python Implementation

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

def find_standard_ellipse(P1, P2, P3, P4):
    x1, y1 = P1
    x2, y2 = P2
    x3, y3 = P3
    x4, y4 = P4
    
    # Calculate line coefficients for L12 and L34
    l1 = y2 - y1
    m1 = -(x2 - x1)
    n1 = (x2 - x1)*y1 - (y2 - y1)*x1
    
    l2 = y4 - y3
    m2 = -(x4 - x3)
    n2 = (x4 - x3)*y3 - (y4 - y3)*x3
    
    # Coefficients for quadratic equations
    A1 = (n1**2) * (y2**2)
    B1 = (l1**2)*(x2**2) - (m1**2)*(y2**2) - (n1**2)
    C1 = m1**2
    
    A2 = (n2**2)*(y2**2)
    B2 = (l2**2)*(x2**2) - (m2**2)*(y2**2) - (n2**2)
    C2 = m2**2
    
    # Solve for v
    numerator_v = C2 * A1 - C1 * A2
    denominator_v = B1 * A2 - B2 * A1
    
    if denominator_v == 0:
        # Handle edge cases (e.g., x2=0 or y2=0)
        if x2 == 0:
            v = 1 / (y2**2)
            denominator_u = (n1**2)*v - (m1**2)
            if denominator_u == 0:
                raise ValueError("No valid ellipse solution exists.")
            u = (l1**2 * v) / denominator_u
            # Verify with second tangent condition
            u_check = (l2**2 * v) / ((n2**2)*v - (m2**2))
            if not np.isclose(u, u_check):
                raise ValueError("No valid ellipse solution exists.")
        elif y2 == 0:
            u = 1 / (x2**2)
            denominator_v = (n1**2)*u - (l1**2)
            if denominator_v == 0:
                raise ValueError("No valid ellipse solution exists.")
            v = (m1**2 * u) / denominator_v
            v_check = (m2**2 * u) / ((n2**2)*u - (l2**2))
            if not np.isclose(v, v_check):
                raise ValueError("No valid ellipse solution exists.")
        else:
            raise ValueError("No valid ellipse solution exists.")
    else:
        v = numerator_v / denominator_v
        if x2 == 0:
            u = (1 - v*(y2**2)) / (x2**2)
        else:
            u = (1 - v*(y2**2)) / (x2**2)
    
    # Ensure solution is an ellipse
    if u <= 0 or v <= 0:
        raise ValueError("Solution is not an ellipse (u or v non-positive).")
    
    # Calculate a and b
    a = np.sqrt(1 / u)
    b = np.sqrt(1 / v)
    
    # Verify all conditions
    cond1 = np.isclose(u*x2**2 + v*y2**2, 1)
    cond2 = np.isclose(u*x3**2 + v*y3**2, 1)
    cond3 = np.isclose((l1**2)/u + (m1**2)/v, n1**2)
    cond4 = np.isclose((l2**2)/u + (m2**2)/v, n2**2)
    
    if not all([cond1, cond2, cond3, cond4]):
        raise ValueError("Computed values do not satisfy all conditions.")
    
    return a, b

# Example Usage
if __name__ == "__main__":
    # Sample points (ellipse x²/4 + y²/1 = 1)
    P1 = (0, 2/np.sqrt(3))
    P2 = (1, np.sqrt(3)/2)
    P3 = (2, 0)
    P4 = (2, 1 + np.sqrt(2))
    
    a, b = find_standard_ellipse(P1, P2, P3, P4)
    print(f"a = {a:.2f}, b = {b:.2f}")
    
    # Plotting
    fig, ax = plt.subplots()
    
    # Plot points
    points = [P1, P2, P3, P4]
    x_coords, y_coords = zip(*points)
    ax.scatter(x_coords, y_coords, color='red', label='Points')
    
    # Plot lines L12 and L34
    x_vals = np.linspace(-3, 3, 100)
    y_l12 = (4 - x_vals) / (2 * np.sqrt(3))
    ax.plot(x_vals, y_l12, 'g--', label='L12')
    ax.axvline(x=2, color='b--', label='L34')
    
    # Plot ellipse
    ellipse = Ellipse((0, 0), 2*a, 2*b, edgecolor='blue', facecolor='none', label='Ellipse')
    ax.add_patch(ellipse)
    
    ax.set_xlim(-3, 3)
    ax.set_ylim(-2, 3)
    ax.set_aspect('equal')
    ax.legend()
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.title('Standard Ellipse Passing Through Points and Tangent to Lines')
    plt.grid(True)
    plt.show()

Notes

  • The function handles edge cases (vertical/horizontal lines, points on axes) and verifies all conditions to ensure correctness.
  • If no valid ellipse exists (contradictory conditions), it raises a clear error.
  • The plotting code visualizes the ellipse, points, and tangent lines for validation.

内容的提问来源于stack exchange,提问作者Paul Terwilliger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:45:26