2013-04-16 2 views
1

나는 직선을 그리는 무작위로 생성 된 점 또는 "도트"(코드에서 언급 한)가되는 경계선 역할을하는 여러 수직선이있는 파이썬 프로그램을 만들려고합니다. 임의의 각도에서 선. 직선이 수직 "경계"중 하나와 교차하는 경우 색상을 변경하고 싶습니다. 나는 나의 상황을 좀더 분명하게 설명 할 수있는 달성하려는 그림을 가지고있다. 아래에 게시 한 코드에는 "수직 경계"가 그려져 있으며 지역 내에서 무작위로 생성 된 점이 있습니다. 그러나 그것이 내가 붙어있는 곳입니다. 내가 달성하는 것을 목표로하고 무엇무작위로 지향 된 선이 파이썬의 임의의 점에서 그려 짐

:

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

도울 수있는 누군가를 위해 사전에 감사합니다.

+0

는 점을 그릴 후에는 선을 그릴 수 있습니다

enter image description here

이 프로그램은 콘솔로, 지금까지 떨어 핀을 기반으로 PI (π)에 대한 실행 평가를 출력 . 임의의 방향을 만들 수 있습니다. 임의의 x 및 y 위치를 만든 것처럼 거북이를 그 방향으로 돌리고 거기로 이동합니다. 색상을 파악하려면 도트 위치의 x 좌표와 끝선 위치 만 고려하면됩니다. 평행선 중 하나가 x 좌표와 교차하면 색상을 변경해야합니다. 선 중 하나를 교차하는지 확인할 수 있습니다 (예 : parallel_lines()에서 생성 된 x 위치를 저장하고 x 교차점을 확인합니다. –

답변

1

다음은 OP가 설명하는 프로그램의 구현입니다. 라인의 교차점이 있다면, 그것은 사용 파이썬 3 거북이의 선을 제거하고 다른 색상을 다시 그리기 기능을 취소 :

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를 추정하기 위해 방정식을 돌리거나 실제 (또는 가상) 핀을 떨어 뜨릴 수 있습니다.

... 
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