python画螺线_《Think Python》练习4-5:用turtle画螺线
要求:閱讀螺線(spiral)的相關知識; 然后編寫一個繪制阿基米德螺線(或者其他種類的螺線)的程序。
阿基米德螺線
import turtle
from math import sin, cos, pi
bob = turtle.Turtle() # create a Turtle object
bob.delay = 0.01 # 設置延遲值
def spiral(t, a, n, w=6):
''' 畫阿基米德螺線
t: Turtle
a: 每轉w度增加的內徑長度
n: 圈數
w: 精度.默認為6(每次轉過的角度)(角度值)
'''
theta = 0 # 轉過角度初始化
m = int(360 / w) * n # 精度w下轉n圈所需循環次數
#t.lt(w / 2) # 練習4-1第2問
for i in range(m):
dtheta = pi * w/ 180 # 精度變為弧度制
# 勾股定理得到要移動的小距離
length = pow((a*sin(dtheta)*(theta + dtheta))**2 +
(a*cos(dtheta)*(theta + dtheta) - a * theta)**2, 0.5)
#t.lt(w)
t.fd(length)
t.lt(w)
theta += dtheta # 為弧度制
#t.rt(w / 2) #練習4-1第2問
#t.lt(90)
#t.fd(200)
spiral(bob, 5, 3, 6)
turtle.mainloop()
這題參考了 九月初夏 的文章 https://blog.csdn.net/nineth9t/article/details/90322356 還有 4-1的練習 https://blog.csdn.net/nineth9t/article/details/90676590 寫的很明了。
然后這里主要記錄自己在搞這個螺線時的一些疑惑。
因為看了4-1練習的關于停止點偏離思考那篇文章,然后發現里面polyline函數跟螺線的實現函數很像。因為一開始很不明白這個走一小段然后轉一小個角度這是怎么實現畫阿基米德螺線的。但是看了文章后就明白了。
因為在那之前,我的函數是這樣的:
import turtle
def draw_spiral(t, n, w, a=0.05):
'''畫阿基米德螺線
t: Turtle
n: 循環次數(越大圈數越多)
w: 每條弧對應的角度
a: 每轉1度增加的內徑長 (默認值為0.05)
'''
r = 0
b = 0.1
c = 0 # 轉過的角度
for i in range(n):
r = a * c + b
t.circle(r, 1)
c += w
bob = turtle.Turtle()
bob.delay = 0.001
draw_spiral(bob, 1000, 6, 0.05)
思路主要是看了阿基米德螺線的數學關系后得出的:
阿基米德螺線 r=aθ + b
然后在一個小角度內處理成為半徑不變的弧,然后畫出來。
結果就是畫的速度烏龜一樣慢,并且怎么調參數也不會得到作者大大寫的函數的畫的蚊香…只能是松松的蚊香 = =
順便提醒一句要記得 turtle.mainloop() 不然在不是IDLE環境下運行會畫完光速關系窗口了…
然后看了一遍作者的函數代碼。發現沒有用circle這種畫弧的方法ORZ
然后拜讀了 九月初夏 的文章(上面貼的)后才理解了。
ORZ數學不是很好,然后還是沒有明白作者在這里的思路。
歡迎來交流~要是弄出來了,再回來更新。
附作者的代碼:
from __future__ import print_function, division
import turtle
def draw_spiral(t, n, length=3, a=0.1, b=0.0002):
"""Draws an Archimedian spiral starting at the origin.
Args:
n: how many line segments to draw
length: how long each segment is
a: how loose the initial spiral starts out (larger is looser)
b: how loosly coiled the spiral is (larger is looser)
http://en.wikipedia.org/wiki/Spiral
"""
theta = 0.0
for i in range(n):
t.fd(length)
dtheta = 1 / (a + b * theta)
t.lt(dtheta)
theta += dtheta
# create the world and bob
bob = turtle.Turtle()
draw_spiral(bob, 300, 3, 0.1, 0.0002)
turtle.mainloop()
上了stackoverflow看了一下,雖然沒有還是沒有理解上面的。
用平面直角坐標系法畫:
import turtle
from math import cos, sin, pi
def spiral(t, rotations=3, a=0.0, b=5):
theta = 0.0
while theta < rotations * 2 * pi:
radius = a + b * theta
x, y = radius * cos(theta), radius * sin(theta)
turtle.goto(x, y)
theta += 0.1
bob = turtle.Turtle()
spiral(bob)
turtle.mainloop()
然后有因為混著莫名其妙看了4-1的文章,然后就有了以下的莫名其妙關于停止點的偏離的思考。因為一開始我也感覺不出來 arc 函數修改前和修改后會有多大影響…然后看了文章后又對自己的螺線試了一番。
這是沒有用 rt (angle / 2) 和 lt(angle / 2) 的,把之前的代碼這兩行解除注釋來畫條直線來看到停止點與起始點的距離
#t.lt(90)
#t.fd(200)
然后用了rt (angle / 2) 和 lt(angle / 2):
對上去了~大概跟畫字母J的弧對上去是差不多的意思。
然后還有一個疑惑,就是為什么是先前進再轉Δθ呢。
然后就畫了先轉Δθ然后再走的…(注釋了轉一半角的誤差調整下)
emmmmmm跟第一張先走一段距離再轉一個角度比起來竟然有這么多的差別,雖然只是終點偏離的有點遠。螺線結構雖然人眼好像看不出有什么差別就是了ORZ
具體在誤差上有什么差別還沒有去深入探討過。有的話再回來更新。
斐波那契螺線
def FibonacciSpiral(t, n):
'''畫斐波那契螺線
t: Turtle
n: 循環次數
'''
r = a = b = 1
# draw the first half circle
arc(bob, r, 180)
# Draw the 1/4 circle
for i in range(n):
r = a + b # 斐波那契數列
b = a
a = r
arc(bob, r, 90)
閱讀了斐波那契螺線后畫的。
歡迎圍觀和有什么錯誤可以跟我一起交流~
原文鏈接:https://blog.csdn.net/YunoSawano/article/details/105238691
總結
以上是生活随笔為你收集整理的python画螺线_《Think Python》练习4-5:用turtle画螺线的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MediaInfo安装步骤
- 下一篇: 单反相机哪个牌子好?入门单反相机推荐?佳