如何在八边形内绘制八边形,两个中心位于同一点?

时间:2016-01-31 22:25:48

标签: python turtle-graphics

如何在八边形内绘制八边形,其中两个都使用Python在乌龟的同一点居中? 这就是我到目前为止所做的:

import turtle

for i in range(8):
    turtle.forward(100)
    turtle.right(360/8)

    turtle.pendown()

    turtle.goto(20, -20)


    turtle.penup()


    for i in range(8):
           turtle.forward(40)
           turtle.right(360/8)

3 个答案:

答案 0 :(得分:1)

只需将笔放下,在第一个for循环中追踪第一个五边形,移动第一行距离的一半,然后开始新的小八边形

import turtle

turtle.pendown()
for i in range(8):
    turtle.forward(100)
    turtle.right(360/8)

turtle.forward(50)
for i in range(8):
       turtle.forward(FIGURE OUT WHAT THE NEW DISTANCE IS)
       turtle.right(360/8)

如果重要的是跳到20号,-20号,并以八角形40长度开始那么这将是版本

import turtle

turtle.pendown()
for i in range(8):
    turtle.forward(100)
    turtle.right(360/8)

turtle.penup()
turtle.goto(20, -20)
turtle.pendown()

for i in range(8):
       turtle.forward(40)
       turtle.right(360/8)

答案 1 :(得分:1)

请记住points on a circle由公式

给出
x = r * cos(theta)
y = r * sin(theta)

其中r是半径,theta是从原点延伸到点和x轴的光线所对的角度。八边形的顶点在圆上等间隔。因此,如果我们从0到2 pi弧度中选择等距theta s,那么我们将得到八边形的顶点:

import turtle
from math import pi, cos, sin

def ngon(n, dist, phase, shift=(0,0)):
    theta = 2 * pi / n
    x0, y0 = shift
    turtle.penup()
    x = dist * cos(phase) + x0
    y = dist * sin(phase) + y0
    turtle.goto(x, y)
    turtle.pendown()
    for i in range(1, n+1):
        x = dist * cos(i*theta + phase) + x0
        y = dist * sin(i*theta + phase) + y0
        turtle.goto(x, y)

def demo_ngon():
    turtle.speed(0.1)
    turtle.width(2.0)
    ngon(8, dist=100, phase=0)
    ngon(8, dist=68, phase=0)
    turtle.mainloop()

demo_ngon()

答案 2 :(得分:-1)

import turtle

for i in range(8):
    turtle.forward(100)
    turtle.right(360/8)

turtle.forward(50)
for i in range(8):
       turtle.forward(40)
       turtle.right(360/8)