【问题标题】:Randomly orientated lines drawn off a random point in python从python中的随机点绘制的随机定向线
【发布时间】:2013-04-08 18:08:24
【问题描述】:

我正在尝试创建具有几条垂直线的 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()

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

【问题讨论】:

  • 画完点之后,就得画线了。你可以创建一个随机方向,就像你创建一个随机的 x 和 y 位置一样,将你的海龟转向那个方向并移动到那里。要弄清楚颜色,您只需要考虑点位置和结束线位置的 x 坐标。如果它穿过 x 坐标的平行线之一,那么你应该改变它的颜色。您可以检查您是否越过其中一条线,例如通过存储在 parallel_lines() 中产生的线 x 位置并检查 x 交点。

标签: python random line intersection turtle-graphics


【解决方案1】:

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

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。

程序将基于到目前为止丢弃的引脚输出 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

【讨论】:

    猜你喜欢
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多