如何在Python中用乌龟创建镶嵌

时间:2019-04-14 15:37:01

标签: python turtle-graphics tessellation

我正在尝试使用看起来像此图像的python上的海龟图形创建菱形细分模式:

http://www.supercoloring.com/sites/default/files/styles/coloring_medium/public/cif/2015/01/tessellation-with-rhombus-coloring-pages.png

我考虑过先创建六边形图案,然后再将六边形分成三份。我不确定如何递归创建六边形图案。到目前为止,我在运行程序时只改变了乌龟的角度,我没有明确的策略。有关如何解决此问题的任何建议?

到目前为止,我在中心用3只海龟创建了3个六边形,并用于循环绘制围绕3个六边形的六边形。但是,当我循环执行该程序时,海龟会追溯到与以前相同的路径,并且需要一段时间才能绘制其他路径。

到目前为止,这是我的代码:

import turtle
t = turtle.Turtle()
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.left(120)
t2.left(240)
for i in (t, t1, t2):
    i.speed(0)

def hex():
    for b in (t, t1, t2):
        for i in range(6):
             b.forward(100)
             b.right(60)


for i in range(3):
    t.left(120)
hex()
def rep():   
    for i in range(3):
        for a in (t, t1, t2):
            a.forward(100)
            a.right(60)
            for i in range(6):
                a.forward(100)
                a.left(60)
rep()

def rep2():
    for a in (t, t1, t2):
        for i in range(3):
            a.left(120)
            a.forward(100)
            a.right(60)
            rep()

    a.right(120)
    a.forward(100)
    a.right(60)
    rep()
rep2()

2 个答案:

答案 0 :(得分:0)

有很多画图。我将根据菱形形状进行绘制,因为它将允许您用不同的颜色填充它们。为了能够填充每个菱形,需要分别绘制它。该图包含三种不同的菱形形状(它们是同一菱形,但方向不同)。 我将画第一行和第二行。之后是第一行和第二行的重复。这是代码:

<GridViewColumn Header="{x:Static p:Resources.Quantite}" Width="50">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <WrapPanel>
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding Converter="{StaticResource :displayQteSurQteTot}">
                            <Binding Path="Quantite" />
                            <Binding Path="TotalQuantity" />
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </WrapPanel>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

答案 1 :(得分:0)

首先,让我们将三个乌龟,三个功能的六边形细分简化为单个乌龟,单个递归函数解决方案:

from turtle import Screen, Turtle

OUTER_RADIUS = 100
INNER_RADIUS = 3**0.5 * OUTER_RADIUS / 2
SIDES = 6
EXTENT = 360 / SIDES

def tessellation(depth):
    turtle.right(EXTENT/2)

    for _ in range(SIDES):
        turtle.circle(OUTER_RADIUS, EXTENT, 1)

        if depth:
            heading = turtle.heading()

            turtle.right(90)
            tessellation(depth - 1)

            turtle.setheading(heading)

screen = Screen()
turtle = Turtle(visible=False)

screen.tracer(False)  # because I have no patience

turtle.penup()
turtle.goto(-OUTER_RADIUS / 2, -INNER_RADIUS)
turtle.pendown()
tessellation(2)

screen.tracer(True)

screen.exitonclick()

enter image description here

(增加depth参数以填充窗口。)您真正想要的细分是这些图案中的四个(而不是三分之一)彼此重叠。保持我们的初始代码不变:

screen = Screen()
turtle = Turtle(visible=False)

screen.tracer(False)  # because I have no patience

turtle.penup()
turtle.color('blue')
turtle.goto(OUTER_RADIUS / 4, -1 * INNER_RADIUS / 2)
turtle.pendown()
turtle.setheading(0)
tessellation(2)

turtle.penup()
turtle.color('red')
turtle.goto(-OUTER_RADIUS / 2, -2 * INNER_RADIUS / 2)
turtle.pendown()
turtle.setheading(0)
tessellation(2)

turtle.penup()
turtle.color('yellow')
turtle.goto(OUTER_RADIUS / 4, -3 * INNER_RADIUS / 2)
turtle.pendown()
turtle.setheading(0)
tessellation(2)

turtle.penup()
turtle.color('green')
turtle.goto(-OUTER_RADIUS / 2, -4 * INNER_RADIUS / 2)
turtle.pendown()
turtle.setheading(0)
tessellation(2)

screen.tracer(True)

screen.exitonclick()

enter image description here