简体   繁体   中英

Turtle Graphics “if touching colour”

I am trying to make a program in Turtle that draws a Christmas Tree and then some baubles, which I want to be placed randomly on the tree. However because a Christmas Tree is an irregular shape I am not able to place the baubles by randomly choosing x and y co-ordinates. Is there a way to randomly place the baubles on the tree? I was considering an "turtle.pendown()" and then "if turtle.pen touching "green"" but I am not sure how to code this. Any help would be greatly appreciated.

One simple, graphic, approach is to:

  1. Find a Python module that has a routine for performing the "point in polygon" inclusion test

  2. Use turtle's begin_poly() , end_poly() , and get_poly() to capture the vertices that your code generates when drawing the tree

  3. Randomly generate ornaments within the bounding box of the tree but also apply the crossing number test to see if their centers are on the tree

Here's an example implementation using an (exceptionally) abstract tree and ornaments:

from turtle import Turtle, Screen
from random import randrange, choice
from point_in_polygon import cn_PnPoly

screen = Screen()

WINDOW_WIDTH, WINDOW_HEIGHT = screen.window_width(), screen.window_height()

COLORS = ["red", "yellow", "gold", "blue", "white", "pink"]

def draw_abstract_tree(turtle):
    width = WINDOW_WIDTH//4

    turtle.penup()
    turtle.goto(0, -WINDOW_HEIGHT//4)
    turtle.pendown()

    for _ in range(8):
        turtle.forward(width)
        turtle.left(150)
        turtle.forward(1.156 * width)
        turtle.right(150)
        width *= 0.9

    turtle.left(210)

    for _ in range(8):
        turtle.forward(1.156 * width)
        turtle.left(150)
        turtle.forward(width)
        turtle.right(150)
        width /= 0.9

    turtle.goto(0, -WINDOW_HEIGHT//4)

    turtle.setheading(0)

def decorate_tree(turtle, polygon):
    turtle.penup()

    for _ in range(1000):
        x = randrange(-WINDOW_WIDTH/4, WINDOW_WIDTH/4)
        y = randrange(-WINDOW_HEIGHT/4, WINDOW_HEIGHT)
        diameter = randrange(1, 12)

        if cn_PnPoly((x, y), polygon):
            turtle.goto(x, y)
            turtle.color(choice(COLORS))
            turtle.dot(diameter)

yertle = Turtle(visible=False)
yertle.speed("fastest")
yertle.color("darkgreen")

yertle.begin_poly()
draw_abstract_tree(yertle)
yertle.end_poly()

polygon = yertle.get_poly()

yertle.begin_fill()
draw_abstract_tree(yertle)
yertle.end_fill()

decorate_tree(yertle, polygon)

screen.exitonclick()

OUTPUT

在此处输入图片说明

I think turtle doesn't have method to check color.

But turtle uses Canvas from tkinter which have function find_overlaping(rectangle) to check if some objects overlaps this rectangle. Maybe it could works. Maybe you could check if there is tree in some small rectange in random place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM