Skip to content

Commit 850eb03

Browse files
committed
Push toast folder
1 parent ff85f4a commit 850eb03

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

unit_tests/LOS.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
from tkinter import *
2+
import random
3+
4+
root = Tk()
5+
6+
CELL_SIZE = 40
7+
8+
def lineOfSight(start, end, dm):
9+
global CELL_SIZE
10+
if start[1] > end[1]:
11+
lineOfSight(end, start, dm)
12+
elif start[0] == end[0]:
13+
pos = start
14+
tab = [start.copy()]
15+
while pos != end:
16+
pos[1] = pos[1]+dm
17+
if (pos[0]-CELL_SIZE//2)%CELL_SIZE == 0 and (pos[1]-CELL_SIZE//2)%CELL_SIZE == 0:
18+
tab.append(pos.copy())
19+
20+
for i in range(len(tab)):
21+
cell = tab[i]
22+
tab[i] = toastCanvas.create_rectangle(cell[0]-CELL_SIZE//2, cell[1]-CELL_SIZE//2, cell[0]+CELL_SIZE//2, cell[1]+CELL_SIZE//2, fill = 'blue')
23+
toastCanvas.tag_lower(tab[i])
24+
root.update()
25+
26+
elif start[1] == end[1]:
27+
if start[0] > end[0]:
28+
dm = -dm
29+
pos = start
30+
tab = [start.copy()]
31+
while pos != end:
32+
pos[0] = pos[0]+dm
33+
if (pos[0]-CELL_SIZE//2)%CELL_SIZE == 0 and (pos[1]-CELL_SIZE//2)%CELL_SIZE == 0:
34+
tab.append(pos.copy())
35+
36+
for i in range(len(tab)):
37+
cell = tab[i]
38+
tab[i] = toastCanvas.create_rectangle(cell[0]-CELL_SIZE//2, cell[1]-CELL_SIZE//2, cell[0]+CELL_SIZE//2, cell[1]+CELL_SIZE//2, fill = 'blue')
39+
toastCanvas.tag_lower(tab[i])
40+
root.update()
41+
else:
42+
m = (end[1]-start[1])/(end[0]-start[0])
43+
p = start[1] - m * start[0]
44+
posH = start
45+
posB = start
46+
lines = []
47+
tab = [start]
48+
tab2 = []
49+
dx = dm
50+
dy = dm
51+
52+
if m < 0:
53+
m = (start[1]-end[1])/(start[0]-end[0])
54+
dx = -dm
55+
56+
while posH != end:
57+
tmpH = posH
58+
xh = posH[0] + dx
59+
yh = posH[1]
60+
61+
tmpB = posB
62+
xb = posB[0]
63+
yb = posB[1] +dy
64+
65+
if round(m*xh+p, 2) <= yh:
66+
posH = [xh, yh]
67+
else:
68+
posH = [xh-dx, yh+dy]
69+
70+
if m*xb+p < yb:
71+
posB = [xb+dx, yb-dy]
72+
else:
73+
posB = [xb, yb]
74+
75+
if (posH[0]-CELL_SIZE//2)%CELL_SIZE == 0 and (posH[1]-CELL_SIZE//2)%CELL_SIZE == 0:
76+
tab.append(posH)
77+
78+
if (posB[0]-CELL_SIZE//2)%CELL_SIZE == 0 and (posB[1]-CELL_SIZE//2)%CELL_SIZE == 0:
79+
tab.append(posB)
80+
81+
lines.append(toastCanvas.create_line(tmpH[0], tmpH[1], posH[0], posH[1], fill = 'red'))
82+
lines.append(toastCanvas.create_line(tmpB[0], tmpB[1], posB[0], posB[1], fill = 'green'))
83+
root.update()
84+
85+
for i in range(len(tab)):
86+
cell = tab[i]
87+
tab[i] = toastCanvas.create_rectangle(cell[0]-CELL_SIZE//2, cell[1]-CELL_SIZE//2, cell[0]+CELL_SIZE//2, cell[1]+CELL_SIZE//2, fill = 'blue')
88+
toastCanvas.tag_lower(tab[i])
89+
root.update()
90+
91+
def refreshMap():
92+
global CELL_SIZE
93+
toastCanvas.delete(ALL)
94+
for x in range(16):
95+
toastCanvas.create_line(0, x*CELL_SIZE, 16*CELL_SIZE, x*CELL_SIZE)
96+
toastCanvas.create_line(x*CELL_SIZE, 0, x*CELL_SIZE, 16*CELL_SIZE)
97+
98+
def create_lines():
99+
startButton.config(state = DISABLED)
100+
global CELL_SIZE
101+
refreshMap()
102+
pos1 = [random.randint(0,15), random.randint(0,15)]
103+
pos2 = pos1
104+
while pos2 == pos1:
105+
pos2 = [random.randint(0,15), random.randint(0,15)]
106+
107+
print("Line of sight between ", pos1, pos2)
108+
109+
pos1[0] = CELL_SIZE * (pos1[0]+0.5)
110+
pos1[1] = CELL_SIZE * (pos1[1]+0.5)
111+
pos2[0] = CELL_SIZE * (pos2[0]+0.5)
112+
pos2[1] = CELL_SIZE * (pos2[1]+0.5)
113+
114+
toastCanvas.create_line(pos1[0], pos1[1], pos2[0], pos2[1])
115+
lineOfSight(pos1, pos2, CELL_SIZE//2)
116+
startButton.config(state = NORMAL)
117+
118+
toastCanvas = Canvas(root, width = 16*CELL_SIZE, height = 16*CELL_SIZE, bg = 'white')
119+
toastCanvas.pack()
120+
121+
for x in range(16):
122+
toastCanvas.create_line(0, x*CELL_SIZE, 16*CELL_SIZE, x*CELL_SIZE)
123+
toastCanvas.create_line(x*CELL_SIZE, 0, x*CELL_SIZE, 16*CELL_SIZE)
124+
125+
startButton = Button(root, text = 'start', command = create_lines)
126+
startButton.pack()
127+
128+
root.mainloop()

0 commit comments

Comments
 (0)