使用Python乌龟堆叠三角形

时间:2018-11-26 12:13:41

标签: python turtle-graphics

我需要在彼此顶部绘制4个颜色不同的三角形。我已经弄清楚了如何将4彼此相邻绘制,但是我没有设法使它们彼此重叠。这是我的代码:

@Injectable()
export class NgbTimeDateAdapter extends NgbTimeAdapter<Date> {
  // This class is used for converting between ngBootstrap timepicker times and JavaScript dates. As we need the date as well as well as the time, we must cache the date when first converting the time to the NgbTimeStruct and reuse it when converting the NgbTimeStruct to a date.
  cachedDate: Date;
  fromModel (value: Date): NgbTimeStruct {
    if (!value) {
      return null;
    }

    this.cachedDate = new Date(value.getTime());
    return {
      hour: value.getHours(),
      minute: value.getMinutes(),
      second: value.getSeconds()
    };
  }
  toModel (time: NgbTimeStruct): Date {
    if (!time) {
      return null;
    }

    const returnDate = this.cachedDate ? new Date(this.cachedDate.getTime()) : new Date();
    returnDate.setHours(time.hour);
    returnDate.setMinutes(time.minute);
    returnDate.setSeconds(time.second);
    returnDate.setMilliseconds(0);
    return returnDate;
  }

  updateCachedDate (offsetDays: number): void {
    // When scrolling the timepicker onto a different day, we need a way to handle it. The component with the datepicker must call this method to adjust the date as appropriate.
    if (this.cachedDate) {
      this.cachedDate.setDate(this.cachedDate.getDate() + offsetDays);
    }
  }
}

奥托(Otto)是我乌龟的名字。 setcolor是我编写的用于分配随机颜色的函数。另外,您能告诉我如何绘制3x3的三角形堆吗?非常感谢。我使用的是jupyter笔记本,因此它与常规Python可能有所不同。可以参考图片here

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

import turtle
import math
from random import randint

otto = turtle.Turtle()

def repeat_triangle(t, l):
    for i in range(3):
        t.color(randint(0,255),randint(0,255),randint(0,255))
        t.begin_fill()
        t.fd(100) 
        t.lt(120)
        t.fd(100)
        t.lt(120)
        t.fd(100)
        t.lt(120)
        t.fd(100)
        #added code starts here
        t.lt(180) #turn 180 (reverse direction)
        t.fd(50) #go halfway
        t.lt(60) #turn downwards and start drawing 
        t.fd(100)
        t.lt(120)
    t.fd(100) #finishing after the loop
    t.lt(120)
    t.fd(100)
    #added code finishes here
    otto.end_fill()

otto.shape('turtle')
repeat_triangle(otto, 80)

turtle.mainloop()
turtle.bye()

答案 1 :(得分:0)

通过标记而不是绘画更好地生活的另一个示例:

from turtle import Screen, Turtle
from random import random

TRIANGLE_EDGE = 100
CURSOR_EDGE = 20

TRIANGLE_HEIGHT = TRIANGLE_EDGE * 3 ** 0.5 / 2

def repeat_triangle(turtle, repetitions):
    for _ in range(repetitions):
        turtle.color(random(), random(), random())
        turtle.stamp()
        turtle.forward(TRIANGLE_HEIGHT)

screen = Screen()

otto = Turtle('triangle', visible=False)
otto.penup()
otto.setheading(90)
otto.shapesize(TRIANGLE_EDGE / CURSOR_EDGE)

repeat_triangle(otto, 4)

screen.mainloop()

enter image description here

此外,此代码可能不正确,具体取决于您使用的是哪种乌龟变体:

t.color(randint(0,255),randint(0,255),randint(0,255))

Python随附的乌龟默认将float设置为0到1之间的数字-如果您想使用0到255之间的int,则必须通过以下方式进行请求:

turtle.colormode(255)

对您的绘图代码进行简单重做以堆积三角形的方法可能是:

from turtle import Screen, Turtle
from random import randint

def repeat_triangle(t, length):
    height = length * 3 ** 0.5 / 2

    for _ in range(4):
        t.color(randint(0, 255), randint(0, 255), randint(0, 255))

        t.begin_fill()

        for _ in range(3):
            t.fd(length)
            t.lt(120)

        t.end_fill()

        t.sety(t.ycor() + height)

screen = Screen()
screen.colormode(255)

otto = Turtle('turtle')
otto.penup()

repeat_triangle(otto, 100)

screen.mainloop()