关于椭圆的方程(有Python画的动图)

关于椭圆的方程(有Python画的动图)

flyfish

几何定义

椭圆是平面上所有到两个固定点(焦点)的距离之和为常数的点的集合。这两个固定点叫做焦点。

解析几何描述

设椭圆的两个焦点为 F 1 F_1 F1 和 F 2 F_2 F2,焦距(两焦点之间的距离的一半)为 c c c,长轴的半长轴为 a a a,短轴的半短轴为 b b b,椭圆上任意一点到这两个焦点的距离之和是一个常数 2 a 2a 2a。如果椭圆的中心在原点,长轴平行于 x x x 轴,则椭圆的标准方程为: x 2 a 2 + y 2 b 2 = 1 \frac{x^2}{a^2} + \frac{y^2}{b^2} = 1 a2x2+b2y2=1如果长轴平行于 y y y 轴,只需交换 a a a 和 b b b 的位置: x 2 b 2 + y 2 a 2 = 1 \frac{x^2}{b^2} + \frac{y^2}{a^2} = 1 b2x2+a2y2=1

py 复制代码
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation, PillowWriter

def plot_ellipse_with_moving_point(a, b, num_frames=100, interval=50):
    # 椭圆方程参数
    theta = np.linspace(0, 2 * np.pi, num_frames)
    x = a * np.cos(theta)
    y = b * np.sin(theta)
    
    # 焦点位置
    c = np.sqrt(a**2 - b**2)
    F1 = (-c, 0)
    F2 = (c, 0)
    
    # 创建图形
    fig, ax = plt.subplots(figsize=(8, 6))
    ax.plot(x, y, label=f'Ellipse: $\\frac{{x^2}}{{{a}^2}} + \\frac{{y^2}}{{{b}^2}} = 1$')
    ax.scatter(*F1, color='red')
    ax.scatter(*F2, color='red')
    ax.text(F1[0], F1[1], 'F1', fontsize=12, ha='right')
    ax.text(F2[0], F2[1], 'F2', fontsize=12, ha='left')
    ax.axhline(0, color='black', linewidth=0.5)
    ax.axvline(0, color='black', linewidth=0.5)
    ax.grid(color='gray', linestyle='--', linewidth=0.5)
    ax.set_aspect('equal', adjustable='box')
    ax.set_title('Ellipse with Moving Point')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.legend()

    # 初始化点 P 和连接线
    point, = ax.plot([], [], 'bo')
    line1, = ax.plot([], [], 'gray', linestyle='dotted')
    line2, = ax.plot([], [], 'gray', linestyle='dotted')

    # 初始化函数
    def init():
        point.set_data([], [])
        line1.set_data([], [])
        line2.set_data([], [])
        return point, line1, line2

    # 更新函数
    def update(frame):
        P = (a * np.cos(theta[frame]), b * np.sin(theta[frame]))
        point.set_data([P[0]], [P[1]])
        line1.set_data([F1[0], P[0]], [F1[1], P[1]])
        line2.set_data([F2[0], P[0]], [F2[1], P[1]])
        return point, line1, line2

    # 创建动画
    ani = FuncAnimation(fig, update, frames=num_frames, init_func=init, interval=interval, blit=True)

    # 保存动画
    ani.save('ellipse_with_moving_point.gif', writer=PillowWriter(fps=20))

    plt.show()

# 参数
a = 5
b = 3
plot_ellipse_with_moving_point(a, b)
相关推荐
confiself1 分钟前
A2UI实时渲染展示
开发语言·javascript·css
NGC_661113 分钟前
Java基础面试题2
java·开发语言·python
马猴烧酒.17 分钟前
【JAVA算法|hot100】贪心算法类型题目详解笔记
java·开发语言·ide·笔记·算法·spring·贪心算法
语戚17 分钟前
深入浅出 AOP:织入时机、JDK 动态代理与 CGLIB 原理及 Spring 选择策略
java·开发语言·spring·jdk·代理模式·aop·动态代理
bubiyoushang88819 分钟前
基于MATLAB的可见光通信(VLC)发射端:电-光转换与LED驱动仿真
开发语言·matlab
vx-程序开发19 分钟前
springboot具备推荐和预警机制的大学生兼职平台的设计与实现-计算机毕业设计源码17157
java·c++·spring boot·python·spring·django·php
EnCi Zheng24 分钟前
11a. 阿里云大模型API调用基础
人工智能·python·阿里云·云计算
不光头强40 分钟前
Java中的异常
java·开发语言
Coding茶水间41 分钟前
基于深度学习的管道缺陷检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
开发语言·人工智能·深度学习·yolo·机器学习
shamalee43 分钟前
MS SQL Server partition by 函数实战二 编排考场人员
java·服务器·开发语言