You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

已知外部点,求解到旋转椭圆的切点坐标

已知外部点,求解到旋转椭圆的切点坐标

我来帮你解决这个问题——已知外部点C,我们需要找到它到旋转椭圆的两个切点A(x,y)和B(x1,y1),先看一下问题示意图:

A?,B?

解决这个问题的核心思路是先求出过外部点C的两条椭圆切线的方程,再通过切线与椭圆的联立方程计算出切点坐标。下面是我整理的Python代码,能先帮你算出两条切线的斜率和截距:

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 (也就是外部点C).

    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),
    )

后续获取切点坐标的步骤:

  • 调用上述函数得到两条切线的斜率m和截距h,切线方程为 y = m*x + h
  • 写出旋转椭圆的转换方程:将坐标系平移到椭圆中心后再旋转变换,椭圆的标准形式为 (X/a)² + (Y/b)² = 1,其中X = (x - x0)*c + (y - y0)*sY = -(x - x0)*s + (y - y0)*cx0,y0是椭圆中心,c=cos(rotation)s=sin(rotation)
  • 将切线方程代入椭圆的转换方程,解这个一元二次方程得到切点的x坐标,再代入切线方程算出对应的y坐标,就能得到两个切点A和B的具体坐标了。

备注:内容来源于stack exchange,提问作者Miran C

火山引擎 最新活动