动画移动点 [英] Animating a moving dot

查看:39
本文介绍了动画移动点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为一个随机移动的点/图设置动画,直到它穿过一个圆圈,然后它会保持不变.理想情况下,将有几个点,当最终在圆上时,将均匀分布在所述圆上.我看过使用 matplotlib 和 matplotlib.animate 的代码,它们为函数的图形设置动画,但我无法完全理解它们.

I need to animate a dot/plot that moves randomly until it crosses a circle, which it then stays on. Ideally, there would be several dots which, when eventually on the circle, would be equally distributed on said circle. I've seen codes using matplotlib and matplotlib.animate which animated a function's graph, but I was not able to fully understand them.

感谢您的时间!

这就是一个点的样子

并带有几个点

推荐答案

我从来没有在 Python 中动画过任何东西,所以我认为我可以学习一些新的东西,并编写了以下动画点的代码,所以它会随机移动指示.也许它会帮助你开始你的任务.该代码基于 matplotlib-animation-tutorial

I've never animated anything in Python, so I though that I can learn something new, and wrote following code that animates a dot, so it moves in random directions. Maybe it will help You to start with Your task. The code is based on matplotlib-animation-tutorial.

我有一段很长的火车旅行,并且已经实现了您所要求的东西.您这里有一堆随机移动的点.一旦他们进入了蓝色圆圈,他们就会尝试最大程度地延长它们之间的距离,以便过一会儿您就可以了.

I had some long train trip and I've implemented the things You were asking for. You have a bunch of randomly moving dots here. Once they get inside the blue circle, they try to maximize the distance between them so after a while it looks like You wanted.

"""
Matplotlib Animation Example

author: Jake Vanderplas
email: vanderplas@astro.washington.edu
website: http://jakevdp.github.com
license: BSD
Please feel free to use and modify this, but keep the above information. Thanks!
"""
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import math

# 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):
        def distance(x1, y1, x2, y2):
            return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)

        def inside(x1, y1):
            if distance(x1, y1, 5, 5) <= 1:
                return True
            else:
                return False

        def calc_dist(d):
            ret = 0
            for x in dots:
                if inside(x.x, x.y) and x != d:                            
                    ret = ret + distance(x.x, x.y, d.x, d.y)
            return ret

        # if dot is inside the circle it tries to maximize the distances to
        # other dots inside circle
        if inside(self.x, self.y):
            dist = calc_dist(self)
            for i in xrange(1, 10):
                self.velx = self.generate_new_vel()
                self.vely = self.generate_new_vel()
                self.x = self.x + self.velx
                self.y = self.y + self.vely
                if calc_dist(self) <= dist or not inside(self.x, self.y):
                    self.x = self.x - self.velx
                    self.y = self.y - self.vely
        else:
            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
            if self.x >= 10:
                self.x = 10
                self.velx = -1 * self.velx
            if self.x <= 0:
                self.x = 0
                self.velx = -1 * self.velx
            if self.y >= 10:
                self.y = 10
                self.vely = -1 * self.vely
            if self.y <= 0:
                self.y = 0
                self.vely = -1 * self.vely

# Initializing dots
dots = [dot() for i in xrange(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')
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.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, frames=200, interval=20)

plt.show()

这篇关于动画移动点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆