def display_grid(self, aps, conn, nroads): for n in range(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: # Get all the points in the line self.points[n] = self.get_line(int(x1[0]), int(y1[0]), int(x1[1]), int(y1[1])) 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])) 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 # Create a line object with the x y values of the points in a line self.roads[n] = plot2d.line2d(x1, y1, color='g') plot2d.line(self.roads[n]) for bs in aps: bs.prop = ginp(1)[0] bs_x = round(bs.prop[0], 2) bs_y = round(bs.prop[1], 2) self.scatter = plot2d.scatter(float(bs_x), float(bs_y)) bs.params['position'] = bs_x, bs_y, 0 bs.set_pos_wmediumd(bs.params['position']) plot2d.instantiateNode(bs) plot2d.instantiateAnnotate(bs) plot2d.instantiateCircle(bs) plot2d.text(bs, float(bs_x), float(bs_y)) plot2d.circle(bs, float(bs_x), float(bs_y)) plot2d.draw() sleep(1) if 'src' in conn: for c in range(len(conn['src'])): line = plot2d.line2d([ conn['src'][c].params['position'][0], conn['dst'][c].params['position'][0] ], [ conn['src'][c].params['position'][1], conn['dst'][c].params['position'][1] ], 'b', ls='dashed') plot2d.line(line)
def display_grid(self, aps, conn, nroads): for n in range(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: # Get all the points in the line self.points[n] = self.get_line(int(x1[0]), int(y1[0]), int(x1[1]), int(y1[1])) 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])) 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 # Create a line object with the x y values of the points in a line self.roads[n] = plot2d.line2d(x1, y1, color='g') plot2d.line(self.roads[n]) for bs in aps: bs.prop = ginp(1)[0] bs_x = '%.2f' % bs.prop[0] bs_y = '%.2f' % bs.prop[1] self.scatter = plot2d.scatter(float(bs_x), float(bs_y)) bs.params['position'] = bs_x, bs_y, 0 bs.set_pos_wmediumd(bs.params['position']) plot2d.instantiateNode(bs) plot2d.instantiateAnnotate(bs) plot2d.instantiateCircle(bs) plot2d.text(bs, float(bs_x), float(bs_y)) plot2d.circle(bs, float(bs_x), float(bs_y)) plot2d.draw() sleep(1) if 'src' in conn: for c in range(len(conn['src'])): line = plot2d.line2d([conn['src'][c].params['position'][0], conn['dst'][c].params['position'][0]], \ [conn['src'][c].params['position'][1], conn['dst'][c].params['position'][1]], 'b', ls='dashed') plot2d.line(line)
def simulate_car_movement(self, cars, aps, scatter, com_lines, mobility): # temporal variables points = [[], []] scatter.remove() nodes = cars + aps while com_lines: com_lines[0].remove() del com_lines[0] # iterate over each car for car in cars: # get all the properties of the car vel = round(np.random.uniform(car.speed[0], car.speed[1])) pos_x = car.prop[0] pos_y = car.prop[1] car.params['position'] = pos_x, pos_y, 0 car.set_pos_wmediumd(car.params['position']) angle = car.prop[2] # calculate new position of the car pos_x = pos_x + vel * cos(angle) * self.time_per_iteration pos_y = pos_y + vel * sin(angle) * self.time_per_iteration if (pos_x < car.prop[3] or pos_x > car.prop[4]) \ or (pos_y < car.prop[5] or pos_y > car.prop[6]): self.repeat(car) points[0].append(car.initial[0]) points[1].append(car.initial[1]) else: car.prop[0] = pos_x car.prop[1] = pos_y points[0].append(pos_x) points[1].append(pos_y) for node in nodes: if nodes == car: continue else: # compute to see if vehicle is in range inside = math.pow((node.prop[0] - pos_x), 2) + \ math.pow((node.prop[1] - pos_y), 2) if inside <= math.pow(node.params['range'][0], 2): if isinstance(node, AP): color = 'black' else: color = 'r' line = plot2d.line2d([pos_x, node.prop[0]], [pos_y, node.prop[1]], color=color) com_lines.append(line) plot2d.line(line) plot2d.update(car) plot2d.pause() if not mobility.thread_._keep_alive: exit() scatter = plot2d.scatter(points[0], points[1]) plot2d.draw() return [scatter, com_lines]
def simulate_car_movement(self, cars, aps, scatter, com_lines, mobility): # temporal variables points = [[], []] scatter.remove() nodes = cars + aps while com_lines: com_lines[0].remove() del com_lines[0] while mobility.pause_simulation: pass # iterate over each car for car in cars: # get all the properties of the car vel = round(np.random.uniform(car.speed[0], car.speed[1])) pos_x = car.prop[0] pos_y = car.prop[1] car.position = pos_x, pos_y, 0 car.set_pos_wmediumd(car.position) angle = car.prop[2] # calculate new position of the car pos_x = pos_x + vel * cos(angle) * self.time_per_iteration pos_y = pos_y + vel * sin(angle) * self.time_per_iteration if (pos_x < car.prop[3] or pos_x > car.prop[4]) \ or (pos_y < car.prop[5] or pos_y > car.prop[6]): self.repeat(car) points[0].append(car.initial[0]) points[1].append(car.initial[1]) else: car.prop[0] = pos_x car.prop[1] = pos_y points[0].append(pos_x) points[1].append(pos_y) for node in nodes: if nodes == car: continue else: # compute to see if vehicle is in range inside = math.pow((node.prop[0] - pos_x), 2) + \ math.pow((node.prop[1] - pos_y), 2) if inside <= math.pow(node.wintfs[0].range, 2): if isinstance(node, AP): color = 'black' else: color = 'r' line = plot2d.line2d([pos_x, node.prop[0]], [pos_y, node.prop[1]], color=color) com_lines.append(line) plot2d.line(line) plot2d.update(car) plot2d.pause() if not mobility.thread_._keep_alive: exit() scatter = plot2d.scatter(points[0], points[1]) plot2d.draw() return [scatter, com_lines]