Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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 - Fatal编程技术网

使用Python生成重叠的三角形

使用Python生成重叠的三角形,python,turtle-graphics,Python,Turtle Graphics,我正试图使用Python Turtle生成一个特定的模式,但我遇到了一些问题。我已经创建了第一个三角形,但我不确定如何在其中添加第二个三角形并对其着色 我需要的是: 我目前拥有的: 迄今为止的代码: forward(200) left(120) forward(200) left(120) forward(200) right(120) done() 这里有一个例子 import turtle def draw_square(turtle, length): for i in rang

我正试图使用Python Turtle生成一个特定的模式,但我遇到了一些问题。我已经创建了第一个三角形,但我不确定如何在其中添加第二个三角形并对其着色

我需要的是:

我目前拥有的:

迄今为止的代码:

forward(200)
left(120)
forward(200)
left(120)
forward(200)
right(120)
done()
这里有一个例子

import turtle
def draw_square(turtle, length):
    for i in range(4):
        turtle.forward(length)
        turtle.right(90)

def Retat_square(turtle, length,nbr):
    for i in range(nbr):
        draw_square(turtle, length) 
        turtle.right(360/nbr)

def main():
    window = turtle.Screen()
    window.bgcolor("blue")
上面是一个方法,它将绘制一个正方形,下面是一个用turtle类调用该方法的方法

s = turtle.Turtle()
s.shape("turtle")
s.color("yellow")
s.speed()

Retat_square(s,100,30)

我希望这将对您有所帮助

以下是一个基于冲压而非绘图的完整解决方案,这是解决一些海龟问题的更好方法:

import turtle
from operator import add

RED = (1.0, 0.0, 0.0)
GREEN = (0.0, 1.0, 0.0)
SUM = map(add, RED, GREEN)

TRIANGLE_SIZE = 200
BORDER_SIZE = 5

STAMP_UNIT = 20
SQRT_3 = 3 ** 0.5

turtle.shape("triangle")
turtle.hideturtle()
turtle.penup()
turtle.right(30)  # realign triangle
turtle.fillcolor(RED)
turtle.shapesize(TRIANGLE_SIZE / STAMP_UNIT, TRIANGLE_SIZE / STAMP_UNIT, BORDER_SIZE)
turtle.stamp()

turtle.fillcolor(GREEN)
y_offset = TRIANGLE_SIZE * SQRT_3 / 4
turtle.goto(TRIANGLE_SIZE / 4, -y_offset)
turtle.stamp()

turtle.shapesize(TRIANGLE_SIZE / STAMP_UNIT / 2, TRIANGLE_SIZE / STAMP_UNIT / 2, BORDER_SIZE)
turtle.fillcolor(SUM)
turtle.sety(turtle.ycor() + 2 * y_offset / 3)
turtle.stamp()

turtle.exitonclick()
我将讨论颜色混合作为回答

输出


您需要添加代码,而不仅仅是输出。您可以使用and命令,只需将乌龟翻转180度,向前移动100度,然后重新开始绘图。@zehnpaard如何操作?抱歉,尝试了解180度左右的旋转是
右(180)
左(180)
。由于您在
done()
之前已经执行了
right(120)
,因此需要将另一个
right(60)
旋转180度,然后执行
向前(100)
。向左/向右旋转正确的度数(这真的需要你来计算),然后重复你已经绘制的另一个三角形的代码。OP的问题是用不同的颜色填充两个或三个重叠的三角形。这个答案生成了几十个没有填充颜色的正方形。你能更清楚地说明这与OP的问题有什么关系吗?