簡體   English   中英

在多邊形(正多邊形)中繪制多邊形

[英]Draw polygon in polygons (regular polygons)

這是我繪制正多邊形的代碼:

import turtle

tr = turtle.Turtle()

tr.lt(150)
for x in range(3,13):
    for i in range(x):
        tr.fd(80)
        tr.lt(360//x)

turtle.done()

這是我的輸出:實際產量

但我的預期輸出是:預期產出

你能幫助我嗎?

正如評論中已經指出的那樣

您可能想將烏龜移動到多邊形的末端,這樣末端就不會連接

  • @鼠尾

看起來你需要計算半徑偏移量並從中心移入/移出,如果你想讓它們都同心的話。 目前,您總是在同一點開始第一個/最后一個頂點。

  • @CrazyChunky

在快速瀏覽了turtle 方法之后

我想出了這個

import turtle
import math
tr = turtle.Turtle()

r0 = 20
tr.lt(150)
for x in range(3,13):
    points = [
        (r0 * (x-1) * math.cos(k*2*math.pi/x),
         r0 * (x-1) * math.sin(k*2*math.pi/x))
        for k in range(1,x+1)
    ]
    tr.penup() # avoid creating a line connecting two polygons
    tr.goto(*points[-1])
    tr.pendown() # draw one polygon
    for tx, ty in points:
        tr.goto(tx,ty);
turtle.done()

PS.:我以前從來沒有聽說過這個模塊,看到它安裝在我的機器上,我感到很驚訝。

一個簡單的解決方案使用circle()為我們完成我們的工作:

import turtle
from math import pi

for sides in range(3, 13):
    radius = 40 * sides / pi

    turtle.penup()
    turtle.sety(-radius)
    turtle.pendown()

    turtle.circle(radius, steps=sides)

turtle.hideturtle()
turtle.done()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM