-
Notifications
You must be signed in to change notification settings - Fork 1
/
bat_visualize.py
72 lines (61 loc) · 2.51 KB
/
bat_visualize.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
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 31 14:54:41 2016
@author: Visualize_Bat
"""
import math
import time
import random
from Tkinter import *
import matplotlib
matplotlib.use('TkAgg')
import pylab
class bat_visualization(object):
def __init__(self, sidelength):
self.top = Tk()
self.sidelength = sidelength
self.canvas = Canvas(self.top, bg="white", height = sidelength, width = sidelength)
self.canvas.pack()
def arraytotile(self, indexx, indexy, length): #assume a square
ll = self.sidelength
a = ll/length*indexx
b = ll/length*(indexx+1)
c = ll - ll/length*indexy
d = ll - ll/length*(indexy+1)
return a, c, b, d
def createcanvas(self, A):
for i in range(A.shape[0]):
for j in range(A.shape[1]):
sl = A.shape[0]
coord = self.arraytotile(i, j, sl)
color = "white"
if A[i,j] < 0.5:
color = "white"
else:
color = "black"
self.canvas.create_rectangle(coord, fill = color )
def batoval(self, batpos, cavelength, radius):
scale = self.sidelength/cavelength
a = batpos.get_x()*scale - radius*scale
b = self.sidelength - batpos.get_y()*scale - radius*scale
c = batpos.get_x()*scale + radius*scale
d = self.sidelength - batpos.get_y()*scale + radius*scale
return a, b, c, d
def fovarc(self, batpos, cavelength, sense_range):
scale = self.sidelength/cavelength
a = batpos.get_x()*scale - sense_range*scale
b = self.sidelength - batpos.get_y()*scale - sense_range*scale
c = batpos.get_x()*scale + sense_range*scale
d = self.sidelength - batpos.get_y()*scale + sense_range*scale
return a, b, c, d
def update(self, A, batcoord, cavelength, radius, direction, fov, srange, delay = 0.1):
self.createcanvas(A)
coord = self.batoval(batcoord, cavelength, radius)
fov_coord = self.fovarc(batcoord, cavelength, srange)
self.canvas.create_oval(coord, fill="red")
self.canvas.create_arc(fov_coord, start=direction-fov/2, extent=fov, fill="blue", stipple="gray25")
self.top.update()
time.sleep(delay)
def done(self):
"Indicate that the animation is done so that we allow the user to close the window."
self.top.mainloop()