def simulate_car_movement(self, scatter, com_lines): # temporal variables points = [[], []] scatter.remove() nodes = dict(self.cars.items() + self.bss.items()) # print initial while com_lines: com_lines[0].remove() del(com_lines[0]) # iterate over each car for car in self.cars: # get all the properties of the car velocity = round(np.random.uniform(self.velo[car][0], self.velo[car][1])) position_x = self.cars[car][0] position_y = self.cars[car][1] car.params['position'] = position_x, position_y, 0 angle = self.cars[car][2] # calculate new position of the car position_x = position_x + velocity * cos(angle) * self.time_per_iteraiton position_y = position_y + velocity * sin(angle) * self.time_per_iteraiton # check if car gets out of the line # no need to check for y coordinates as car follows the line if position_x < self.cars[car][3] or position_x > self.cars[car][4]: self.repeat(car) points[0].append(self.initial[car][0]) points[1].append(self.initial[car][1]) else: self.cars[car][0] = position_x self.cars[car][1] = position_y points[0].append(position_x) points[1].append(position_y) for node in nodes: if nodes[node] == nodes[car]: continue else: # compute to see if vehicle is in range inside = math.pow((nodes[node][0] - position_x), 2) + math.pow((nodes[node][1] - position_y), 2) if inside <= math.pow(node.params['range'] + 30, 2): line = plot.plotLine2d([position_x, nodes[node][0]], [position_y, nodes[node][1]], color='r') com_lines.append(line) plot.plotLine(line) plot.drawTxt(car) plot.drawCircle(car) scatter = plot.plotScatter(points[0], points[1]) plot.plotDraw() return [scatter, com_lines]
def simulate_car_movement(self,scatter,com_lines): #temporal variables points= [[],[]] scatter.remove() nodes = dict(self.cars.items() + self.bss.items()) #print initial while com_lines: com_lines[0].remove() del(com_lines[0]) #iterate over each car for car in self.cars: #get all the properties of the car velocity = round(np.random.uniform(self.velo[car][0],self.velo[car][1])) position_x = self.cars[car][0] position_y = self.cars[car][1] car.params['position'] = position_x, position_y, 0 angle = self.cars[car][2] #calculate new position of the car position_x = position_x + velocity*cos(angle)*self.time_per_iteraiton position_y = position_y + velocity*sin(angle)*self.time_per_iteraiton #check if car gets out of the line # no need to check for y coordinates as car follows the line if position_x < self.cars[car][3] or position_x> self.cars[car][4]: self.repeat(car) points[0].append(self.initial[car][0]) points[1].append(self.initial[car][1]) else: self.cars[car][0] = position_x self.cars[car][1] = position_y points[0].append(position_x) points[1].append(position_y) for node in nodes: if nodes[node] == nodes[car]: continue else: #compute to see if vehicle is in range inside = math.pow((nodes[node][0]-position_x),2) + math.pow((nodes[node][1]-position_y),2) if inside <= math.pow(node.range+30,2): line = plot.plotLine2d([position_x,nodes[node][0]],[position_y,nodes[node][1]], color='r') com_lines.append(line) plot.plotLine(line) plot.drawTxt(car) plot.drawCircle(car) scatter = plot.plotScatter(points[0],points[1]) plot.plotDraw() return [scatter,com_lines]
def display_grid(self, baseStations, nroads): for n in range(0, nroads): if n == 0: p = ginp(2) self.points[n] = p self.all_points = p else: p = ginp(1) self.points[n] = p self.all_points.append(p[0]) x1 = [x[0] for x in self.points[n]] y1 = [x[1] for x in self.points[n]] if n == 0: self.points[n] = self.get_line( int(x1[0]), int(y1[0]), int(x1[1]), int(y1[1])) # Get all the points in the line else: self.points[n] = self.get_line( int(self.all_points[n][0]), int(self.all_points[n][1]), int(p[0][0]), int(p[0][1])) # Get all the points in the line x1 = [x[0] for x in self.points[n]] y1 = [x[1] for x in self.points[n]] self.interX[n] = x1 self.interY[n] = y1 self.road[n] = plot.plotLine2d( x1, y1, color='g' ) # Create a line object with the x y values of the points in a line plot.plotLine(self.road[n]) #plot.plotDraw() for i in range(len(baseStations)): self.bss[baseStations[i]] = ginp(1)[0] bs_x = self.bss[baseStations[i]][0] bs_y = self.bss[baseStations[i]][1] self.scatter = plot.plotScatter(bs_x, bs_y) baseStations[i].params['position'] = bs_x, bs_y, 0 plot.instantiateAnnotate(baseStations[i]) plot.instantiateCircle(baseStations[i]) plot.drawTxt(baseStations[i]) plot.drawCircle(baseStations[i]) plot.plotDraw()
def display_grid(self, baseStations, nroads): for n in range(0, nroads): if n == 0: p = ginp(2) self.points[n] = p self.all_points = p else: p = ginp(1) self.points[n] = p self.all_points.append(p[0]) x1 = [x[0] for x in self.points[n]] y1 = [x[1] for x in self.points[n]] if n == 0: self.points[n] = self.get_line(int(x1[0]),int(y1[0]),int(x1[1]),int(y1[1])) # Get all the points in the line else: self.points[n] = self.get_line(int(self.all_points[n][0]),int(self.all_points[n][1]),int(p[0][0]),int(p[0][1])) # Get all the points in the line x1 = [x[0] for x in self.points[n]] y1 = [x[1] for x in self.points[n]] self.interX[n] = x1 self.interY[n] = y1 self.road[n] = plot.plotLine2d(x1,y1, color='g') # Create a line object with the x y values of the points in a line plot.plotLine(self.road[n]) #plot.plotDraw() for i in range(len(baseStations)): self.bss[baseStations[i]] = ginp(1)[0] bs_x = self.bss[baseStations[i]][0] bs_y = self.bss[baseStations[i]][1] self.scatter = plot.plotScatter(bs_x, bs_y) baseStations[i].params['position'] = bs_x, bs_y, 0 plot.instantiateAnnotate(baseStations[i]) plot.instantiateCircle(baseStations[i]) plot.drawTxt(baseStations[i]) plot.drawCircle(baseStations[i]) plot.plotDraw()