Python繪製五角星等能一筆畫出的角星

來源:互聯網
上載者:User

標籤:函數   十六   方法   整數   忘記   alt   賦值   分享   oop   

只有奇數和4的整數倍的角星才能一筆畫出,見程式和注釋:

import mathimport turtle as t#這種方法只能畫只在尖角處轉向就可以畫出的角星,\# 即可以用等長線段(線段長度為不轉彎的最大)不提筆畫完的角星\#如注釋吊28,29行,則偶數角數變為原來2倍,證明此方法只能畫奇數和4的整數倍的角星\#即只有奇數和4的整數倍的角星才能一筆畫出def loop(r,n):    t.fd(ol(r,n))    t.right(180-360/(2*n))def ol(r,n):    na = (360/(4*n)) / 180 * math.pi    ol = r * math.cos(na) * 2    ol = int(round(ol))    return oldef relocation(r,n):    t.penup()    t.left(90)    t.fd(r)    t.right(180 - 360/(4*n))    t.pendown()def main(r,n):    times=n    if n in (1,2,3,4,8):        print(‘無法一筆畫出{0:.0f}角星‘.format(times))        exit()    if n%2==0:        n/=2        if n%2==1:            print(‘無法一筆畫出{0:.0f}角星‘.format(times))            exit()    relocation(r,n)    for i in range(times):        loop(r,n)    t.penup()    t.goto(0,0)    t.seth(0)    t.pendown()r=200n=eval(input(‘請輸入要繪製的角星的角數n:‘)) #eval()函數很重要,不要總忘記加!!!t.pensize(8)  #注意t.pensize(20)不是t.pensize=20,變數賦值採用=,屬性更改用t.pensize(20)t.color(‘green‘)t.speed(10)t.setup(450,450)main(r,n)t.done()

五角星和十六角星:

Python繪製五角星等能一筆畫出的角星

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.