python - 在 Python 中制作 pi 数字的可视化表示
问题描述
我正在尝试对圆圈内的前一百万位 pi 进行视觉表示。就像在编码培训频道中一样,但使用的是 Python。(https://www.youtube.com/watch?v=WEd_UIKG-uc&index=137&list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH)
它应该看起来像这样:
但它看起来像这样:
首先我导入math
和tkinter
上课,我阅读了文档并制作了画布。我认为这里没有问题:
from math import *
from tkinter import *
openpi = open (r'PI_numbers.txt', 'r')
pi = openpi.read()
win= Tk()
win = Canvas(win, width=500, height=500)
win.configure(background=('black'))
win.create_oval(0,0,500,500, fill='white')
接下来,由于我不知道如何制作开关盒,所以我搜索并找到了一些东西。
def zero(sx,sy):
angle=36*0
x= 250* (1 + cos(angle))
y= 250* (1 + sin(angle))
win.create_line(sx,sy, x, y)
sx=x
sy=y
def one(sx,sy):
angle=36*1
x = 250 * (1 + cos(angle))
y = 250 * (1 + sin(angle))
win.create_line(sx, sy, x, y)
sx = x
sy = y
def two(sx,sy):
angle=36*2
x = 250 * (1 + cos(angle))
y = 250 * (1 + sin(angle))
win.create_line(sx, sy, x, y)
sx = x
sy = y
像这样到九点。进而:
y=250
x=250
options = {
'0': zero(sx=x, sy= y),
'1': one(sx=x, sy= y),
'2': two(sx=x, sy= y),
'3': three(sx=x, sy= y),
'4': four(sx=x, sy= y),
'5': five(sx=x, sy= y),
'6': six(sx=x, sy= y),
'7': seven(sx=x, sy= y),
'8': eight(sx=x, sy= y),
'9': nine(sx=x, sy= y),
".": point()
}
i=0
while len(pi)> i:
n = pi[i]
options [n]
i += 1
win.pack()
win.mainloop()
所以,在这里我试图从最后一个数字到新的数字。我从圆周的中心 250 250 开始。我发现的问题不仅是 x 和 y 错误,而且它总是从中心开始,我不知道为什么。
并且options [n]
我得到这个 : 声明似乎没有效果。
解决方案
我建议使用模块turtle,因为它更容易绘制东西
我非常同意@Superior 的这种观点,但我个人会避免使用数学库,而是让海龟(在 tkinter 上运行)代替所有工作:
from turtle import Turtle, Screen
LARGE_RADIUS = 200
SMALL_RADIUS = 15
def make_pi():
""" from https://stackoverflow.com/a/9005163/5771269 """
q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
while True:
if 4 * q + r - t < m * t:
yield m
q, r, m = 10 * q, 10 * (r - m * t), (10 * (3 * q + r)) // t - 10 * m
else:
q, r, m, t, k, x = k * q, x * (r + 2 * q), (q * (7 * k + 2) + r * x) // (t * x), t * x, k + 1, x + 2
screen = Screen()
screen.mode('logo') # put zero straight up
turtle = Turtle('turtle', visible=False)
turtle.speed('fastest')
turtle.penup()
turtle.sety(LARGE_RADIUS)
turtle.right(90)
positions = []
for index in range(10): # pre calculate all positions on circle
positions.append(turtle.position())
turtle.circle(-LARGE_RADIUS, extent=360 / 10)
turtle.home()
turtle.pendown()
turtle.showturtle()
turtle.speed('slow')
previous_digit = -1 # track digit repetitions
for digit in make_pi():
if digit == previous_digit: # same digit again, circle around
turtle.setheading(turtle.towards(0, 0))
turtle.left(90)
turtle.circle(-SMALL_RADIUS)
else:
position = positions[digit]
turtle.setheading(turtle.towards(position))
turtle.goto(position)
previous_digit = digit
screen.mainloop() # never reached
我没有使用固定大小的 PI_numbers.txt 文件,而是使用@batbaatar在 StackOverflow 上发布的 pi 算法的数字将其设置为永远运行。更换你认为合适的。
我稍微修改了绘图,显示了一个数字何时通过绕回同一个数字来跟随自己:
注意在我们遇到第一个双 7 之前需要多长时间。我已将 turtle 置于 Logo 模式以自然地将 0 置于屏幕顶部。turtle.speed()
您可以在第二次调用时调整动画的速度。
这不是一个合适的海龟程序,因为它是建立在 a 之上的while True:
,应该screen.ontimer()
用来提取每个数字,以便其他事件(如screen.exitonclick()
)可以执行。
推荐阅读
- swift - 无法删除 Firestore 侦听器
- go - 无法从 tls.Conn 获取 fd
- android - 我怎么知道安卓游戏是用 Unity 游戏引擎开发的?
- google-cloud-firestore - 如何使用 gRPC 调用 Firestore
- java - 使用 Maven 运行单元测试
- python - 熊猫间隔不适用于日期时间对象
- password-hash - 在 Java 中设置 Argon2 算法类型
- ssis - 在 SSIS 中使用单个连接在多个表中加载多个 excel 文件
- javascript - (中间值)}.then 不是函数
- python - python pyomo:离散变量的声明