-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateBirds.py
64 lines (54 loc) · 2.13 KB
/
generateBirds.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
53
54
55
56
57
58
59
60
61
62
63
64
import sys
from random import randint
from math import sqrt
from numpy.random import normal as randCoords
from random import vonmisesvariate as randDir
def main():
# USAGE:
# generateBirds.py <number of centers> <size> <number of birds> <diameter of centers (size of std dev)> <output file name>
try:
numCenters = int(sys.argv[1])
size = int(sys.argv[2])
numBirds = int(sys.argv[3])
diameter = size*0.05
outputFileName = sys.argv[4]
except:
print "USAGE: generateBirds.py <number of centers> <size> <number of birds> <output file name>"
return
centers = []
minDistBetweenCenters = int(size * 0.3)
for c in range(numCenters):
nextCenter = (randint(0, size), randint(0, size), randint(0, size))
while distToClosestCenter(nextCenter, centers, size) < minDistBetweenCenters or closeToBoundary(nextCenter, size):
nextCenter = (randint(0, size), randint(0, size), randint(0, size))
centers.append(nextCenter)
fout = open(outputFileName, "w")
for c in centers:
for b in range(numBirds/numCenters):
# Create position coordinates according to a gaussian distribution around center c
coords = map(int, tuple(randCoords(c, diameter)))
map(lambda x: x % size, coords)
direction = randDir(0,0)
birdInfo = str(coords[0])+","+str(coords[1])+","+str(coords[2])
# Create a random directional vector
for i in range(3):
birdInfo += "," + str(randint(-5, 5))
birdInfo += "\n"
# Print info to file
fout.write(birdInfo)
fout.close()
def distToClosestCenter(nextCenter, centers, size):
minDist = size
for c in centers:
if distance(nextCenter, c) < minDist:
minDist = distance(nextCenter, c)
return minDist
def closeToBoundary(nextCenter, size):
for i in range(3):
if nextCenter[i] < size*0.2 or nextCenter[i] > size*0.8:
return True
return False
def distance(p0, p1):
return sqrt( (p0[0]-p1[0])**2 + (p0[1]-p1[1])**2 )
if __name__ == '__main__':
main()