def create_refresh_button(): button_area = shapes.Rectangle(0, 0, 30, 30) button = rendering.Button(button_area) points = [] dy = None y = 0 for angle in range(0, 270, 15): point_outer = (-math.sin(math.radians(angle)) * 9, -math.cos(math.radians(angle)) * 9) point_inner = (-math.sin(math.radians(angle)) * 7, -math.cos(math.radians(angle)) * 7) points.append(point_outer) points.insert(0, point_inner) if dy is None: dy = (point_outer[1] - point_inner[1]) y = (point_outer[1] + point_inner[1]) / 2 polygon_points = [(5, y), (0, y + 5), (0, y - 5)] arrow = shapes.Polygon(polygon_points) open_circle = shapes.Polygon(points) shape_graphic = rendering.SimpleMonoColouredGraphic( open_circle, colours.GREEN) arrow_graphic = rendering.SimpleMonoColouredGraphic(arrow, colours.GREEN) old_pos = (open_circle.left, open_circle.down) button.add_and_center_canvas(shape_graphic) new_pos = (open_circle.left, open_circle.down) arrow.translate([x - y for x, y in zip(new_pos, old_pos)]) button.add_canvas(arrow_graphic) return button
def mouseButtonPress(self, button, time): controller = self.getController() if button == 1: p = coordsToTileEdges(self.x(), self.y(), controller.mapTileSize()) if self.__drawing == True: last = self.__polygon.getLastPoint() if last is not None and self.x() == last.x and self.y() == last.y \ and self.__polygon.convex(): self.__polygon.delExtraPoint() self.__polygon.forceCClockwise() action = undo.ShapeAddAction(controller, self.__polygon) controller.addUndoAction(action) controller.addShape(self.__polygon) self.__polygon = shapes.Polygon() self.__drawing = False else: self.__polygon.addPoint(shapes.Point(self.x(), self.y())) else: self.__polygon.addPoint(shapes.Point(self.x(), self.y())) self.__drawing = True elif button == 3: if self.__drawing: if self.__polygon.convex(): self.__drawing = False self.__polygon.forceCClockwise() action = undo.ShapeAddAction(controller, self.__polygon) controller.addUndoAction(action) controller.addShape(self.__polygon) self.__polygon = shapes.Polygon() return True
def __init__(self): gtk.DrawingArea.__init__(self) self.__checkerBoard = graphics.getCheckerPattern(16) self.shapes = [] vPoly = shapes.Polygon([ shapes.Point(10, 10), shapes.Point(10, 86), shapes.Point(40, 86), shapes.Point(86, 10), ]) iPoly = shapes.Polygon([ shapes.Point(106, 10), shapes.Point(96, 86), shapes.Point(140, 86), shapes.Point(120, 50), shapes.Point(160, 10), ]) circle = shapes.Circle(35) circle.setCenter(shapes.Point(200, 48)) self.shapes.append(vPoly) self.shapes.append(iPoly) self.shapes.append(circle) self.set_size_request(256, 96)
def create_play_button(): button_area = shapes.Rectangle(0, 0, 30, 30) button_icon = shapes.Polygon([(6, 0), (-6, -10), (-6, 10)]) # self.button_graphic_background = rendering.SimpleMonoColouredGraphic(button_rect_background, ) button_graphic_icon = rendering.SimpleMonoColouredGraphic( button_icon, colours.GREEN) button = rendering.Button(button_area) button.add_and_center_canvas(button_graphic_icon) return button
def __init__(self, controller, maxX, maxY): ShapeTool.__init__(self, controller, maxX, maxY) # True if a shape is in the middle of being drawn self.__drawing = False self.__polygon = shapes.Polygon() # These keep the number of redraws down self.__lastX = 0 self.__lastY = 0 self.setInstructions( "Left click to add points, right click to finish.") self.setSnapInterval(int(self.getController().mapTileSize() // 2))
def create_time_warp_button(increase): button = rendering.RectangularButton((30, 30)) c = 1 if increase else -1 for dx in [-5, 5]: button_icon = shapes.Polygon([(3 * c, 0), (-3 * c, -10), (-3 * c, 10)]) # self.button_graphic_background = rendering.SimpleMonoColouredGraphic(button_rect_background, ) button_graphic = rendering.SimpleMonoColouredGraphic( button_icon, colours.GREEN) button.add_and_center_canvas(button_graphic) button_graphic.translate((dx, 0)) return button
def get_visibility_polygon(self, current_position, current_rotation, num_rays, visibility_angle): # c = coord.Coord(self.x, self.y) vis_points = [int(current_position.get_x()), int(current_position.get_y())] rotation = current_rotation - visibility_angle offset = (visibility_angle * 2.0)/num_rays bbox = self.get_bbox(current_position) hits = list(self.__rtree_idx.intersection(bbox.get_rtree_bbox(), objects=True)) polygon_hits = [item.object for item in hits] # polygon_ids = [item.id for item in hits] # print('Polygons considered:', polygon_ids) nearby_polygons = polygon_hits + [bbox] + [self.__boundary_polygon] while rotation < current_rotation + visibility_angle: rotation_x = math.cos(coord.Coord.to_radians(-rotation)) rotation_y = math.sin(coord.Coord.to_radians(-rotation)) r = coord.Coord(current_position.get_x() + rotation_x, current_position.get_y() + rotation_y) rotation += offset if r.get_x() < 0 or r.get_x() > self.__width or r.get_y() < 0 or r.get_y() > self.__height: vis_points.append(int(current_position.get_x())) vis_points.append(int(current_position.get_y())) continue ray = shapes.Line(current_position, r) # print('ray:', ray) closest_intersect = None for polygon in nearby_polygons: # print('polygon:',polygon) for i in range(polygon.get_num_lines()): intersect = shapes.Line.get_intersection(ray, polygon.get_line(i)) if not intersect: continue if not closest_intersect or intersect[1] < closest_intersect[1]: closest_intersect = intersect if not closest_intersect: print('Closest intersect not found') print('From coordinate:', current_position) print('Ray:', ray) print('Segment:', polygon.get_line(i)) continue vis_points.append(int(closest_intersect[0].get_x())) vis_points.append(int(closest_intersect[0].get_y())) vis_points_tuple = tuple(vis_points) visibility_polygon = shapes.Polygon(vis_points_tuple) return visibility_polygon
def __parsePolygon(self, polygon): p = shapes.Polygon() p.delExtraPoint() if "points" in polygon: for point in polygon["points"]: p.addPoint(shapes.Point(point["x"], point["y"])) else: log.error("Polygon specified with no points") if "damage" in polygon: p.damage = polygon["damage"] if "friction" in polygon: p.friction = polygon["friction"] if "restitution" in polygon: p.restitution = polygon["restitution"] return p
def readShapesFromJson(data): shapes_list = [] if 'Figures' not in data.keys(): return [] palette = data['Palette'] i = 1 for shape in data['Figures']: if (validate.validateShape(shape)): color = '' if 'color' in shape: if validate.validateColor(shape['color'], palette): color = shape['color'] if shape['type'] == 'point': shapes_list.append(shapes.Point(shape['x'], shape['y'])) elif shape['type'] == 'polygon': points = [] for point in shape['points']: points.append(shapes.Point(point[0], point[1])) shapes_list.append(shapes.Polygon(points, color)) elif shape['type'] == 'rectangle': point = shapes.Point(shape['x'], shape['y']) rect = shapes.Rectangle(point, shape['width'], shape['height'], color) shapes_list.append(rect) elif shape['type'] == 'square': point = shapes.Point(shape['x'], shape['y']) shapes_list.append( shapes.Square(point, shape.get('size'), color)) elif shape['type'] == 'circle': point = shapes.Point(shape['x'], shape['y']) shapes_list.append( shapes.Circle(point, shape.get('radius'), color)) else: print('Wrong type in Figure number', i, 'in file.') else: print('Wrong parameters in Figure number', i, 'in file.') i = i + 1 return shapes_list
def create_obstacle_polygon(vertices): o = Obstacle(partial(distance_to_polygon, vertices=vertices), partial(dir_from_polygon, vertices=vertices), shapes.Polygon(vertices)) return o
def convexHull(points): points = toNumpy(points) verts = sp.ConvexHull(points).vertices hull = map(lambda idx: shapes.Point(points[idx, 0], points[idx, 1]), verts) return shapes.Polygon(hull)
def __init__(self, parent): tk.Frame.__init__(self, parent) self.__Graph = tk.Canvas(self, width=Graph.WIDTH, height=Graph.HEIGHT, bg='white') self.__Graph.pack() self.pack() self.__Parent = parent # Axes of graph in the middle self.__SceneChange = True self.__CordsSys = [ myMatrix.Vector3D(0.0, 0.0, 0.0), # Origin myMatrix.Vector3D(1, 0.0, 0.0), # X myMatrix.Vector3D(0.0, 1, 0.0), # Y myMatrix.Vector3D(0.0, 0.0, 1) # Z ] self.__GraphCube = [ myMatrix.Vector3D(-1, 1, -1), myMatrix.Vector3D(1, 1, -1), myMatrix.Vector3D(1, -1, -1), myMatrix.Vector3D(-1, -1, -1), myMatrix.Vector3D(-1, 1, 1), myMatrix.Vector3D(1, 1, 1), myMatrix.Vector3D(1, -1, 1), myMatrix.Vector3D(-1, -1, 1) ] self.__AxisNumbersPosition = [ [ [ myMatrix.Vector3D(-0.8, -1, -1), myMatrix.Vector3D(-0.8, -1, 1) ], [ myMatrix.Vector3D(-0.6, -1, -1), myMatrix.Vector3D(-0.6, -1, 1) ], [ myMatrix.Vector3D(-0.4, -1, -1), myMatrix.Vector3D(-0.4, -1, 1) ], [ myMatrix.Vector3D(-0.2, -1, -1), myMatrix.Vector3D(-0.2, -1, 1) ], [myMatrix.Vector3D(0, -1, -1), myMatrix.Vector3D(0, -1, 1)], [ myMatrix.Vector3D(0.2, -1, -1), myMatrix.Vector3D(0.2, -1, 1) ], [ myMatrix.Vector3D(0.4, -1, -1), myMatrix.Vector3D(0.4, -1, 1) ], [ myMatrix.Vector3D(0.6, -1, -1), myMatrix.Vector3D(0.6, -1, 1) ], [ myMatrix.Vector3D(0.8, -1, -1), myMatrix.Vector3D(0.8, -1, 1) ], ], [ [ myMatrix.Vector3D(-1, -0.8, -1), myMatrix.Vector3D(-1, -0.8, 1) ], [ myMatrix.Vector3D(-1, -0.6, -1), myMatrix.Vector3D(-1, -0.6, 1) ], [ myMatrix.Vector3D(-1, -0.4, -1), myMatrix.Vector3D(-1, -0.4, 1) ], [ myMatrix.Vector3D(-1, -0.2, -1), myMatrix.Vector3D(-1, -0.2, 1) ], [myMatrix.Vector3D(-1, 0, -1), myMatrix.Vector3D(-1, 0, 1)], [ myMatrix.Vector3D(-1, 0.2, -1), myMatrix.Vector3D(-1, 0.2, 1) ], [ myMatrix.Vector3D(-1, 0.4, -1), myMatrix.Vector3D(-1, 0.4, 1) ], [ myMatrix.Vector3D(-1, 0.6, -1), myMatrix.Vector3D(-1, 0.6, 1) ], [ myMatrix.Vector3D(-1, 0.8, -1), myMatrix.Vector3D(-1, 0.8, 1) ], ], [ [ myMatrix.Vector3D(1, -1, -0.8), myMatrix.Vector3D(-1, -1, -0.8) ], [ myMatrix.Vector3D(1, -1, -0.6), myMatrix.Vector3D(-1, -1, -0.6) ], [ myMatrix.Vector3D(1, -1, -0.4), myMatrix.Vector3D(-1, -1, -0.4) ], [ myMatrix.Vector3D(1, -1, -0.2), myMatrix.Vector3D(-1, -1, -0.2) ], [myMatrix.Vector3D(1, -1, 0), myMatrix.Vector3D(-1, -1, 0)], [ myMatrix.Vector3D(1, -1, 0.2), myMatrix.Vector3D(-1, -1, 0.2) ], [ myMatrix.Vector3D(1, -1, 0.4), myMatrix.Vector3D(-1, -1, 0.4) ], [ myMatrix.Vector3D(1, -1, 0.6), myMatrix.Vector3D(-1, -1, 0.6) ], [ myMatrix.Vector3D(1, -1, 0.8), myMatrix.Vector3D(-1, -1, 0.8) ], ], ] self.__AxisEdges = [ [[2, 3], [6, 7], [0, 1], [4, 5]], [[0, 3], [4, 7], [1, 2], [5, 6]], [ [6, 2], [3, 7], [1, 5], [0, 4], ], ] self.__FacesMid = [ myMatrix.Vector3D(0.0, 0.0, -1.0), myMatrix.Vector3D(1.0, 0.0, 0.0), myMatrix.Vector3D(0.0, 0.0, 1.0), myMatrix.Vector3D(-1.0, 0.0, 0.0), myMatrix.Vector3D(0.0, 1.0, 0.0), myMatrix.Vector3D(0.0, -1.0, 0.0), ] #self.dotz = myMatrix.Vector3D(1, 0, 0) self.__Points = [ shapes.Point(5, -1.0, 1.0), shapes.Point( 4, -1.0, 1.0, ), shapes.Point(3, -1.0, 1.0), shapes.Point(2, -1.0, 1.0), shapes.Point(1, -1.0, 1.0, '#00ff00'), shapes.Point(0.0, -1.0, 1.0), shapes.Point(-1, -1.0, 1.0), shapes.Point(-2, -1.0, 1.0, '0000ff'), shapes.Point(-3, -1.0, 1.0), shapes.Point(-4, -1.0, 1.0), shapes.Point(-5, -1.0, 1.0), ] self.Polygons = [shapes.Polygon(0, 0, 0, 2, -1, 2, 15, 15, 15)] self.PolyGonePeaks = [ myMatrix.Vector3D(0, 0, 0), myMatrix.Vector3D(0, 1, 0), myMatrix.Vector3D(-2, 1, 0), myMatrix.Vector3D(-1, 0, 0), myMatrix.Vector3D(-1, 0, -1), myMatrix.Vector3D(-1, 2, -1), myMatrix.Vector3D(0, 1, -1), myMatrix.Vector3D(0, 0, -1), ] self.PoygonEdges = [ [0, 3], [0, 1], [0, 7], [1, 2], [3, 2], [3, 4], [4, 5], [4, 7], [5, 6], [5, 2], [6, 7], [6, 1], ] # Steny kociek s cislami vrcholov self.__CubeFaces = [[0, 1, 2, 3], [1, 5, 6, 2], [5, 4, 7, 6], [4, 0, 3, 7], [0, 4, 5, 1], [3, 2, 6, 7]] #self.__Points = [ # myMatrix.Vector3D(1.0, 1, 1), #] self.__Show = [0, 0, 0] self.__LabelCords = [] # Uhly ktore sa pouzivaju pri rotaciach self.__RotationMatX = myMatrix.Matrix(4, 4) self.__RotationMatY = myMatrix.Matrix(4, 4) self.__RotationMatZ = myMatrix.Matrix(4, 4) self.__Rot = myMatrix.Matrix(4, 4) self.__Angles = [15.0, 45.0, 0.0] self.rotationUpdate() # Translation matrix, na posun kamery self.m_TranslationCam = [0.0, 0.0, 0.0] # Zvacsovnaie scaleX = 0.5 scaleY = 0.5 scaleZ = 0.5 self.__Scale = myMatrix.Matrix(4, 4) self.__Scale[(0, 0)] = scaleX self.__Scale[(1, 1)] = scaleY self.__Scale[(2, 2)] = scaleZ self.__Tsf = myMatrix.Matrix(4, 4) # self.Tr = myMatrix.Matrix(4, 4) # self.Tr[(0, 3)] = 2 self.__InnerTranslation = [0, 0, 0] self.__InnerTsf = myMatrix.Matrix(4, 4) self.__InnerTsf[(0, 0)] = 1 self.__InnerTsf[(1, 1)] = 1 self.__InnerTsf[(2, 2)] = 1 self.__InnerTsf[(0, 3)] = self.__InnerTranslation[0] self.__InnerTsf[(1, 3)] = self.__InnerTranslation[1] self.__InnerTsf[(2, 3)] = self.__InnerTranslation[2] self.__BaseScaledNumber = [] self.m_OutterStartPosition = [0, 0, 0] for i in range(3): self.__BaseScaledNumber.append([]) lastNum = -0.8 for j in range(9): self.__BaseScaledNumber[i].append( round(lastNum - self.m_OutterStartPosition[i] * 0.8, 2) / self.__InnerTsf[i, i]) lastNum += 0.2 self.__Projection = myMatrix.Matrix(4, 4) # self.__Projection[(2, 3)] = -1.0 # self.__Projection[(3, 3)] = 0.0 # self.__Projection[(3, 2)] = -0.5 fov = 90.0 # between 30 and 90 ? zfar = 100.0 znear = 0.1 S = 1 / (math.tan(math.radians(fov / 2))) # 1st version (Perspective __Projection) self.__Projection[(0, 0)] = S self.__Projection[(1, 1)] = S self.__Projection[(2, 2)] = -zfar / (zfar - znear) self.__Projection[(3, 2)] = -0.05 # self.Proj[(2, 3)] = -(zfar * znear) / (zfar - znear) self.lctrl_pressed = False self.__Graph.bind("<B1-Motion>", self.dragcallback) self.__Graph.bind("<ButtonRelease-1>", self.releasecallback) self.__Graph.bind("<B3-Motion>", self.dragcallbackRight) self.__Graph.bind("<ButtonRelease-3>", self.releasecallbackRight) # self.__Graph.bind_all("<d>", self.move) self.__LabelAngle = [0.0, 0.0, 0.0] self.cnt = Graph.RATE self.cntRight = Graph.RATE self.prevmouseX = 0.0 self.prevmouseY = 0.0 self.previousTmpScale = [ self.__InnerTsf[0, 0], self.__InnerTsf[1, 1], self.__InnerTsf[2, 2] ] self.prevmouseYright = 0.0 self.__ScaleFactor = 1.02 for i in range(3): self.updateAxisNumberPositions(i, 1) self.numerifyAxis() self.outterTransformationMatrixUpdate() time_st = time.time() time.sleep(1) print(time.time() - time_st) self.update()
def __init__(self, map_id): self.__polygons = [] self.__boundary_polygon = None self.__all_polygons = None # Includes all the obstacle polygons as well as boundary polygon self.__map_name = 'id_' + str(map_id) + '.polygons' print('Map name:', self.__map_name) self.__expansion_factor = 2.50 self.__expanded_polygons = [] self.__bbox_length = 250 self.__rtree_idx = rtree.index.Index() print('Path:', self.__map_name) assert(os.path.isfile(self.__map_name)) f = open(self.__map_name, 'r') first = True for line in f: line.strip() if len(line) != 0: if first: points_list = line.split(',') self.__width = int(points_list[0]) self.__height = int(points_list[1]) points_tuple = (0, 0, self.__width, 0, self.__width, self.__height, 0, self.__height) self.__boundary_polygon = shapes.Polygon(points_tuple) offset = 10 points_tuple = (offset, offset, self.__width - offset, offset, self.__width - offset, self.__height - offset, offset, self.__height-offset) self.__imaginary_boundary = shapes.Polygon(points_tuple) first = False else: geometry_type = line.split(':')[0].strip() # print(geometry_type) points_string = line.split(':')[1] points_list = points_string.split(',') # print(points_list) points_list = [int(point) for point in points_list] points_tuple = tuple(points_list) polygon = None if geometry_type == 'polygon': polygon = shapes.Polygon(points_tuple) # print(' ') # print(polygon) # print('Expanded polygon:') e_polygon = shapes.Polygon(points_tuple, self.__expansion_factor) # print(e_polygon) self.__expanded_polygons.append(e_polygon) if geometry_type == 'square': centre = (points_tuple[0], points_tuple[1]) length = points_tuple[2] # print(centre) # print(length) polygon = shapes.Square(centre, length) if geometry_type == 'rectangle': centre = (points_tuple[0], points_tuple[1]) width = points_tuple[2] height = points_tuple[3] # print(centre) # print(length) polygon = shapes.Rectangle(centre, width, height) print(str(polygon)) if geometry_type == 'circle': centre = (points_tuple[0], points_tuple[1]) radius = points_tuple[2] num_approx_points = points_tuple[3] polygon = shapes.Circle(centre, radius, num_approx_points) # print(str(polygon)) self.__rtree_idx.insert(len(self.__polygons), polygon.get_rtree_bbox(), obj=polygon) self.__polygons.append(polygon) self.__num_polygons = len(self.__polygons) self.__all_polygons = self.__polygons + [self.__boundary_polygon]