def triangulatePolygon(poly, hole=None): # Triangulate poly with hole cdt = CDT(poly.points) if hole: cdt.add_hole(hole) triangles = cdt.triangulate() # Frustratingly, CDT sometimes returns points that are not # EXACTLY the same as the input points, so we use a KDTree valid_points = [shapes.Point(p.x, p.y) for p in poly.points] if hole: valid_points += [shapes.Point(p.x, p.y) for p in hole] tree = sp.KDTree(toNumpy(valid_points)) def convert(t): def findClosest(point): idx = tree.query(toNumpy([point]))[1] return valid_points[idx] A = findClosest(shapes.Point(t.a.x, t.a.y)) B = findClosest(shapes.Point(t.b.x, t.b.y)) C = findClosest(shapes.Point(t.c.x, t.c.y)) return shapes.Triangle(A, B, C) return map(convert, triangles)
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 convert(t): def findClosest(point): idx = tree.query(toNumpy([point]))[1] return valid_points[idx] A = findClosest(shapes.Point(t.a.x, t.a.y)) B = findClosest(shapes.Point(t.b.x, t.b.y)) C = findClosest(shapes.Point(t.c.x, t.c.y)) return shapes.Triangle(A, B, C)
def selectHandle(): for i, p in enumerate(self.__selectedShape.getHandles()): if shapes.pointInHandle(shapes.Point(self.x(), self.y()), p): self.__handleIndex = i self.__dragStartX, self.__dragStartY = coordsToTileEdges( self.x(), self.y(), self.getController().mapTileSize()) break
def get_tracking_data(tracking_data_path, img_shape): x_dim = img_shape[1] y_dim = img_shape[0] frames, x_arr, y_arr = np.loadtxt(tracking_data_path).T positions = [shapes.Point(x*x_dim, y*y_dim, int(f)) for f, x, y in zip(frames, x_arr, y_arr)] return [ll_to_ul(pos, x_dim) for pos in positions]
def triangulatePoints(points): points = toNumpy(points) triangulation = sp.Delaunay(points) triangles = [] for i in range(len(triangulation.simplices)): verts = map(lambda p: shapes.Point(p[0], p[1]), points[triangulation.simplices[i, :]]) triangle = shapes.Triangle(verts[0], verts[1], verts[2]) triangles.append(triangle) return triangles
def ll_to_ul(point, img_size_x): """ change coordinate system from lower left (ll) to upper left (ul) """ coord = point.y x_axis = img_size_x/2 if coord < x_axis: new_cord = coord + abs(2*(coord-x_axis)) else: new_cord = coord - abs(2*(coord-x_axis)) return shapes.Point(point.x, new_cord, point.frame)
def rect_vs_circle(rect, circle): circle_dist = shapes.Point() circle_dist.x = abs(circle.x - rect.x - rect.width / 2) circle_dist.y = abs(circle.y - rect.y - rect.height / 2) if (circle_dist.x >= (rect.width / 2 + circle.radius)): return False if (circle_dist.y >= (rect.height / 2 + circle.radius)): return False if (circle_dist.x < (rect.width / 2)): return True if (circle_dist.y < (rect.height / 2)): return True corner_dist_sq = (circle_dist.x - rect.width / 2)**2 + (circle_dist.y - rect.height / 2)**2 return (corner_dist_sq < (circle.radius**2))
def mouseButtonPress(self, button, time): if self.__drawing: controller = self.getController() action = undo.ShapeAddAction(controller, self.__circle) controller.addUndoAction(action) controller.addShape(self.__circle) self.__circle = shapes.Circle(int(controller.mapTileSize() / 2), int(controller.mapTileSize() / 2)) self.__drawing = False else: if button == 1: self.__circle.setCenter(shapes.Point(self.x(), self.y())) self.__drawing = True
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 selectShape(self, x, y): """ @type x: int @param x: x-coordinate to test @type y: int @param y: y-coordinate to test @rtype: shapes.Shape @return: the shape selected, or None """ shapeList = self.getController().getShapes() for shape in shapeList: if shape.intersects(shapes.Point(x, y)): return shape return None
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 __parseCircle(self, circle): c = shapes.Circle(0.0) if "center" in circle: c.setCenter( shapes.Point(circle["center"]["x"], circle["center"]["y"])) else: log.error("Circle specified with no center") if "radius" in circle: c.setRadius(circle["radius"]) else: log.error("Circle specified with no radius") if "damage" in circle: c.damage = circle["damage"] if "friction" in circle: c.friction = circle["friction"] if "restitution" in circle: c.restitution = circle["restitution"] return c
def __init__(self, fps=FRAMES_PER_SECOND): game.Game.__init__(self, fps) mouse.set_visible(False) # Create the ship and place it in the center of the screen: center = shapes.Point(self.width / 2, self.height / 2) self.ship = shapes.Ship(center, SHIP_INITIAL_ROTATION, SHIP_COLOR) # Create bullet and upgrade lists: self.bullets = [] self.upgrades = [] # Create asteroids and background stars: self.asteroids = [] self.spawn_asteroids() self.stars = [] while len(self.stars) < (self.width * STAR_DENSITY): self.stars.append(shapes.Star(self.get_random_point())) # Initialize mixer and start looping background music: mixer.init() mixer.music.load(BACKGROUND_MUSIC) mixer.music.play(-1)
def get_random_point(self): random_point = shapes.Point(int(random.uniform(0, self.width - 1)), int(random.uniform(0, self.height - 1))) return random_point
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 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)
import shapes p = shapes.Point() print(p) # <shapes.Point instance at 0x7fb58c31ccb0>
import shapes p1 = shapes.Point(2, 3) print(p1.x) # 2 print(p1.y) # 3 p1.move(4, 5) print(p1.x) # 6 print(p1.y) # 8
def rect_within_circle(rect, circle): return point_within_circle(shapes.Point(rect.x, rect.y), circle) and \ point_within_circle(shapes.Point(rect.x + rect.width, rect.y), circle) and \ point_within_circle(shapes.Point(rect.x + rect.width, rect.y + rect.height), circle) and \ point_within_circle(shapes.Point(rect.x, rect.y + rect.height), circle)