【python 小白到精通】第五章:面向对象编程 – 三星运行模拟

文章目录

前言

这一章学习了 面向对象编程,需要掌握的内容包括:会调用对象的方法和属性;如何编写类。

这一章课程中有个模拟双星运行的小程序,为了增进理解,我改了改代码,成了 模拟三星运行

为什么使用面向对象

完成一些简单任务时,使用面向对象常常会 需要写更多的代码,那我们为什么要使用面向对象呢?

由于面向对象的一些特性 (这里不细说),我们可以设计出 低耦合的系统,更容易修改和维护

三星运行模拟

我们 把星球作为对象,设计一个星球类,类中就包含了星球的一些性质。然后在主程序中调用星球的方法,让它自己的事情自己做。

【python 小白到精通】第五章:面向对象编程 - 三星运行模拟
在多次调整参数后,它才终于可以多一起运行一会而,最终被我调成了类似恒星系的结构(有一个大质量的中心星球)。本来是想试试能不能出现三体那种效果的。

【python 小白到精通】第五章:面向对象编程 - 三星运行模拟

; 定义星球类

import math

class Planet:
    Number = 0

    def __init__(self, color, mass, position, v_init):
        Planet.Number += 1
        self.color = color
        self.mass = mass
        self.x, self.y = position
        self.vx, self.vy = v_init

    def v_change(self, f, t=1e-3):
        fx, fy = f
        ax, ay = fx / self.mass, fy / self.mass
        self.vx += ax * t
        self.vy += ay * t

    def p_change(self, t=1e-3):
        self.x += self.vx * t
        self.y += self.vy * t

    def gravity(self, other_planet):
        distance = math.sqrt((self.x - other_planet.x) ** 2 + (self.y - other_planet.y) ** 2)
        f = self.mass * other_planet.mass / (distance ** 2)
        fx = (other_planet.x - self.x) / distance * f
        fy = (other_planet.y - self.y) / distance * f
        return fx, fy

主程序

import matplotlib.pyplot as plt
from matplotlib import animation

fig = plt.figure()

point_1 = Planet('r', 100, (-3, -3), (0, 0.3))
point_2 = Planet('r', 10, (5, -5), (0, -3))
point_3 = Planet('r', 0.1, (0, 10), (3, 0))

def animate(i):
    fig.clear()
    for _ in range(500):
        f_21 = point_1.gravity(point_2)
        f_31 = point_1.gravity(point_3)
        f_12 = point_2.gravity(point_1)
        f_32 = point_2.gravity(point_3)
        f_13 = point_3.gravity(point_1)
        f_23 = point_3.gravity(point_2)

        point_1.p_change()
        point_1.v_change(f_21)
        point_1.v_change(f_31)

        point_2.p_change()
        point_2.v_change(f_12)
        point_2.v_change(f_32)

        point_3.p_change()
        point_3.v_change(f_13)
        point_3.v_change(f_23)

    plt.scatter(point_1.x, point_1.y, c='r')
    plt.scatter(point_2.x, point_2.y, c='b')
    plt.scatter(point_3.x, point_3.y, c='g')

    plt.xlim(-20, 20)
    plt.ylim(-20, 20)
    plt.draw()

ani = animation.FuncAnimation(fig=fig, func=animate, frames=100, interval=1, blit=False)
plt.show()

14天学习训练营导师课程:
杨鑫《Python 自学编程基础》
杨鑫《 Python 网络爬虫基础》
杨鑫《 Scrapy 爬虫框架实战和项目管理》

Original: https://blog.csdn.net/m0_63238256/article/details/127847811
Author: 清风莫追
Title: 【python 小白到精通】第五章:面向对象编程 – 三星运行模拟

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/726567/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球