My favorites
▼
|
Sign in
randomwalks
Visualization of a Lévy flight
Project Home
Downloads
Wiki
Issues
Source
Repository:
default
wiki
Checkout
Browse
Changes
Clones
Source path:
hg
/
levy.py
122d7106f2c1
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
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
"""Draws a Levy flight using a Pareto distribution (started with PyGame stars.py example)"""
import random, math, pygame
from pygame.locals import *
#constants
WINSIZE = [800, 800]
WINCENTER = [400, 400]
#DMAX = 240
# Pareto shape parameter
ALPHA = 1.5
def next_point(prev):
"choose next destination"
angle = random.uniform(0,(2*math.pi))
# angle = random.normalvariate(0,1.8)
distance = 2 * random.paretovariate(ALPHA)
# distance = 2 * random.weibullvariate(1.0, 0.9)
# cap distance at DMAX
# if distance > DMAX:
# distance = DMAX
point = [(math.sin(angle) * distance)+prev[0], (math.cos(angle) * distance)+prev[1]]
return point
def main():
random.seed()
clock = pygame.time.Clock()
#initialize and prepare screen
pygame.init()
screen = pygame.display.set_mode(WINSIZE)
pygame.display.set_caption('Levy Flight')
white = 255, 240, 200
black = 20, 20, 40
screen.fill(black)
done = False
p = WINCENTER[:]
while not done:
# add point
q = next_point(p)
# draw line
pygame.draw.line(screen, white, p, q, 1)
p = q
pygame.display.update()
for e in pygame.event.get():
if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
done = True
break
elif e.type == MOUSEBUTTONDOWN and e.button == 1:
# start a new random walk on click
p = list(e.pos)
screen.fill(black)
break
# run up to 90 FPS
clock.tick(90)
# if python says run, then we should run
if __name__ == '__main__':
main()
Show details
Hide details
Change log
057b12976082
by Rob Russell <r...@latenightpc.com> on Jan 30, 2011
Diff
Adding levy.py
Go to:
/levy.py
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 1691 bytes, 63 lines
View raw file
Powered by
Google Project Hosting