-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathexercise_2.py
52 lines (41 loc) · 1.25 KB
/
exercise_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#graphical program to trace random walk in 2 dimensions
import math as m
from random import random
from graphics import *
from button import Button
def main():
n = 100
win = GraphWin("Random Walk", 1200, 1200)
win.setCoords(-100, -100, 100, 100)
newWalk = Button(win, Point(-80, -80), 30, 20, "Random Walk")
quitButton = Button(win, Point(80, -80), 30, 20, "Quit")
newWalk.activate()
pt = win.getMouse()
point = Point(0, 0)
while not quitButton.clicked(pt):
newWalk.deactivate()
randWalk(n, win, point)
newWalk.activate()
quitButton.activate()
pt = win.getMouse()
while not (newWalk.clicked(pt) or quitButton.clicked(pt)):
pt = win.getMouse()
point = Point((2 * random() - 1) * 80, (2 * random() - 1) * 80)
win.close()
def randWalk(n, win, point):
point2 = point
for i in range (n):
x = point2.getX()
y = point2.getY()
point1 = simOneStep(x, y)
point1.draw(win)
line = Line(point2, point1)
line.draw(win)
point2 = point1
def simOneStep(x, y):
angle = random() * 2 * m.pi
x = x + m.cos(angle)
y = y + m.sin(angle)
point = Point(x, y)
return point
if __name__ == '__main__': main()