1

我正在尝试创建具有几条垂直线的python程序,这些垂直线充当随机生成的点或“点”(如代码中所指)以随机程度绘制直线的边界。如果直线与垂直“边界”之一相交,我想让它改变颜色。我有一张我正在努力实现的目标的图片,这可能会更清楚地解释我的情况。我在下面发布的代码绘制了“垂直边界”并在该区域内随机生成了点,但这就是我卡住的地方。

我的目标是:

程序示例

我目前的代码:

setup(750,750)
screen_size = 750
max_coord = (screen_size - 30) / 2
### change the number of dots you have via that variable
num_dots = 500
bgcolor('yellow')
dot_size=5


reset() # Create an empty window 
pi = Turtle()
hideturtle()

def parallel_lines(number):
    pi.pensize(2)
    pi.pencolor('black')
    width = pi.window_width()
    height = pi.window_height()
    pi.setheading(90)
    pi.penup()
    pi.setposition(width/-2, height/-2)
for i in range(1, number +2):
    pi.pendown()
    pi.forward(height)
    pi.penup()
    pi.setposition(width/-2+i*(width/(number+1)),height/-2)
parallel_lines(7)

## centre turtle back in the middle of the page
goto(0,0)

### list to hold the dots
x_coords = []
y_coords = []
### Draw the dots via randomint

penup()
color("blue")
for dot_num in range(num_dots):
    dot_pos_x = randint (-max_coord, max_coord)
    dot_pos_y = randint (-max_coord, max_coord)
    goto(dot_pos_x, dot_pos_y)
    dot(dot_size)
    x_coords.append(dot_pos_x)
    y_coords.append(dot_pos_y)

done()

提前感谢任何可以提供帮助的人。

4

1 回答 1

2

这是OP描述的程序的实现。如果有一条线相交,它会使用 Python 3 turtle 的撤消功能来移除这条线并以备用颜色重新绘制它:

from turtle import Turtle, Screen
from random import randint, randrange

SCREEN_SIZE = 750
PLANK_COUNT = 8
PINHEAD_SIZE = 5

FLOOR_COLOR = "yellow"
DEFAULT_COLOR = "blue"
CROSSING_COLOR = "red"

screen = Screen()
screen.setup(SCREEN_SIZE, SCREEN_SIZE)
screen.bgcolor(FLOOR_COLOR)

# configure numbers to replicate Lazzarini's setup

NUMBER_PINS = 3408
PIN_LENGTH = 78.125
PLANK_WIDTH = screen.window_width() / PLANK_COUNT

def parallel_lines(turtle, width, height):

    turtle.penup()
    turtle.setheading(90)

    turtle.sety(height / -2)

    x_coordinates = []

    for i in range(PLANK_COUNT + 1):
        x = i * PLANK_WIDTH - width / 2

        turtle.setx(x)
        turtle.pendown()
        turtle.forward(height)
        turtle.penup()
        turtle.left(180)

        x_coordinates.append(x)

    return x_coordinates


pi = Turtle(visible=False)
pi.speed("fastest")

x_coordinates = parallel_lines(pi, screen.window_width(), screen.window_height())

def crosses(x0, x1, coordinates):
    for coordinate in coordinates:
        if x0 <= coordinate <= x1 or x1 <= coordinate <= x0:
            return True
    return False

previous_crossings = crossings = 0

max_coord = screen.window_width() / 2

for pin in range(NUMBER_PINS):
    x0, y0 = randint(-max_coord, max_coord), randint(-max_coord, max_coord)

    pi.color(DEFAULT_COLOR)
    pi.goto(x0, y0)
    pi.dot(PINHEAD_SIZE)
    pi.setheading(randrange(360))
    pi.pendown()
    pi.forward(PIN_LENGTH)

    if crosses(x0, pi.xcor(), x_coordinates):
        pi.undo()
        pi.color(CROSSING_COLOR)
        pi.dot(PINHEAD_SIZE)
        pi.forward(PIN_LENGTH)

        crossings += 1

    pi.penup()

    if previous_crossings != crossings:
        estimate = (2 * PIN_LENGTH * pin) / (PLANK_WIDTH * crossings)
        print(estimate)
        previous_crossings = crossings

screen.exitonclick()

现在讲故事的其余部分。 OP 没有提到的是,这是地板上的木板图,我们在上面放针,跟踪地板上有多少交叉线,作为估计 PI 值(π )!

阅读有关布冯的针的详细信息。要点是引脚穿过线的概率是 PI 的函数,因此我们可以将等式反过来,删除实际(或虚拟)引脚,以估计 PI。

在此处输入图像描述

该程序基于到目前为止丢弃的引脚将 PI (π) 的运行估计值输出到控制台:

...
3.121212121212121
3.1215970961887476
3.1370772946859904
3.134418324291742
3.131768953068592
3.1381381381381384
3.1384892086330933
3.1358467983243568
3.1451612903225805
3.1454979129397733
3.1458333333333335
3.1491384432560903
3.1465005931198102
3.1438721136767316
3.144208037825059
3.144542772861357
3.1419316843345113
于 2016-12-11T04:26:51.747 回答