-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare_road.py
177 lines (153 loc) · 7.97 KB
/
square_road.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import numpy as np
import random
direction_dic = {'vertical' : 0, 'horizontal' : 1}
turn_dic = {'forward' : 0, 'turn' : 1}
turn_to_dir_dic = {0 : 0, 1 : 1, 2 : 0, 3 : 1}
mvt_with_dir_dic = {0 : (0,1), 1 : (1,0), 2 : (0,-1), 3 : (-1,0)}
class Square_road: ## pas de test pour init, set et get, qui sont reprises de road et qui fonctionnent
def __init__(self, size_road, turn=0, direction=0, init_pos=(0, 0),
size_matrix=(360, 360)):
self._size_matrix = size_matrix
self.map = np.zeros(self._size_matrix)
self._size_road = size_road
self.turn = turn
self.direction = direction
if self.direction == 0:
self.pos_y = init_pos[0] + self._size_road + 2
self.pos_x = init_pos[1] + 1
else :
self.pos_y = init_pos[0] + 1
self.pos_x = init_pos[1] + self._size_road + 2
## on met des 2 sur les bords pour que la voiture s'arrête
for k in range(self._size_matrix[0]):
self.map[k][0] = 2
self.map[k][-1] = 2
for k in range(self._size_matrix[1]):
self.map[0][k] = 2
self.map[-1][k] = 2
self.path = [[self.pos_y, self.pos_x,self.turn]]
self.number_of_turn = 0
#init pour demarer way_to_go
for j in range(self.pos_x,self.pos_x + 4*self._size_road + 1):
for i in range(self.pos_y - self._size_road,self.pos_y + self._size_road):
self.map[i][j] = 1
self.path.append([[i, j, self.turn]])
self.map[self.pos_y + self._size_road][j] = 2
self.map[self.pos_y - self._size_road - 1][j] = 2
self.pos_x += 4*self._size_road
def get_road(self):
return self.map
def show_road(self):
print(self.map)
def draw_road_segment(self):
if self.direction == 0:
for k in range(self._size_road + 1):
self.map[self.pos_y + k][self.pos_x] = 1
self.map[self.pos_y - k][self.pos_x] = 1
self.map[self.pos_y + self._size_road][self.pos_x] = 2
self.map[self.pos_y - self._size_road - 1][self.pos_x] = 2
else:
self.map[self.pos_y][self.pos_x - self._size_road : self.pos_x + self._size_road + 1] = 1
self.map[self.pos_y][self.pos_x + self._size_road + 1] = 2
self.map[self.pos_y][self.pos_x - self._size_road - 1] = 2
def draw_turn(self):
if self.number_of_turn == 1:
for k in range(self.pos_x - self._size_road, self.pos_x + self._size_road + 1):
self.map[self.pos_y - 2*self._size_road - 1][k] = 2
for k in range(self.pos_y - 2*self._size_road - 2, self.pos_y + 1):
self.map[k][self.pos_x + self._size_road + 1] = 2
for i in range(self.pos_y - 2*self._size_road, self.pos_y + 1):
for j in range(self.pos_x - self._size_road, self.pos_x + self._size_road + 1):
self.map[i][j] = 1
elif self.number_of_turn == 2:
self.map[self.pos_y + self._size_road][self.pos_x : self.pos_x + 2*self._size_road + 2] = 2
for k in range(self.pos_y - self._size_road, self.pos_y + self._size_road + 1):
self.map[k][self.pos_x + 2*self._size_road + 2] = 2
for i in range(self.pos_y - self._size_road, self.pos_y + self._size_road):
for j in range(self.pos_x, self.pos_x + 2*self._size_road + 2):
self.map[i][j] = 1
elif self.number_of_turn == 3:
self.map[self.pos_y + 2 * self._size_road + 1][self.pos_x - self._size_road - 1: self.pos_x + self._size_road + 1] = 2
for i in range(self.pos_y, self.pos_y + 2 * self._size_road + 2):
self.map[i][self.pos_x - self._size_road - 1] = 2
for j in range(self.pos_x - self._size_road, self.pos_x + self._size_road+ 1):
for i in range(self.pos_y, self.pos_y + 2 * self._size_road + 1):
self.map[i][j] = 1
else:
self.map[self.pos_y - self._size_road - 1][self.pos_x - 2 * self._size_road - 2 : self.pos_x] = 2
self.map[self.pos_y - self._size_road - 1: self.pos_y + self._size_road][self.pos_x - 2 * self._size_road - 2] = 2
self.map[self.pos_y - self._size_road: self.pos_y + self._size_road][self.pos_x - 2 * self._size_road - 1 : self.pos_x] = 1
def add_segment(self,new_pos):
self.pos_y = new_pos[0]
self.pos_x = new_pos[1]
##if self.turn == 0:
self.path.append([self.pos_y, self.pos_x,self.turn])
'''else :
if self.number_of_turn == 1:
for k in range(1,self._size_road + 1):
self.path.append([self.pos_y, self.pos_x - k,self.turn])
self.path.append([self.pos_y - k, self.pos_x,self.turn])
self.path.append([self.pos_y - self._size_road, self.pos_x,self.turn])
elif self.number_of_turn == 2:
for k in range(1,self._size_road + 1):
self.path.append([self.pos_y, self.pos_x - k,self.turn])
self.path.append([self.pos_y + k, self.pos_x,self.turn])
self.path.append([self.pos_y, self.pos_x + self._size_road,self.turn])
elif self.number_of_turn == 3:
for k in range(1,self._size_road + 1):
self.path.append([self.pos_y, self.pos_x + k,self.turn])
self.path.append([self.pos_y + k, self.pos_x,self.turn])
self.path.append([self.pos_y + self._size_road, self.pos_x,self.turn])
else:
for k in range(1,self._size_road + 1):
self.path.append([self.pos_y, self.pos_x + k,self.turn])
self.path.append([self.pos_y + k, self.pos_x,self.turn])
self.path.append([self.pos_y, self.pos_x - self._size_road,self.turn])
#self.draw_turn()'''
def way_to_go(self):
if self.direction == 0:
if self.number_of_turn == 0:
if self.pos_x + 2*self._size_road + 3 - self._size_matrix[1] == 0:
self.turn = 1
else:
x = random.random()
if x > 0.9:
self.turn = 1
elif self.number_of_turn == 2:
if self.pos_x == self.path[0][1] + 2*self._size_road + 2:
self.turn = 1
else:
if self.number_of_turn == 1:
distance = self.pos_y - [item for item in self.path if item[-1] == 1][-1][0]
if self.pos_y + 2*self._size_road + 7 - self._size_matrix[0] == 0:
self.turn = 1
elif abs(distance) > 4 * self._size_road:
x = random.random()
if x > 0.9:
self.turn = 1
def next_slot(self):
self.way_to_go()
if self.turn == 1:
self.number_of_turn +=1
if self.number_of_turn == 1:
new_pos = (self.pos_y + self._size_road,self.pos_x + self._size_road + 1)
elif self.number_of_turn == 2:
new_pos = (self.pos_y + self._size_road + 1,self.pos_x - self._size_road - 1)
elif self.number_of_turn == 3:
new_pos = (self.pos_y - self._size_road - 1,self.pos_x - self._size_road - 1)
elif self.number_of_turn == 3 and self.pos_y == self.path[0][0] + self._size_road :
self.number_of_turn = 4
new_pos = (self.pos_y,self.pos_x)
else:
new_pos = (self.pos_y + mvt_with_dir_dic[self.number_of_turn][0],self.pos_x + mvt_with_dir_dic[self.number_of_turn][1])
self.add_segment(new_pos)
if self.turn == 1:
self.draw_turn()
else:
self.draw_road_segment()
def continuous_road(self):
while self.number_of_turn != 4:
self.next_slot()
if self.number_of_turn < 4:
self.direction = turn_to_dir_dic[self.number_of_turn]
self.turn = 0