【问题标题】:Can you use VPython in an OOP class?你可以在 OOP 类中使用 VPython 吗?
【发布时间】:2020-02-11 21:20:49
【问题描述】:

我正在模拟太阳系,并且正在使用面向对象的编程来简化添加新行星的过程。

from vpython import *

#gravitational constant
G = 6.674e-11
#seconds in a day
dt = 86400

step = 1
maxstep = 3000

class Planet:

    def __init__(self, mass, radius, position, velocity):
        self.mass = mass
        self.radius = radius
        self.position = position
        self.velocity = velocity

class Star:

    def __init__(self, mass, radius):
        self.mass = mass
        self.radius = radius

Earth = Planet(5.972e24, 6371, vector(0,1.47e8,0), vector(-29.951,0,0))
Mercury = Planet(3.285e23, 2439.7, vector(0,4.6e7,0), vector(-47.400,0,0))
Sun = Star(1.989e30, 696340)

#acceleration vectors
a_Earth = -G*Sun.mass*Earth.position/mag(Earth.position)**3
a_Mercury = -G*Sun.mass*Mercury.position/mag(Mercury.position)**3

#position vectors
Earth.position = Earth.position + Earth.velocity*dt + 0.5*a_Earth*dt**2

我正在尝试做的是使用VPython的sphere命令来表示行星和恒星,有没有办法在定义新的类时使用它?

【问题讨论】:

    标签: python-3.x object vpython


    【解决方案1】:

    当然。例如,用这个结束你的 Planet init

    返回球体(质量=质量,半径=半径,位置=位置,速度=速度)

    其中 pos 和 radius 是 VPython 球体属性,而 mass 和 velocity 是你自己的属性。

    对于 VPython 问题,最好在 VPython 论坛上发帖,那里有更多的 VPython 用户会看到你的问题,而不是在 stackoverflow 上发帖:

    https://groups.google.com/forum/?fromgroups&hl=en#!forum/vpython-users

    【讨论】:

      【解决方案2】:

      尝试子类化 sphere (is-a) 或让您的类有一个 (has-a) sphere。

      【讨论】:

        猜你喜欢
        • 2023-03-07
        • 2023-04-04
        • 1970-01-01
        • 2020-01-29
        • 2016-12-01
        • 2010-09-23
        • 1970-01-01
        • 2014-11-13
        • 1970-01-01
        相关资源
        最近更新 更多