def __init__(self, num_sides, radius): GPolygon.__init__(self) dx = -radius dy = 0 self.addVertex(dx, dy) da = 360 / num_sides edge = math.sin(da / 2 * math.pi / 180) * radius * 2 angle = 0 for i in range(num_sides): self.addPolarEdge(edge, angle) angle -= da
def drawTriangle(edge, dx, dy, whole): tri = GPolygon() x = -edge / 2 y = edge / (2 * math.sqrt(3)) print(x, y) tri.addVertex(x, y) tri.addPolarEdge(edge, 0) tri.addPolarEdge(edge, 120) tri.addPolarEdge(edge, -120) whole.add(tri, dx - edge / 2, dy - edge * (math.sqrt(3) / 2))
def drawTriangle(edge, x, y, whole): tri = GPolygon() tri.addVertex(x, y) tri.addVertex(x + edge, y) tri.addVertex(x + edge / 2, y - edge * math.sqrt(3) / 2) whole.add(tri)
def __init__(self, nSides, radius): ''' Creates a regular polygon with its reference point at the center. The nSides parameter indicates the number of sides and radius parameter indicates the distance from the reference point to any vertex. ''' GPolygon.__init__(self) edge = 2 * radius * math.sin((math.pi / nSides)) d_ang = 360 / nSides self.addVertex(edge / 2, radius) angle = 180 for i in range(nSides): self.addPolarEdge(edge, angle) angle -= d_ang
def __init__(self, nSides, radius): """ Initializes the Regular Polygon of the given perameters. The reference point is the center of the shape. """ GPolygon.__init__(self) self.nSides = nSides self.radius = radius theta = radians(360 / self.nSides) interiorTheta = radians(((self.nSides - 2) * 180) / self.nSides) sideLen = 2 * self.radius * sin(theta / 2) self.addVertex(-sideLen / 2, self.radius * cos(theta / 2)) newTheta = 0 for i in range(self.nSides - 1): self.addPolarEdge(sideLen, degrees(newTheta)) newTheta += (pi - interiorTheta)
def createHexagon(side): hex = GPolygon() hex.addVertex(-side, 0) angle = 60 for i in range(6): hex.addPolarEdge(side, angle) angle -= 60 return hex
def create_hexagon(side): """ Creates a GPolygon representing a regular hexagon with the reference point at the center. """ hex = GPolygon() hex.add_vertex(-side, 0) angle = 60 for i in range(6): hex.add_polar_edge(side, angle) angle -= 60 return hex
def createHexagon(side): """ Creates a GCompound representing a regular hexagon with the specified side length. The reference point is the center. """ hex = GPolygon() hex.addVertex(-side, 0) angle = 60 for i in range(6): hex.addPolarEdge(side, angle) angle -= 60 return hex
def drawDiamond(height,theta,color,diamondAngle): #draws diamond of given height and color, with its' right edge on line with angle theta l=((.5*height)*cos((radians(.5*diamondAngle)))) diamond=GPolygon() t=theta+diamondAngle diamond.addVertex(0,0) diamond.addPolarEdge(l,t) t=t-diamondAngle diamond.addPolarEdge(l,t) t=t-(180-diamondAngle) diamond.addPolarEdge(l,t) diamond.setFillColor(color) diamond.setFilled(True) return diamond
def drawTriangle(size): t = GPolygon() t.addVertex(0, -(size / (2 * sqrt(3)))) t.addPolarEdge(size, 300) t.addPolarEdge(size, 180) return t
def createNose(): nose = GPolygon() nose.addVertex(0, NOSE_TIP) nose.addEdge(NOSE_WIDTH * -.5, NOSE_HEIGHT) nose.addEdge(NOSE_WIDTH, 0) snowman.add(nose)
def _createBackground(self): frame = GRect(0, 0, self._frameWidth, self._frameHeight) frame.setFilled(True) frame.setColor(FRAME_COLOR) self.add(frame) x1 = self._frameWidth / 2 x0 = x1 - PIECE_WIDTH - COLUMN_SEP x2 = x1 + PIECE_WIDTH + COLUMN_SEP y0 = TOP_MARGIN + PIECE_HEIGHT / 2 y1 = self._frameHeight - BOTTOM_MARGIN - PIECE_HEIGHT / 2 h = CHANNEL_WIDTH / 2 poly = GPolygon() poly.addVertex(x0 - h, y1 + h) poly.addVertex(x0 - h, y0 - h) poly.addVertex(x2 + h, y0 - h) poly.addVertex(x2 + h, y1 + h) poly.addVertex(x2 - h, y1 + h) poly.addVertex(x2 - h, y0 + h) poly.addVertex(x1 + h, y0 + h) poly.addVertex(x1 + h, y1 + h) poly.addVertex(x1 - h, y1 + h) poly.addVertex(x1 - h, y0 + h) poly.addVertex(x0 + h, y0 + h) poly.addVertex(x0 + h, y1 + h) poly.addVertex(x0 - h, y1 + h) poly.setFilled(True) poly.setColor(CHANNEL_COLOR) self.add(poly)
def __init__(self, color, level, puzzle): """Creates a piece with the indicated color and initial level""" GCompound.__init__(self) self._level = level self._puzzle = puzzle self.setColor(color) frame = GRect(PIECE_WIDTH, PIECE_HEIGHT) frame.setFilled(True) frame.setColor(PIECE_COLOR) self.add(frame, -PIECE_WIDTH / 2, 0) poly = GPolygon() dw = PIECE_WIDTH / puzzle.getNLevels() w0 = (level - 1) * dw w1 = level * dw poly.addVertex(-w0 / 2, 0) poly.addVertex(w0 / 2, 0) poly.addVertex(w1 / 2, PIECE_HEIGHT) poly.addVertex(-w1 / 2, PIECE_HEIGHT) poly.setFilled(True) poly.setColor(color) self.add(poly) border = GRect(PIECE_WIDTH, PIECE_HEIGHT) border.setColor(BORDER_COLOR) self.add(border, -PIECE_WIDTH / 2, 0)