[Python] Tangent points to a rotated ellipse from outer point

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    C = known outer point, Expected result A(x,y), B(x1,y1)

    [​IMG]

    I guess the answer could be hidden inside first answer here, which calculates tangents slopes & intercepts. Modified a bit I assume. Tangent lines to a rotated ellipse from a point

    from typing import Tuple

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


    def find_tangent_lines(
    center: Tuple[float, float],
    semi_axes: Tuple[float, float],
    rotation: float,
    reference_point: Tuple[float, float],
    ):
    """Find the Ellipse's two tangents that go through a reference point.

    Args:
    center: The center of the ellipse.
    semi_axes: The semi-major and semi-minor axes of the ellipse.
    rotation: The counter-clockwise rotation of the ellipse in radians.
    reference_point: The coordinates of the reference point.

    Returns:
    (m1, h1): Slope and intercept of the first tangent.
    (m2, h2): Slope and intercept of the second tangent.
    """
    x0, y0 = center
    a, b = semi_axes
    s, c = np.sin(rotation), np.cos(rotation)
    p0, q0 = reference_point

    A = (-a**2*s**2 - b**2*c**2 + (y0-q0)**2)
    B = 2*(c*s*(a**2-b**2) - (x0-p0)*(y0-q0))
    C = (-a**2*c**2 - b**2*s**2 + (x0-p0)**2)

    if B**2 - 4*A*C < 0:
    raise ValueError('Reference point lies inside the ellipse')

    t1, t2 = (
    (-B + np.sqrt(B**2 - 4*A*C))/(2*A),
    (-B - np.sqrt(B**2 - 4*A*C))/(2*A),
    )
    return (
    (1/t1, q0 - p0/t1),
    (1/t2, q0 - p0/t2),
    )

    Continue reading...

Compartilhe esta Página