Python draw tongue line

The moving line through the origin intersects the circle x 2 + y 2 − ay = 0 , a > 0 x^2+y^2-ay=0, a>0x2+y2a y=0,a>0 at point P, intersecting liney = ay=ay=a is at point Q, and the parallel lines passing through P and Q as the X-axis and the Y-axis respectively intersect at point M, then the trajectory of point M is called the tongue line.

Setting a = 2 a = 2a=2 , then the parametric equation of the circle is

x = cos ⁡ θ , y = sin ⁡ θ + 1 x=\cos\theta,y=\sin\theta+1x=cosθ ,y=sinθ+1

Let the equation of the moving line be y = kxy=kxy=k x , then withkkThe change of k can get a tongue line

insert image description here

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

a = 2
fig = plt.figure(figsize=(15,4))
ax = fig.add_subplot(autoscale_on=False, 
    xlim=(-5,5),ylim=(-0.1,2.5))
ax.grid()

t = np.arange(0,6.4,0.1)
ax.plot(np.cos(t),np.sin(t)+1,'-',lw=1) #圆方程
ax.plot([-5,5],[a,a],'-',lw=1)

xLine, = ax.plot([],[],linestyle='--',lw=0.5)
yLine, = ax.plot([],[],linestyle='--',lw=0.5)
kLine, = ax.plot([],[],linestyle='--',lw=0.5)

trace, = ax.plot([],[],'-', lw=1)
k_text = ax.text(0.05,0.85,'',transform=ax.transAxes)
textTemplate = 'k = %.1f\n'

xs,ys = [],[]
def animate(theta):
    if(theta==0):
        xs.clear()
        ys.clear()
    k = np.tan(theta)
    px = 2*k/(k**2+1)
    py = k*px
    qx = a/k
    xLine.set_data([px,qx],[py,py])
    yLine.set_data([qx,qx],[2,py])
    kLine.set_data([0,qx],[0,a])
    xs.append(qx)
    ys.append(py)
    trace.set_data(xs,ys)
    k_text.set_text(textTemplate % k)
    return xLine, yLine, kLine, trace, k_text

frames = np.arange(0,np.pi,0.05)
ani = animation.FuncAnimation(fig, animate, 
    frames, interval=50, blit=True)
ani.save("witch.gif")

plt.show()

Guess you like

Origin blog.csdn.net/m0_37816922/article/details/124244437