def E_adhesion(vertices, edges, Lambda, L): e = 0. for edge in edges: i1 = edge[0] i2 = edge[1] v1 = vertices[i1] vertex2 = vertices[i2] v2 = v1 + periodic_diff(vertex2, v1, L) dist = euclidean_distance(v1[0], v1[1], v2[0], v2[1]) e += Lambda * dist return e
def E_adhesion(vertices, edges, Lambda , L): e = 0. for edge in edges: i1 = edge[0] i2 = edge[1] v1 = vertices[i1] vertex2 = vertices[i2] v2 = v1 + periodic_diff(vertex2, v1, L) dist = euclidean_distance(v1[0], v1[1], v2[0], v2[1]) e += Lambda * dist return e
def bresenham_fov(origin, target, radius, blocks=None): """ Generate a list of points between origin and target that are visible, using the Bresenham Algorithm. Both origin and target must be a tuple with (point_x, point_y) coordinates. blocks is a function that receives a tuple with (point_x, point_y) coordinates, and returns True if that coordinate blocks vision. False otherwise. Returns a list of points starting at the origin and going until the first coordinate that blocks vision. Implementation based on: http://tech-algorithm.com/articles/drawing-line-using-bresenham-algorithm/ """ visible_points = [] point_x, point_y = origin[0], origin[1] w, h = target[0] - point_x, target[1] - point_y longest = max(abs(w), abs(h)) shortest = min(abs(w), abs(h)) # get the sign (-1, 0, 1) of a value sign = lambda x: 0 if x == 0 else 1 if x > 0 else -1 dx1 = sign(w) dy1 = sign(h) dx2 = 0 if (w == 0 or abs(h) > abs(w)) else (1 if w > 0 else -1) dy2 = 0 if (h == 0 or abs(w) > abs(h)) else (1 if h > 0 else -1) numerator = longest >> 1 for _ in xrange(0, longest + 1): point = (point_x, point_y) visible_points.append(point) if ((blocks and blocks(point)) or euclidean_distance(origin, point) >= radius): return visible_points numerator += shortest if numerator >= longest: numerator -= longest point_x += dx1 point_y += dy1 else: point_x += dx2 point_y += dy2 return visible_points
file1.write(txt) # Write a string in a text file file1.close() # Closes the file associated (it is mandatory) file2 = open(filename, 'r') # Open a text file for input operations. The file must exist. content = file2.read() # Assign all the text from the file in a string variable file2.close() # Closes the file associated (it is mandatory) print(content) ################################## ## EXAMPLES OF EXTERNAL LIBRARY ## ################################## # To be completed ############################/ ## EXAMPLES OF OWN LIBRARY ## ############################/ p1 = geometry.Point(2, -1) p2 = geometry.Point(-2, 2) euc_dist = geometry.euclidean_distance(p1, p2) print(euc_dist)
def calculate_forces(self): """ The force calculations have been joined into a single function for performance reasons. """ forces = {"separation": (0, 0), "alignment": (0, 0), "cohesion": (0, 0), "avoidance": (0, 0), "flight": (0, 0)} self.fleeing = False flight_x, flight_y = 0.0, 0.0 for predator in self.predators: self.fleeing = True distance = self.distance(predator) if distance < PREDATOR_RADIUS: dead = self.die() if dead: return forces factor = 1 - distance / self.world.neighbourhood_radius rel_x, rel_y = self.relative_coordinates(predator.x, predator.y) flight_x -= factor * rel_x flight_y -= factor * rel_y forces["flight"] = (flight_x, flight_y) self.avoiding = False avoidance_x, avoidance_y = 0.0, 0.0 for obstacle in self.world.obstacles: distance = self.shortest_distance(obstacle.x, obstacle.y) if distance < (BOID_RADIUS + self.world.neighbourhood_radius + OBSTACLE_RADIUS): if distance < OBSTACLE_RADIUS: dead = self.die() if dead: return forces normalized_vx, normalized_vy = self.normalized_velocity intersection = False for n in xrange(1, int(distance)): future_x = self.x + n * normalized_vx future_y = self.y + n * normalized_vy d = euclidean_distance((future_x, future_y), obstacle.position) if d < (obstacle.r + BOID_RADIUS): intersection = True break if intersection: self.avoiding = True factor = 1 - (distance - obstacle.r) / self.world.neighbourhood_radius future_x = self.x + distance * normalized_vx future_y = self.y + distance * normalized_vy v1 = self.relative_coordinates(*obstacle.position) v2 = self.relative_coordinates(future_x, future_y) a = angle(v1, v2) if a < 0: factor *= -1 avoidance_x += factor * -normalized_vy avoidance_y += factor * normalized_vx forces["avoidance"] = (avoidance_x, avoidance_y) if len(self.neighbours) == 0: return forces sum_x, sum_y = 0.0, 0.0 sum_vx, sum_vy = 0.0, 0.0 sep_x, sep_y = 0.0, 0.0 for boid in self.neighbours: distance = self.world.distances[self, boid] factor = 1 - distance / self.world.neighbourhood_radius nvx, nvy = boid.normalized_velocity sum_vx += nvx sum_vy += nvy sum_x += boid.x sum_y += boid.y rel_x, rel_y = normalize_vector(*self.relative_coordinates(boid.x, boid.y)) sep_x -= factor * rel_x sep_y -= factor * rel_y forces["separation"] = (sep_x, sep_y) forces["alignment"] = normalize_vector(sum_vx, sum_vy) avg_x = sum_x / len(self.neighbours) avg_y = sum_y / len(self.neighbours) forces["cohesion"] = normalize_vector(*self.relative_coordinates(avg_x, avg_y)) return forces
def shortest_distance(self, x, y): """ The magnitude of the relative coordinate vector. """ return euclidean_distance((0, 0), self.relative_coordinates(x, y))
def velocity_magnitude(self): return euclidean_distance((0, 0), self.velocity)
def calculate_forces(self): """ The force calculations have been joined into a single function for performance reasons. """ forces = { 'separation': (0, 0), 'alignment': (0, 0), 'cohesion': (0, 0), 'avoidance': (0, 0), 'flight': (0, 0) } self.fleeing = False flight_x, flight_y = 0.0, 0.0 for predator in self.predators: self.fleeing = True distance = self.distance(predator) if distance < PREDATOR_RADIUS: dead = self.die() if dead: return forces factor = 1 - distance / self.world.neighbourhood_radius rel_x, rel_y = self.relative_coordinates(predator.x, predator.y) flight_x -= factor * rel_x flight_y -= factor * rel_y forces['flight'] = (flight_x, flight_y) self.avoiding = False avoidance_x, avoidance_y = 0.0, 0.0 for obstacle in self.world.obstacles: distance = self.shortest_distance(obstacle.x, obstacle.y) if distance < (BOID_RADIUS + self.world.neighbourhood_radius + OBSTACLE_RADIUS): if distance < OBSTACLE_RADIUS: dead = self.die() if dead: return forces normalized_vx, normalized_vy = self.normalized_velocity intersection = False for n in xrange(1, int(distance)): future_x = self.x + n * normalized_vx future_y = self.y + n * normalized_vy d = euclidean_distance((future_x, future_y), obstacle.position) if d < (obstacle.r + BOID_RADIUS): intersection = True break if intersection: self.avoiding = True factor = 1 - (distance - obstacle.r) / self.world.neighbourhood_radius future_x = self.x + distance * normalized_vx future_y = self.y + distance * normalized_vy v1 = self.relative_coordinates(*obstacle.position) v2 = self.relative_coordinates(future_x, future_y) a = angle(v1, v2) if a < 0: factor *= -1 avoidance_x += factor * -normalized_vy avoidance_y += factor * normalized_vx forces['avoidance'] = (avoidance_x, avoidance_y) if len(self.neighbours) == 0: return forces sum_x, sum_y = 0.0, 0.0 sum_vx, sum_vy = 0.0, 0.0 sep_x, sep_y = 0.0, 0.0 for boid in self.neighbours: distance = self.world.distances[self, boid] factor = 1 - distance / self.world.neighbourhood_radius nvx, nvy = boid.normalized_velocity sum_vx += nvx sum_vy += nvy sum_x += boid.x sum_y += boid.y rel_x, rel_y = normalize_vector( *self.relative_coordinates(boid.x, boid.y)) sep_x -= factor * rel_x sep_y -= factor * rel_y forces['separation'] = (sep_x, sep_y) forces['alignment'] = normalize_vector(sum_vx, sum_vy) avg_x = sum_x / len(self.neighbours) avg_y = sum_y / len(self.neighbours) forces['cohesion'] = normalize_vector( *self.relative_coordinates(avg_x, avg_y)) return forces