如何使用 turtle.gotto 打印对称六边形

How to print simmetric hexagons using turtle.gotto

我的任务是在一条线上打印对称的六边形。

示例:

我必须创建一个这样的函数:def hexagone(point, longueur,c): 其中point是X轴和Y轴。 "longueur" 是六边形中心到他任意一个角的距离。我确实使用这个距离来计算和获取我的 turtle.gotto() 的每个 x / y 坐标。 "C"代表六边形各部分的颜色。

到目前为止,我可以完美地在点 0,0 处绘制六边形:

import turtle
from math import pi, sin, cos



def hexagone(point, longueur,c):
   l= point[0] + longueur

   turtle.up()
   turtle.goto(point)
   turtle.color(c[0]) #black
   turtle.down()
   turtle.begin_fill() 
   turtle.goto(l * cos(4 / 3 * pi), l * sin(4 / 3 * pi))
   turtle.goto(l * cos(5 / 3 * pi), l * sin(5 / 3 * pi))
   turtle.goto(l * cos(0), l * sin(0)) 
   turtle.goto(point) 
   turtle.end_fill()



   turtle.color(c[1])  #blue
   turtle.begin_fill()
   turtle.goto(l * cos(0), l * sin(0)) 
   turtle.goto(l * cos(pi / 3), l * sin(pi / 3))
   turtle.goto(l * cos(pi * 2 / 3), l * sin(pi * 2 / 3))
   turtle.goto(point)  
   turtle.end_fill()



   turtle.color(c[2]) #red
   turtle.begin_fill()
   turtle.goto(l * cos(pi * 2 / 3), l * sin(pi * 2 / 3))
   turtle.goto(-l, point[1])
   turtle.goto(l * cos(4 / 3 * pi), l * sin(4 / 3 * pi))
   turtle.goto(point)
   turtle.end_fill()
   turtle.up()




   return True

hexagone((0,0), 50, ("black",("blue"),("red")))
turtle.done()

第一个六边形很完美。我在 origin(0,0) 打印它。 但是,如果我尝试简单地将 point[0]->(x) 增加 100 : 我 += 100 hexagone((i,0), 50, ("black",("blue"),("red"))) 它在六边形上打印六边形,并且越来越大。 我的公式显然有问题,并且缩放比例不正确。 有谁知道我怎样才能完成这项工作以存档示例的结果?

如果你想在 (100,50) 中显示,那么你必须使用 (0,0) 进行所有计算,并且在计算之后你必须将 100 添加到每个 x50 到您在 goto() 中使用的每个 y - 所以您为 (0,0) 创建它,然后将它移动 (100,50)

在代码中我使用 0 而不是 point[0]point[1] 因为我计算 (0,0)

然后我从 point 得到 x,y(这将是 10050) 并在每个 goto()

中使用
goto(... +x, ... +y)

完整代码

import turtle
from math import pi, sin, cos

def hexagone(point, longueur, c):
   l = 0 + longueur # 0 instead of point[0]

   x, y = point

   turtle.up()
   turtle.goto(point)
   turtle.color(c[0]) #black
   turtle.down()
   turtle.begin_fill() 
   turtle.goto(l * cos(4 / 3 * pi )+x, l * sin(4 / 3 * pi)+y)
   turtle.goto(l * cos(5 / 3 * pi)+x, l * sin(5 / 3 * pi)+y)
   turtle.goto(l * cos(0)+x, l * sin(0)+y) 
   turtle.goto(point) 
   turtle.end_fill()

   turtle.color(c[1])  #blue
   turtle.begin_fill()
   turtle.goto(l * cos(0)+x, l * sin(0)+y) 
   turtle.goto(l * cos(pi / 3)+x, l * sin(pi / 3)+y)
   turtle.goto(l * cos(pi * 2 / 3)+x, l * sin(pi * 2 / 3)+y)
   turtle.goto(point)  
   turtle.end_fill()

   turtle.color(c[2]) #red
   turtle.begin_fill()
   turtle.goto(l * cos(pi * 2 / 3)+x, l * sin(pi * 2 / 3)+y)
   turtle.goto(-l+x, 0+y)  # 0 instead of point[1]
   turtle.goto(l * cos(4 / 3 * pi)+x, l * sin(4 / 3 * pi)+y)
   turtle.goto(point)
   turtle.end_fill()
   turtle.up()

   return True

hexagone((0,0), 50, ("black",("blue"),("red")))
hexagone((100,0), 50, ("black",("blue"),("red")))
hexagone((0,100), 50, ("black",("blue"),("red")))
hexagone((100,100), 50, ("black",("blue"),("red")))
turtle.done()

我会采取不同的方法来解决这个问题——因为六边形是由相同形状的三段组成的,将那个形状定义为光标并通过旋转和冲压绘制六边形:

from turtle import Screen, Turtle
from math import pi, cos, sin

LONGUEUR = 25
COLORS = ['red', 'blue', 'black']

def define_shape(longueur):
    polygon = ((0, 0), \
        (longueur * cos(pi * 2 / 3), longueur * sin(pi * 2 / 3)), \
        (- longueur, 0), \
        (longueur * cos(4 / 3 * pi), longueur * sin(4 / 3 * pi)), \
    )

    screen.register_shape('diamond', polygon)

def hexagone(point, colors):
    turtle.setposition(point)

    for color in colors:
        turtle.left(120)
        turtle.color(color)
        turtle.stamp()

screen = Screen()
screen.mode('logo')

define_shape(LONGUEUR)

turtle = Turtle(shape='diamond', visible='False')
turtle.speed('fastest')  # because I have no patience
turtle.penup()

for x in range(-15 * LONGUEUR, 16 * LONGUEUR, 3 * LONGUEUR):
    hexagone((x, 0), COLORS)

screen.exitonclick()

我们可以更进一步,通过定义 复合 形状来制作整个六边形,包括颜色和光标。那么我们只需要在需要的地方移动光标和戳记即可。