【问题标题】:How do I generate random dots inside my turtle circle and rectangle?如何在我的乌龟圈和矩形内生成随机点?
【发布时间】:2016-06-07 09:06:32
【问题描述】:

在海龟图形中,我画了一个矩形和一个圆形。如何在每个形状中放置 10 个随机点?这是我的代码:

import turtle
import math
import random 

# draw a rectangle at a specific location
def drawRectangle(x = -75, y = 0, width = 100, height = 100): 
    turtle.penup() # Pull the pen up
    turtle.goto(x + width / 2, y + height / 2)
    turtle.pendown() # Pull the pen down
    turtle.right(90)
    turtle.forward(height)
    turtle.right(90)
    turtle.forward(width)
    turtle.right(90)
    turtle.forward(height)
    turtle.right(90)
    turtle.forward(width)    


# Draw a circle at a specific location
def drawCircle(x = 50, y = 0, radius = 50): 
    turtle.penup() # Pull the pen up
    turtle.goto(x, -50)
    turtle.pendown() # Pull the pen down
    turtle.begin_fill() # Begin to fill color in a shape
    turtle.circle(radius) 

【问题讨论】:

标签: python function random turtle-graphics


【解决方案1】:

在矩形内生成随机点,非常简单。您只需生成一个随机 x 坐标,范围从(在您的情况下为 -75)的原点位置,直到它的结尾,即原点 + 宽度(-75 + 100)。 然后,您将对 y 坐标执行相同的操作。之后,您移动到生成的位置并绘制一个点。

我的代码:

# draw random dots inside of rectangle
# @param origin: is a touple, containing `x` and `y` coordinates
# @param number_of_dots: int, number of dots
# @param size:   is a touple, containing `width` and `height` of rectangle
def draw_random_dots_in_rectangle(origin, number_of_dots, size=RECTANGLE_SIZE):
    # loops number_of_dots times
    for _ in range(number_of_dots):
        # generate a random position inside of given rectangle
        # using min/max, because of possible negative coordinates
        # weakness - does also place dots on the edges of the rectangle
        rand_x = randint(min(origin[0], origin[0] + size[0]), max(origin[0], origin[0] + size[0]))
        rand_y = randint(min(origin[1], origin[1] + size[1]), max(origin[1], origin[1] + size[1]))
        # moves to the random position
        move_turtle_to((rand_x, rand_y))
        # creates a dot
        t.dot(DOT_DIAMETER)

但是,对圆形做同样的事情是不可能的。它要复杂得多,需要了解analytic geometry。在您的情况下,您需要equation of circles。有了它,您可以计算生成的位置是否在给定圆内。

我的代码:

# draw random dot inside of circle
# @param origin: is a touple, containing `x` and `y` coordinates
# @param number_of_dots: int, number of dots
# @param radious: int, radious of circle
def draw_random_dots_in_circle(origin, number_of_dots, radius=CIRCLE_RADIOUS):
    # loops number_of_dots times
    for _ in range(number_of_dots):
        # loops until finds position inside of the circle
        while True:
            # generates random x position
            # subtracting radious and adding double of radious to simulate bounds of square
            # which would be large enought to fit the circle
            rand_x = randint(min(origin[0] - radius, origin[0] + radius * 2),
                             max(origin[0] - radius, origin[0] + radius * 2))
            # generated random y position
            # adding  double of radious to sumulate bounds of square
            # which would be large enought to fit the circle
            rand_y = randint(min(origin[1], origin[1] + radius * 2),
                             max(origin[1], origin[1] + radius * 2))

            # test if the generated position is in the radious
            if (origin[0] - rand_x) ** 2 + (origin[1] + radius - rand_y) ** 2 < radius ** 2:
                # if it is, move to the position
                move_turtle_to((rand_x, rand_y))
                # draw dot
                t.dot(DOT_DIAMETER)
                # break out from the infinite loops
                break

本质上它与以前的过程相同,但具有等式检查。
我希望这至少有一点帮助。我自己曾多次努力弄清楚如何在计算机科学中做某些事情,并且很多次我发现解析几何就是答案。所以我强烈建议至少检查一下。
我的洞码:

#!/usr/bin/env python3

import turtle
from random import randint

RECTANGLE_SIZE = 60, 80
CIRCLE_RADIOUS = 10
DOT_DIAMETER   = 3

t = turtle.Turtle() # turtle object
t.speed(0)          # set the fastest drawing speed

# move turtle to position without drawing
# @param: position is a touple containing `x` and `y` coordinates
def move_turtle_to(position):
    t.up()   # equivalent to .penuo()
    t.goto(position[0], position[1])
    t.down() # equivalent to .pendown()


# draws a rectangle from given origin with given size
# @param origin: is a touple, containing `x` and `y` coordinates
# @param size:   is a touple, containing `width` and `height` of rectangle
def draw_rectangle(origin, size=RECTANGLE_SIZE):
    # movese to the origin
    move_turtle_to(origin)

    # simple way of drawing a rectangle
    for i in range(4):
        t.fd(size[i % 2])
        t.left(90)

# draws a circle from given origin with given radious
# @param origin: is a touple, containing `x` and `y` coordinates
# @param radious: int, radious of circle
def draw_circle(origin, radius=CIRCLE_RADIOUS):
    # moves to the origin
    move_turtle_to(origin)
    # draws the circle
    t.circle(radius)


# Now to what you asked
# draw random dots inside of rectangle
# @param origin: is a touple, containing `x` and `y` coordinates
# @param number_of_dots: int, number of dots
# @param size:   is a touple, containing `width` and `height` of rectangle
def draw_random_dots_in_rectangle(origin, number_of_dots, size=RECTANGLE_SIZE):
    # loops number_of_dots times
    for _ in range(number_of_dots):
        # generate a random position inside of given rectangle
        # using min/max, because of possible negative coordinates
        # weakness - does also place dots on the edges of the rectangle
        rand_x = randint(min(origin[0], origin[0] + size[0]),     max(origin[0], origin[0] + size[0]))
        rand_y = randint(min(origin[1], origin[1] + size[1]),     max(origin[1], origin[1] + size[1]))
        # moves to the random position
        move_turtle_to((rand_x, rand_y))
        # creates a dot
        t.dot(DOT_DIAMETER)

# draw random dot inside of circle
# @param origin: is a touple, containing `x` and `y` coordinates
# @param number_of_dots: int, number of dots
# @param radious: int, radious of circle
def draw_random_dots_in_circle(origin, number_of_dots, radius=CIRCLE_RADIOUS):
    # loops number_of_dots times
    for _ in range(number_of_dots):
        # loops until finds position inside of the circle
        while True:
            # generates random x position
            # subtracting radious and adding double of radious to simulate bounds of square
            # which would be large enought to fit the circle
            rand_x = randint(min(origin[0] - radius, origin[0] + radius * 2),
                             max(origin[0] - radius, origin[0] + radius * 2))
            # generated random y position
            # adding  double of radious to sumulate bounds of square
            # which would be large enought to fit the circle
            rand_y = randint(min(origin[1], origin[1] + radius * 2),
                             max(origin[1], origin[1] + radius * 2))

            # test if the generated position is in the radious
            if (origin[0] - rand_x) ** 2 + (origin[1] + radius - rand_y) ** 2 < radius ** 2:
                # if it is, move to the position
                move_turtle_to((rand_x, rand_y))
                # draw dot
                t.dot(DOT_DIAMETER)
                # break out from the infinite loops
                break


# example code
draw_rectangle((0, 0))

draw_random_dots_in_rectangle((0, 0), 50)

draw_circle((-20, -20))

draw_random_dots_in_circle((-20, -20), 20)

input()

【讨论】:

  • 有没有办法简化你写的代码?该代码看起来比我知道的更高级。我还试图使圆与矩形大小相同,并将它们并排放置。另外,当我播放程序时,它似乎是无限的。每个形状我只需要 10 个点。
  • @tin_man12,我真的没有想到,如何在不更改 assignment 的情况下进行简化。例如如果你想在从 0,0 到 inf,inf 的空间中做这些对象,你可以摆脱那些 min/max-es。所以不要做一次消极的事情。
  • @tin_man12,但代码本身并不真正复杂。就像我在答案中所说的那样,数学很复杂。如果您至少学习一些解析几何,您会发现它并不难。然后,您只需对方程式检查进行编程,就可以开始了。
猜你喜欢
  • 2015-07-04
  • 1970-01-01
  • 2013-05-12
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多