Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何细分此形状?_Python_Turtle Graphics_Tessellation - Fatal编程技术网

Python 如何细分此形状?

Python 如何细分此形状?,python,turtle-graphics,tessellation,Python,Turtle Graphics,Tessellation,我正在尝试细分以下形状,如下图所示。(我没有足够的声誉来发布图片,所以就在这里。 我正在使用python turtle图形来尝试使每个形状彼此适合,但我不知道如何或何时重新开始 我已经做出了形状,如下所示 import turtle t = turtle.Turtle() t.left(30) t.speed("fastest") turtle.delay(0) counter = 0 t.begin_fill() def setup(length): t.forward(lengt

我正在尝试细分以下形状,如下图所示。(我没有足够的声誉来发布图片,所以就在这里。 我正在使用python turtle图形来尝试使每个形状彼此适合,但我不知道如何或何时重新开始

我已经做出了形状,如下所示

import turtle

t = turtle.Turtle()
t.left(30)
t.speed("fastest")
turtle.delay(0)
counter = 0 
t.begin_fill()
def setup(length):
    t.forward(length)
    t.right(120)
    t.forward(length / 3)
    t.left(60)
    t.forward(length / 3)
    t.left(120)
    t.forward(length)
    t.left(60)
    t.forward(length)
    t.left(120)
    t.forward(length / 3)
    t.left(60)
    t.forward(length / 3)
    t.right(120)
    t.forward(length)
    t.right(60)

while True:
    setup(100)

我不知道从这里到哪里去了,现在的代码只是创建了基本形状。

您可以创建绘制完整图形的函数

def figure(length):
    setup(length)
    setup(length)
    setup(length)
然后你可以画一个
,在画第二个
图之前,使用
penup()
forward()
pendown()
等移动海龟

我尝试了不同的组合,得到了这样的结果

示例1()

示例2()

可能带有一些递归的示例1可以给出预期的结果

import turtle

# --- functions ---

def setup(length):
    t.forward(length)
    t.right(120)
    t.forward(length / 3)
    t.left(60)
    t.forward(length / 3)
    t.left(120)
    t.forward(length)
    t.left(60)
    t.forward(length)
    t.left(120)
    t.forward(length / 3)
    t.left(60)
    t.forward(length / 3)
    t.right(120)
    t.forward(length)
    t.right(60)

def figure(length):
    for _ in range(3):
        setup(length)

def example1(length):

    for _ in range(3):
        figure(length)

        t.penup()
        t.forward(length + length/3)
        t.right(120)
        t.backward(length/3)
        t.pendown()

def example2(length):

    for _ in range(3):
        figure(length)

        t.penup()
        t.left(60)
        t.forward(length + length)
        t.right(60)
        t.pendown()


# --- main ---

t = turtle.Turtle()
t.speed(0)
turtle.delay(0)

t.left(30)

#example1(50)
example2(50)

turtle.mainloop()

编辑:
图中使用递归


这里是思考这个问题的另一种方式。许多细分是更简单的几何瓷砖的变形。这一个可以被认为是变形六边形:

因此,如果我们编写代码以六边形平铺平面,那么我们应该能够使用相同的代码以这种形状平铺平面。基于图章的六边形平铺程序:

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

SIDES = 6
OUTER_RADIUS = 90
INNER_RADIUS = 3**0.5 * OUTER_RADIUS / 2

def tessellation(depth):
    turtle.stamp()

    if depth:
        angle = 0

        while angle < 2 * pi:

            position = turtle.position()

            x = 2 * INNER_RADIUS * cos(angle)
            y = 2 * INNER_RADIUS * sin(angle)

            turtle.goto(turtle.xcor() + x, turtle.ycor() + y)
            tessellation(depth - 1)

            turtle.setposition(position)

            angle += 2 * pi / SIDES

screen = Screen()

turtle = Turtle(visible=False)
turtle.penup()
turtle.sety(-OUTER_RADIUS)  # center point correction!
turtle.begin_poly()
turtle.circle(OUTER_RADIUS, steps=6)
turtle.end_poly()

screen.register_shape('tile', turtle.get_poly())

turtle.shape('tile')
turtle.settiltangle(30)  # orient tile
turtle.fillcolor('white')
turtle.home()
turtle.showturtle()

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

screen.exitonclick()
从海龟导入屏幕,海龟
从数学输入π,sin,cos
边数=6
外半径=90
内半径=3**0.5*外半径/2
def细分(深度):
乌龟邮票()
如果深度:
角度=0
当角度<2*pi时:
位置=海龟。位置()
x=2*内半径*cos(角度)
y=2*内半径*sin(角度)
turtle.goto(turtle.xcor()+x,turtle.ycor()+y)
细分(深度-1)
乌龟。设置位置(位置)
角度+=2*pi/边
screen=screen()
海龟=海龟(可见=假)
乌龟
海龟。sety(-外半径)#中心点修正!
乌龟,开始
海龟圆(外半径,台阶=6)
乌龟
screen.register\u shape('tile',turtle.get\u poly())
龟形('tile'))
乌龟。设置倾斜角度(30)#定向瓷砖
海龟。填充颜色(“白色”)
海龟之家
海龟
screen.tracer(False)#因为我没有耐心
镶嵌(2)
屏幕跟踪(真)
screen.exitonclick()

替换OP设计的一个问题是原点不在中心:

但是我们会在上面贴上绷带,而不是修改绘图代码。让我们修改上面的代码,使用OP的代码来绘制图形:

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

SIDES = 6
OUTER_RADIUS = 90
INNER_RADIUS = 3**0.5 * OUTER_RADIUS / 2

def setup(length):
    turtle.forward(length)
    turtle.right(120)
    turtle.forward(length / 3)
    turtle.left(60)
    turtle.forward(length / 3)
    turtle.left(120)
    turtle.forward(length)
    turtle.left(60)
    turtle.forward(length)
    turtle.left(120)
    turtle.forward(length / 3)
    turtle.left(60)
    turtle.forward(length / 3)
    turtle.right(120)
    turtle.forward(length)
    turtle.right(60)

def figure(length):
    for _ in range(3):
        setup(length)

def tessellation(depth):
    turtle.stamp()

    if depth:
        angle = 0

        while angle < 2 * pi:

            position = turtle.position()

            x = 2 * INNER_RADIUS * cos(angle)
            y = 2 * INNER_RADIUS * sin(angle)

            turtle.goto(turtle.xcor() + x, turtle.ycor() + y)
            tessellation(depth - 1)

            turtle.setposition(position)

            angle += 2 * pi / SIDES

screen = Screen()

turtle = Turtle(visible=False)
turtle.penup()
turtle.goto(OUTER_RADIUS / 9, -2 * OUTER_RADIUS / 9)  # center point correction!
turtle.begin_poly()
figure(INNER_RADIUS)
turtle.end_poly()

screen.register_shape('tile', turtle.get_poly())

turtle.shape('tile')
turtle.settiltangle(30)  # orient tile
turtle.fillcolor('white')
turtle.home()
turtle.showturtle()

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

screen.exitonclick()
从海龟导入屏幕,海龟
从数学输入π,sin,cos
边数=6
外半径=90
内半径=3**0.5*外半径/2
def设置(长度):
乌龟。向前(长度)
乌龟,对(120)
乌龟。向前(长度/3)
乌龟。左(60)
乌龟。向前(长度/3)
乌龟。左(120)
乌龟。向前(长度)
乌龟。左(60)
乌龟。向前(长度)
乌龟。左(120)
乌龟。向前(长度/3)
乌龟。左(60)
乌龟。向前(长度/3)
乌龟,对(120)
乌龟。向前(长度)
乌龟。对(60)
def数字(长度):
对于范围(3)内的uu:
设置(长度)
def细分(深度):
乌龟邮票()
如果深度:
角度=0
当角度<2*pi时:
位置=海龟。位置()
x=2*内半径*cos(角度)
y=2*内半径*sin(角度)
turtle.goto(turtle.xcor()+x,turtle.ycor()+y)
细分(深度-1)
乌龟。设置位置(位置)
角度+=2*pi/边
screen=screen()
海龟=海龟(可见=假)
乌龟
乌龟。转到(外半径/9,-2*外半径/9)#中心点修正!
乌龟,开始
图(内半径)
乌龟
screen.register\u shape('tile',turtle.get\u poly())
龟形('tile'))
乌龟。设置倾斜角度(30)#定向瓷砖
海龟。填充颜色(“白色”)
海龟之家
海龟
screen.tracer(False)#因为我没有耐心
镶嵌(2)
屏幕跟踪(真)
screen.exitonclick()
这为我们提供了修改后的六边形细分:

您反复运行
setup()
,但您应该在下一个图形之前绘制一个图形并改变位置。将一个
setup()、下一个
left()`或
right()
加上一些值和下一个设置()(都不带
而为True
),以查看它如何更改图像。之后,您可以尝试在left()中增加值/右()或者您必须使用
penup/forward/pendown
移动它。使用不同的值进行尝试,直到在正确的位置获得两个数字。
from turtle import Screen, Turtle
from math import pi, sin, cos

SIDES = 6
OUTER_RADIUS = 90
INNER_RADIUS = 3**0.5 * OUTER_RADIUS / 2

def setup(length):
    turtle.forward(length)
    turtle.right(120)
    turtle.forward(length / 3)
    turtle.left(60)
    turtle.forward(length / 3)
    turtle.left(120)
    turtle.forward(length)
    turtle.left(60)
    turtle.forward(length)
    turtle.left(120)
    turtle.forward(length / 3)
    turtle.left(60)
    turtle.forward(length / 3)
    turtle.right(120)
    turtle.forward(length)
    turtle.right(60)

def figure(length):
    for _ in range(3):
        setup(length)

def tessellation(depth):
    turtle.stamp()

    if depth:
        angle = 0

        while angle < 2 * pi:

            position = turtle.position()

            x = 2 * INNER_RADIUS * cos(angle)
            y = 2 * INNER_RADIUS * sin(angle)

            turtle.goto(turtle.xcor() + x, turtle.ycor() + y)
            tessellation(depth - 1)

            turtle.setposition(position)

            angle += 2 * pi / SIDES

screen = Screen()

turtle = Turtle(visible=False)
turtle.penup()
turtle.goto(OUTER_RADIUS / 9, -2 * OUTER_RADIUS / 9)  # center point correction!
turtle.begin_poly()
figure(INNER_RADIUS)
turtle.end_poly()

screen.register_shape('tile', turtle.get_poly())

turtle.shape('tile')
turtle.settiltangle(30)  # orient tile
turtle.fillcolor('white')
turtle.home()
turtle.showturtle()

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

screen.exitonclick()