随机定向的线从python中的随机点绘制

时间:2013-04-16 10:58:55

标签: python random line intersection turtle-graphics

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

我的目标是实现:

Example of program

我目前的代码:

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()

提前感谢任何有帮助的人。

1 个答案:

答案 0 :(得分: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(π)值的一种方法!

了解详情Buffon's needle。要点是引脚穿过线的概率是PI的函数,因此我们可以转动方程式,丢弃实际(或虚拟)引脚,以估算PI。

enter image description here

程序根据目前为止删除的引脚将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