我试图编写一个能够生成在圆圈内移动的点的函数。我已经有了一个用matpltolib动画绘制动画点的功能(感谢Tony Babarino)但是我在编写强制点留在圆圈中的部分时遇到了麻烦
这是如何使运动发挥作用的部分
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
# Initializing number of dots
N = 25
# Creating dot class
class dot(object):
def __init__(self):
self.x = 10 * np.random.random_sample()
self.y = 10 * np.random.random_sample()
self.velx = self.generate_new_vel()
self.vely = self.generate_new_vel()
def generate_new_vel(self):
return (np.random.random_sample() - 0.5) / 5
def move(self) :
if np.random.random_sample() < 0.95:
self.x = self.x + self.velx
self.y = self.y + self.vely
else:
self.velx = self.generate_new_vel()
self.vely = self.generate_new_vel()
self.x = self.x + self.velx
self.y = self.y + self.vely
# Initializing dots
dots = [dot() for i in range(N)]
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
d, = ax.plot([dot.x for dot in dots],
[dot.y for dot in dots], 'ro', markersize=3)
circle = plt.Circle((5, 5), 1, color='b', fill=False)
ax.add_artist(circle)
# animation function. This is called sequentially
def animate(i):
for dot in dots:
dot.move()
d.set_data([dot.x for dot in dots],
[dot.y for dot in dots])
return d,
# call the animator.
anim = animation.FuncAnimation(fig, animate, frames=200, interval=20)
plt.show()
我想通过添加一个强制点在圆圈中点的部分来改善函数移动 我希望圆圈的边框就像一个点不能交叉的栅栏
我知道如何检测一个点何时越过线但我不知道在那之后该怎么做。
非常感谢您的帮助
答案 0 :(得分:0)
我改变了代码,这样你就不会有振动和逃避圆圈的问题。进入圆圈的每个点都不会逃逸(点在开始时可以在圆外),也不会在圆的边缘振动。 导入numpy为np 从matplotlib导入pyplot作为plt 来自matplotlib导入动画
# Initializing number of dots
N = 25
XC = 5
YC = 5
R = 1
R_SQR = R **2
# Creating dot class
class dot(object):
def __init__(self, ind):
self.x = 10 * np.random.random_sample()
self.y = 10 * np.random.random_sample()
self.velx = self.generate_new_vel()
self.vely = self.generate_new_vel()
self.in_circle = not self.calc_out_of_circle()
def generate_new_vel(self):
return (np.random.random_sample() - 0.5) / 5
def move(self) :
if np.random.random_sample() < 0.95:
self.check_out_of_circle()
self.x += self.velx
self.y += self.vely
else:
self.velx = self.generate_new_vel()
self.vely = self.generate_new_vel()
self.check_out_of_circle()
self.x += self.velx
self.y += self.vely
self.check_inside_circle()
def calc_out_of_circle_with_params(self, x, y):
return (x - XC) ** 2 + (y - YC) ** 2 >= R_SQR ** 2
def calc_out_of_circle(self):
return self.calc_out_of_circle_with_params(self.x, self.y)
def check_out_of_circle(self):
if self.in_circle and self.calc_out_of_circle_with_params(self.x + self.velx, self.y + self.vely):
self.velx = -self.velx
self.vely = -self.vely
def check_inside_circle(self):
if not self.calc_out_of_circle():
self.in_circle = True
# Initializing dots
dots = [dot(i) for i in range(N)]
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
d, = ax.plot([dot.x for dot in dots],
[dot.y for dot in dots], 'ro', markersize=3)
circle = plt.Circle((XC, YC), R, color='b', fill=False)
ax.add_artist(circle)
# animation function. This is called sequentially
def animate(i):
for dot in dots:
dot.move()
d.set_data([dot.x for dot in dots],
[dot.y for dot in dots])
return d,
# call the animator.
anim = animation.FuncAnimation(fig, animate, frames=200, interval=20)
# plt.axis('equal')
plt.show()