python - 从Python中的随机点绘制随机方向的线

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

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

最佳答案

这是 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

关于python - 从Python中的随机点绘制随机方向的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16035313/

相关文章:

python - 使用 eval 将 json 转换为 dict 是一个不错的选择吗?

objective-c - 尴尬的 arc4random 结果

excel - 在excel中每十行生成两个假 bool 值

c++ - 更改行看起来像一个箭头- c++ builder borland

python - Python 的 os.system() 相当于 cocoa/Objective-C 的什么?

python - 如何理解python函数描述?

python - 类型提示子类返回 self

php -/dev/urandom 错误(网络主机拒绝许可)

regex - 如何将逗号添加到文本文件中一行的最后一个字符

javascript - 如何根据 C3.js/D3.js 图表中的阈值更改线条颜色?