海龟式测距仪

2024-10-03 15:22:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经写了一个程序,使用特定的分布创建一个随机行走。例如,如果它是柯西分布,程序将生成一个适当的随机数并将其附加到列表中。然后,海龟将此列表用作随机方向上的步骤列表。现在我想测量每一步中海龟和随机行走开始点(0,0)之间的距离,我似乎找不到一个好的解决方案。这不能用turtle.distance()完成,因为我想画一个依赖于步长计数的距离图。我的第一个想法是得到海龟停下来的每个点的坐标,然后用毕达哥拉斯定理计算距离,但我在路上的某个地方犯了错误,无法获得它们。有人能解释一下原因吗?或者也许有更好的方法

cauchylist = []
for i in randomlist:
    cauchylist.append(0.15*math.tan(((i-0.5)*math.pi)) )
    
Franklin = turtle.Turtle()
u = input("Which walk to walk? C - Cauchy or G - Gauss.")
if u == "C":
    start()
    walk(Franklin, cauchylist)
    cv = turtle.getcanvas()
    cv.postscript(file="walk_cauchy.ps")
    
    Franklin.penup()
    Franklin.fd()
    Franklin.end_poly()

    coordinates = Franklin.get_poly()
    turtle.done()


Tags: 程序距离列表步骤math解决方案方向cv
3条回答

get_poly()将返回一个元组。 例如:

coordinates = turtle.get_poly()
coordinates = ((0.00,0.00), (100.00,0.00), (128.19,10.26), (136.87,59.50))

您可以使用for循环轻松访问每个元组

for c in coordinates:
    print(c)

输出:

(0.00,0.00)
(100.00,0.00)
(128.19,10.26)
(136.87,59.50)

I would like to measure the distance between the turtle in each step and point (0,0) where the random walk is starting and I can't seem to find a good solution for that. It cannot be done with turtle.distance()

这个前提不成立。我们可以做turtle.distance(0, 0),这不会影响海龟的状态,但会给我们到原点的距离。为了演示,我使用distance()对@PatrickArtner的示例进行了修改:

from turtle import Screen, Turtle
from random import seed, randint

seed(42)  # repeatable randomness for demonstration purposes

screen = Screen()
turtle = Turtle()

distances = []

for _ in range(20):
    turtle.forward(randint(-50, 50))
    turtle.left(randint(-180, 180))
    distances.append(turtle.distance(0, 0))

print(distances)

screen.exitonclick()

控制台输出

> python3 test.py
[31.0, 68.97157492789178, 87.25113481192517, 98.48389701852807, 134.17622966161073,
 141.40760908816227, 138.90181656585844, 128.7642376522535, 113.79561063931855,
 108.70118700467431, 66.5351678460713, 87.4088872825077, 113.65616399911758,
 115.22672747425486, 122.12225694530927, 128.35176588895283, 157.57222689310848,
 128.33399580245668, 129.3846600939952, 153.87822281203412]
>

I cannot use t.forward and t.left commands because I get an error "invalid command name . canvas turtle". I think it's because my turtle walk is defined by function looking like that:

def walk(turtle, steps):
     x = 0
     y = 0
     for i in steps:
         kat = random.random() * 360
         turtle.setheading(kat)
         turtle.forward(math.fabs(i))
         bounds(turtle)

However, if I don't use it as on the example You gave, I create list full of only last distance value. Can anything be done about that?

我们需要看到更多的代码来解决这个问题,例如bounds()的定义等等

海龟当前位置到原点的距离是通过将posi值平方、相加并从中得到平方根来计算的:

from turtle import *
from math import sqrt
from random import seed, choice

seed(42) # ensure same randoms for demonstation purposes

def calc_dist(t):
    return sqrt(sum(map(lambda x:x*x, t.pos())))

t = Turtle()

dists = []

for _ in range(20):
    t.forward(choice(range(100))-50)
    t.left(choice(range(360))-180)
    dists.append(calc_dist(t))

print(dists)

输出:

[31.0, 68.97157492789178, 87.25113481192517, 98.48389701852805, 134.17622966161073, 
 141.40760908816227, 138.90181656585844, 128.76423765225354, 113.79561063931857, 
 108.70118700467432, 66.53516784607132, 87.40888728250773, 113.65616399911761, 
 115.22672747425487, 122.12225694530929, 128.35176588895283, 157.57222689310848, 
 128.33399580245668, 129.3846600939952, 153.87822281203412]

在海龟每次移动后sqrt(sum(map(lambda x:x*x, t.pos())))进行计算,并将结果存储在列表中

tutle seeded path

相关问题 更多 >