QuickDraw 中的 python3 递归动画

2024-03-07

我有一个文本文件,其中包含行星及其相应的卫星/卫星以及它们的轨道半径和周期,我想用它来创建动画quickdraw类似于下面的:

文本文件如下:

RootObject: Sun

Object: Sun
Satellites: Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,Ceres,Pluto,Haumea,Makemake,Eris
Radius: 20890260
Orbital Radius: 0

Object: Miranda
Orbital Radius: 5822550
Radius: 23500
Period: 1.413

Object: Ariel
Orbital Radius: 8595000
Radius: 60000
Period: 2.520379

Object: Umbriel
Orbital Radius: 11983500
Radius: 60000
Period: 4.144177

Object: Titania
Orbital Radius: 19575000
Radius: 75000
Period: 8.7058

Object: Oberon
Orbital Radius: 26235000
Radius: 75000
Period: 13.463

Object: Uranus
Orbital Radius: 453572956
Radius: 2555900
Period: 30799
Satellites: Puck,Miranda,Ariel,Umbriel,Titania,Oberon

Object: Neptune
Orbital Radius: 550000000
Radius: 2476400
Period: 60190
Satellites: Triton

Object: Triton
Orbital Radius: 40000000
Radius: 135300
Period: -5.8

Object: Mercury
Orbital Radius: 38001200
Period: 87.9691
Radius: 243900.7

Object: Venus
Orbital Radius: 57477000
Period: 224.698
Radius: 605100.8

Object: Earth
Orbital Radius: 77098290
Period: 365.256363004
Radius: 637100.0
Satellites: Moon

Object: Moon
Orbital Radius: 18128500
Radius: 173700.10
Period: 27.321582

Object: Mars
Orbital Radius: 106669000
Period: 686.971
Radius: 339600.2
Satellites: Phobos,Deimos

Object: Phobos
Orbital Radius: 3623500.6
Radius: 200000
Period: 0.31891023

Object: Deimos
Orbital Radius: 8346000
Period: 1.26244
Radius: 200000.2

Object: Jupiter
Orbital Radius: 210573600
Period: 4332.59
Radius: 7149200
Satellites: Io,Europa,Ganymede,Callisto

Object: Ceres
Orbital Radius: 130995855
Period: 1679.67
Radius: 48700

Object: Io
Orbital Radius: 22000000
Period: 1.7691377186
Radius: 182100.3

Object: Europa
Orbital Radius: 36486200
Period: 3.551181
Radius: 156000.8

Object: Ganymede
Orbital Radius: 47160000
Period: 7.15455296
Radius: 263400

Object: Callisto
Orbital Radius: 69700000
Period: 16.6890184
Radius: 241000

Object: Saturn
Orbital Radius: 353572956
Period: 10759.22
Radius: 6026800
Satellites: Mimas,Enceladus,Tethys,Dione,Rhea,Titan,Iapetus

Object: Mimas
Orbital Radius: 8433396
Radius: 20600
Period: 0.9

Object: Enceladus
Orbital Radius: 10706000
Radius: 25000
Period: 1.4

Object: Tethys
Orbital Radius: 13706000
Radius: 50000
Period: 1.9

Object: Dione
Orbital Radius: 17106000
Radius: 56000
Period: 2.7

Object: Rhea
Orbital Radius: 24000000
Radius: 75000
Period: 4.5

Object: Titan
Orbital Radius: 50706000
Radius: 257600
Period: 15.945

Object: Iapetus
Radius: 75000
Orbital Radius: 72285891
Period: 79

我已经将原来的代码(长得离奇)改为了这个较短的代码:(归功于 sudo_O 的出色表现help https://stackoverflow.com/questions/13519350/inputing-data-into-dictionary-python/13519883#13519883)

file = open("data1.txt","r")

def data(file):
    d = {}
    for line in file:
        if line.strip() != '':
            key,value = line.split(":")
            if key == 'RootObject':
                continue
            if key == 'Object':                
                obj = value.strip()
                d[obj]={}
            else:
                d[obj][key] = value.strip()
    return d

planets = data(file)

print(planets)

我最大的问题是我不知道如何编写从文件导入数据并通过以下方式使用它的代码recursion创建与所示类似的动画。我不断被告知解决方案非常简单,代码实际上非常短,但我不知道如何做到这一点,这实际上非常令人沮丧。

这是我绕圆旋转的代码,但看起来太复杂了......

import math

print("circle",400,300,50)

print("flush false")

centreX=400
centreY=300
radius=100
angle=math.pi/2

while True:
    print("color 0 0 0")
    print("clear")
    print("color 255 0 255")
    print("circle",centreX,centreY,radius)
    childX=math.sin(angle)*radius*1.5+centreX
    childY=math.cos(angle)*radius*1.5+centreY
    print("circle",childX,childY,radius/2)
    print("refresh")
    angle+=0.01

规模代码:

scale=250/max([dict[planets]["Orbital Radius"] for x in dict if "Orbital Radius" in dict[planets]])

绕轨道运行的行星代码:

print("flush false")
scale=250/max([dict[planets]["Orbital Radius"] for x in dict if "Orbital Radius" in dict[planets]])
t=0
x=400
y=300
while True:
    print("refresh")
    print("colour 0 0 0")
    print("clear")
    print("colour 255 255 255")
    print("fillcircle",x,y,dict['Sun']['Radius']*scale)
    print("text ", "\"Sun\"",x+dict['Sun']['Radius']*scale,y)
    r_earth=dict['Earth']['Orbital Radius']*scale;
    print("circle",x,y,r_earth)
    r_X=x+math.sin(t*2*math.pi/dict['Earth']['Period'])*r_earth
    r_Y=y+math.cos(t*2*math.pi/dict['Earth']['Period'])*r_earth
    print("fillcircle",r_X,r_Y,dict['Earth']['Radius']*scale)
    print("text ", "\"Earth\"",r_X+dict['Earth']['Radius']*scale,r_Y)
    t+=0.02

这段代码不能很好地工作,但是作为初学者,我对这个问题已经处于极限......

最后的努力!!这行得通吗?

print("flush false")
scale = 250/max([dict[planets]["Orbital Radius"] for x in dict if "Orbital Radius" in dict[planets]])
t = 0
x = 400
y = 300
print("fillcircle",400,300,dict['Sun']['Radius']*scale)
print("text ", "\"Sun\"",x+dict['Sun']['Radius']*scale,y)


while True:
    r_new = dict['Object']['Orbital Radius']*scale
    print("circle",x,y,r_new)
    r_X = x + math.sin(t*2*math.pi/dict['Object']['Period'])*r_new
    r_Y = y + math.cos(t*2*math.pi/dict['Object']['Period'])*r_new
    print("fillcircle",r_X,r_Y,dict['Object']['Radius']*scale)
    print("text ",Object,r_X+dict['Object']['Radius']*scale,r_Y)
    t += 0.02
    if planets['Object']['Satellites'] = 0
        return
    else:
        r_sat = dict['Object']['Satellites']['Orbital Radius']*scale
        print("circle",x,y,r_sat)
        r_satX = x + math.sin(t*2*math.pi/dict['Object']['Satellites']['Period'])*r_sat
        r_satY = y + math.cos(t*2*math.pi/dict['Object']['Satellites']['Period'])*r_sat
        print("fillcircle",r_satX,r_satY,dict['Object']['Satellites']['Radius']*scale)
        print("text ",['Object']['Satellites'],r_satX+dict['Object']['Satellites']['Radius']*scale,r_satY)
        t += 0.02

第一个问题递归- 递归函数是调用自身的函数!一个简单的例子是倒计时函数,例如:

def countdown(n):
    # counting down the recursive way! 
    if n > 0:
        print n
        countdown(n-1)
    else:
        return

Calling countdown(10)将打印10, 9, 8,.., 2, 1.

你可以看到countdown传递了一个数字n它所做的就是打印该数字,然后调用自身但传递n-1这次。只有一次n=0如果传递了,那么它就没有什么可做的了,因此每个递归调用都会返回。适合您的词典词典案例(我们称其为词典库以避免混淆) a recursive打印方法是:

  1. 将整个库传递给打印例程。
  2. 打印第一本词典。
  3. 递归调用自身并传递减去第一个字典的库。
  4. 当图书馆没有更多词典可供打印时返回。

像这样的事情:

def recursive_print(dic):

    if len(dic) > 0:                  # If dictionaries in library > 0 
        print dic.keys()[0]           # Print the key i.e Earth
        print dic[dic.keys()[0]]      # Print the dictionary value for i.e Earth
        dic.popitem()                 # Remove the Earth dictionary from library
        recursive_print(dic)          # Recursive call 
    else:
        return                        # Printed all, return up the stack.


planets = data(file)
recursive_print(planets)

下一步不是以当前格式打印字典,而是进行一些计算/转换,以便输出有效quickdraw输入,就像您已经使用的用于打印圆圈的代码一样quickdraw.

您需要担心比例,以确保所有物体都适合绘图表面,找到最大的天体Orbital Radius并用它来计算比例。

使用类似于您的代码的列表理解,我们可以找到最大值:

max([planet[key]['Orbital Radius'] for key in planet])

>>> 8595000

scale = gridsize/max([planet[key]['Orbital Radius'] for key in planet])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

QuickDraw 中的 python3 递归动画 的相关文章

随机推荐

  • 将 Typescript Map 转换为 json 字符串表示形式

    我有一个Map
  • .NET 捕获一般异常

    NET 编程指南规定我们不应该捕获一般异常 我认为以下代码不是很好 因为一般异常类型捕获 private object CreateObject string classname object obj null if string IsNu
  • 如果设备空闲,Android 前台服务会变慢

    我有一个 android 前台服务 通过通知调用 在服务中 我只是每 10 秒记录一次 Tick trap 但服务的优先级是每 X 秒在 Web 视图中导航一次 所以我使用新线程并也在主线程中工作 如果我将应用程序连接到 USB 日志似乎没
  • 为什么对变量调用方法会阻止 Rust 推断变量的类型?

    此代码编译 derive Debug Default struct Example impl Example fn some method self fn reproduction gt Example let example Defaul
  • 网站图标不工作

    我尝试插入网页的图标无法正常工作 有人可以告诉我插入它所需的代码和样式吗
  • 以动态/编程方式向 SQL 添加 WHERE 子句

    如何以编程方式向 SQL 存储过程添加搜索条件 在我的应用程序 C 中 我使用存储过程 SQL Server 2008R2 ALTER PROCEDURE dbo PROC001 userID varchar 20 password var
  • Vue 过渡与 Tailwind css 在淡出时不可见

    我使用 Tailwind css 和 Vue js 创建模式 由于 Tailwind 不支持 Vue 2 我必须添加过渡 您可以在这里看到想要的效果 https tailwindui com components application u
  • VideoView的setVideoPath和setVideoURI有什么区别

    视频查看 http developer android com reference android widget VideoView html有两种不同的方式来指定要播放的视频 设置视频路径 http developer android c
  • 如何在仅引用数据的表中循环

    我正在使用功能模块RSAQ QUERY CALL 取回一张桌子 DATA gr data TYPE REF TO data CALL FUNCTION RSAQ QUERY CALL EXPORTING query ZXXXXXXXX us
  • 通过 docker 实现 RSelenium

    我的操作系统是windows 8 1 R的版本是3 3 3 我已经安装了 RSelenium 软件包 并尝试使用以下命令运行它 library RSelenium start RSelenium server startServer che
  • 如何在步骤 1 中单击“下一步”时让 JQuery-Steps 调用 ajax 服务

    我正在使用 jquery 步骤 尽管我需要在单击第一个 下一步 时通过 ajax 调用 c 服务 但这是否可以在显示步骤 2 之前调用并返回 尽管 ajax 事件在加载步骤 2 后返回 但以下代码仍然有效 非常感谢 感谢任何帮助 Jquer
  • Java 形式类型参数定义(泛型)

    我想定义一个泛型类型 其实际类型参数只能是 数字基元包装类之一 Long Integer Float Double String 我可以用这样的定义满足第一个要求 public final class MyClass
  • 以编程方式重新启动设备

    在我的 Android 应用程序中 我想在单击按钮时重新启动我的 Android 设备 但它不起作用 到目前为止我已经做到了 ImageButton restartmob ImageButton this findViewById R id
  • 传单图块在移动设备上加载不正确

    我遇到了传单地图在移动设备上加载不正确的问题 该地图的加载纬度 经度为 25 748503 80 286949 迈阿密市中心 但在移动设备上加载的纬度 经度约为 25 584223 80 028805 大西洋沿岸 将地图拖回到正确的位置似乎
  • 我可以将 terraform 输出设置为环境变量吗?

    因此 terraform 生成了一堆我感兴趣的输出 如何将这些值通过管道传递给环境变量或其他东西 以便我的脚本能够在有人运行后获取它们terraform apply Terraform 无法直接修改调用它的 shell 的环境 一般来说 程
  • 在谷歌地图中为每个国家提供不同的颜色

    有谁知道如何在谷歌地图中为每个国家提供不同的颜色 e g 在世界地图上 蓝色覆盖英国 然后红色中国 等 我想知道google是否提供API来为每个国家提供颜色 Thanks 使用谷歌地图这确实不容易 正如 oezi 所说 你需要为你想要着色
  • 使用以位集作为键的映射时出现问题

    我正在尝试创建一个map在 C 中bitset作为钥匙 但是编译器会生成以下错误消息 In file included from usr include c 4 6 string 50 0 from usr include c 4 6 bi
  • Presto 中的用户定义函数

    我目前正在使用 Presto 0 80 我必须编写一个用户定义的函数来在选择查询期间将摄氏度转换为华氏度 我使用 Hive QL 做了同样的事情 但想知道我们是否可以在 Facebook Presto 中复制相同的内容 任何帮助将不胜感激
  • GAE组织数据结构问题

    好的 我正在与 GAE 合作 我想创建这样的东西 我有类型 组 主题 标签 每个 组 可以有尽可能多的 根据需要 主题 每个 主题 可以有任意多个 标签 如所须 每个 组 可以有任意多个 标签 如所须 它就像一个圆圈 现在我有这样的事情 c
  • QuickDraw 中的 python3 递归动画

    我有一个文本文件 其中包含行星及其相应的卫星 卫星以及它们的轨道半径和周期 我想用它来创建动画quickdraw类似于下面的 文本文件如下 RootObject Sun Object Sun Satellites Mercury Venus