def Load(self, handle): pos = SafeLoadLine('GatePos', handle) cornersx = SafeLoadLine('GateX', handle) cornersy = SafeLoadLine('GateY', handle) topx = SafeLoadLine('TopX', handle) topy = SafeLoadLine('TopY', handle) bottomx = SafeLoadLine('BottomX', handle) bottomy = SafeLoadLine('BottomY', handle) self.x = self.env_width * float(pos[0]) self.y = self.env_height * float(pos[1]) self.w = float(pos[2]) self.top[:, 0] = [float(x) for x in topx] self.top[:, 1] = [float(y) for y in topy] self.bottom[:, 0] = [float(x) for x in bottomx] self.bottom[:, 1] = [float(y) for y in bottomy] self.corners[:, 0] = [float(x) for x in cornersx] self.corners[:, 1] = [float(y) for y in cornersy] # apply corrections to make sure the gates are oriented right self.w *= -1 if self.w < 0: self.w = self.w + (np.pi * 2) if self.w > np.pi: self.w -= np.pi self.top = np.squeeze(self.top[np.ix_([2, 3, 0, 1]), :2]) self.bottom = np.squeeze(self.bottom[np.ix_([2, 3, 0, 1]), :2]) self.corners = np.squeeze(self.corners[np.ix_([2, 3, 0, 1]), :2]) self.w -= np.pi / 2 avgtopy = np.mean(self.top[:, 1]) avgbottomy = np.mean(self.bottom[:, 1]) # flip top and bottom if necessary if avgtopy < avgbottomy: tmp = self.top self.top = self.bottom self.bottom = tmp # compute gate height and width # compute other things like polygon p1, p2, p3, p4 = [x[:2] for x in self.corners] self.box = sympy.Polygon(p1, p2, p3, p4) p1, p2, p3, p4 = [x[:2] for x in self.top] self.top_box = sympy.Polygon(p1, p2, p3, p4) p1, p2, p3, p4 = [x[:2] for x in self.bottom] self.bottom_box = sympy.Polygon(p1, p2, p3, p4)
def revisit_geometry(json_data): """ https://cis2020-revisit-geometry.herokuapp.com/instructions Wrapper around revisit_geometry :param json_data: raw json data :rtype: dict """ # parse data shape_coord = json_data["shapeCoordinates"] shape_coord = [(d["x"], d["y"]) for d in shape_coord] line_coord = json_data["lineCoordinates"] line_coord = [(d["x"], d["y"]) for d in line_coord] # sympy logic polygon = sympy.Polygon(*shape_coord) line = sympy.Line(*line_coord) intersections = polygon.intersection(line) json_results = [{ "x": round(float(pt.x), 2), "y": round(float(pt.y), 2) } for pt in intersections] return json_results
def fill(xPoly, yPoly, alpha, d): polyArray = [] x, y = rotate(xPoly, yPoly, alpha) for i in range(len(x)): polyArray.append((x[i], y[i])) poly = sympy.Polygon(*polyArray) reverse = False real = 0 printed = 0 minX = min(x) minY = min(y) maxX = max(x) maxY = max(y) count = (maxY - minY) / d inter = [] xFilled = [] yFilled = [] while printed < count: real += d l = sympy.Line((minX, minY + real), (maxY, minY + real)) inter.append(poly.intersection(l)) for i in range(len(inter[printed]) - 1): tempL = sympy.Segment(inter[printed][i], inter[printed][i + 1]) if poly.encloses_point(tempL.midpoint): if reverse: inter[printed] = np.flip(inter[printed], axis=0) xFilled.append(inter[printed][i][0]) xFilled.append(inter[printed][i + 1][0]) yFilled.append(inter[printed][i][1]) yFilled.append(inter[printed][i + 1][1]) reverse = not reverse print(printed) printed += 1 xFinal, yFinal = rotate(xFilled, yFilled, -1 * alpha) return xFinal, yFinal
def is_inside_arena(pose): p1, p2, p3, p4 = map(sympy.Point, [(-0.9909883, -4.218833), (-1.92709, 0.9022037), (-7.009388, -1.916794), (-4.107592, -7.078834)]) living_room = sympy.Polygon(p1, p2, p3, p4) person_pose = sympy.Point(pose.pose.position.x, pose.pose.position.y) # Inspection test pose return living_room.encloses_point(person_pose)
def Load(self, handle): isdeep = SafeLoadLine('IsDeepTissue', handle) sx = [float(x) for x in SafeLoadLine('SurfaceX', handle)] sy = [float(x) for x in SafeLoadLine('SurfaceY', handle)] self.corners = np.array([sx, sy]).transpose() self.corners[:, 1] = self.env_height - self.corners[:, 1] self.deep = (isdeep[0] == 'true') if not self.deep: self.color = [232. / 255, 146. / 255, 124. / 255] else: self.color = [207. / 255, 69. / 255, 32. / 255] self.poly = sympy.Polygon(*[x[:2] for x in self.corners])
def get_best_route(vertices, vision_radious=0.5, debug=False): plot(vertices) points = list_all_points_inside_polyg(vertices, vision_radious) """Entry point of the program.""" # Instantiate the data problem. data = create_data_model(points) # Create the routing index manager. manager = pywrapcp.RoutingIndexManager(len(data['locations']), data['num_vehicles'], data['depot']) # Create Routing Model. routing = pywrapcp.RoutingModel(manager) distance_matrix = compute_euclidean_distance_matrix( Polygon(vertices), data['locations'], sympy.Polygon(*vertices).is_convex()) def distance_callback(from_index, to_index): """Returns the distance between the two nodes.""" # Convert from routing variable Index to distance matrix NodeIndex. from_node = manager.IndexToNode(from_index) to_node = manager.IndexToNode(to_index) return distance_matrix[from_node][to_node] transit_callback_index = routing.RegisterTransitCallback(distance_callback) # Define cost of each arc. routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index) # Setting first solution heuristic. search_parameters = pywrapcp.DefaultRoutingSearchParameters() search_parameters.first_solution_strategy = ( routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC) # Solve the problem. assignment = routing.SolveWithParameters(search_parameters) # Print solution on console. ordered_points = compute_route(points, manager, routing, assignment) if assignment and debug: print_solution(ordered_points, Polygon(vertices)) return ordered_points
import turtle for i in range(5): turtle.fd(100) turtle.rt(144) ''' 6. Use SymPy to determine the area of a triangle given points a, b and c. ''' import sympy tri = sympy.Polygon(a, b, c) area = tri.area ''' 7. Use VPython to build a 3D snowman. ''' ''' Sources: https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html '''