import math import copy # used for copying objects class polygon: """ Represents a regular polygon of arbitrary number of edges and color. """ def __init__(self, ed, color = "none", length = 1): self.edges = ed # we could also define these above as variables self.color = color self.length = length def __str__(self): return "A " + self.color + " polygon with " + str(self.edges) + \ " edges of length " + str(self.length) def angles(self): """ returns inner angle in radians """ return ((self.edges - 2)*math.pi)/(self.edges) def area(self): """ calcules the area of the polygon """ return (self.edges*self.length**2)/(4*math.tan(math.pi/self.edges)) def paint(self, newColor): """ change the color of the polygon to newColor """ self.color = newColor def multArea(self, n): """ create a new polygon with a multiple of the area of the old one. """ newArea = self.area() * n oldEdges = self.edges oldColor = self.color newLength = math.sqrt((newArea*4*math.tan(math.pi/oldEdges))/oldEdges) return polygon(oldEdges, oldColor, length = newLength) # calling the constructor (i.e. init) class box: def __init__(self): # boxes always start empty self.contents = [] def __str__(self): return "This is a box of " + str(len(self.contents)) + " polygons." def listContents(self): """ prints out the contents of the box """ print "This box contains:" for p in self.contents: #note - this calls the __str__ function for the polygon print p def putin(self, item): """ Can put in either a single polygon or another box of polygons.""" if isinstance(item, box): for p in item.contents: self.contents[:0] = [p] # inserts at the beginning of the list elif isinstance(item, polygon): self.contents[:0] = [item] # inserts at the beginning of the list else: print "This box is only for polygons." def takeout(self, edges = 0, color = "", area = 0): """ Take out all polygons that meet any specified criteria. """ if edges == 0 and color == "" and area == 0: print "Please remember to specify what kind of polygons you want to take out." else: noDel = True newContents = [] for p in self.contents: if edges == p.edges or color == p.color or area == int(p.area()): noDel = False else: newContents.append(p) # could also write newContents[len(newContents):] = [p] if noDel: print "No polygons in this box matched your criteria for deletion." else: self.contents = newContents def how_many(self, edges = 0, color = ""): count = 0 if edges == 0 and color == "": print "Please specify what you want to count." elif edges != 0 and color != "": print "Please be more specific about what you want to count." else: for p in self.contents: if edges == p.edges or color == p.color: count = count + 1 if count == 0: print "No polygons in this box matched your criteria." else: return count def paint(self, newColor): """ Paint every polygon in the box a new color. """ for p in self.contents: p.paint(newColor) if True: # can use this to toggle running the code below or not # INSTANTIATION (making INSTANCES of a CLASS) blueTriangle = polygon(3, length = 3, color="blue")#order doesn't matter for optionals yellowSquare = polygon(4, "yellow", 2) #however, u don't need names if in order redPentagon = polygon(5, "red")#don't need to specify all if in order # METHODS - self # self in a param list basically says: # "this is a method that can act upon an instance of this class" # (aka - use the dotted method notation) # you don't pass in self as an argument, but it must be in the param list in order # for the dotted method notation to work # CALLING A METHOD ON AN OBJECT - DOTTED METHOD NOTATION # even though no params - still need parens - that makes it a function print "1." print "Yellow area is: " + str(yellowSquare.area()) print # STR # why might you want a string representation of your class? # in debugging if you wanted to print it out, but didn't feel like printing messages # each time print "2." print yellowSquare print #create a new square, twice the size of the yellow one, and paint it orange print "3." newSquare = yellowSquare.multArea(2) print "New square's area is: " + str(newSquare.area()) # the yellowsquare's size didn't change because multArea print "Yellow area is: " + str(yellowSquare.area()) print # returned a new instance of the polygon class print "4." newSquare.paint("orange") print yellowSquare print newSquare print # COPY # creates a shallow copy which is fine for us. use copy.deepcopy for a deep copy print "5." ys = copy.copy(yellowSquare) yellowSquare.length = 20 print ys print yellowSquare print # classes interacting with each other print "6." aBox = box() aBox.putin(newSquare) aBox.putin(yellowSquare) aBox.putin(blueTriangle) aBox.putin(redPentagon) print aBox print # point out LISTCONTENTS - called print polygon - str fun print "7." aBox.listContents() print #you can do things to the polygons when they're inside the box print "8." print "Painting everything blue" aBox.paint("blue") aBox.listContents() print # takeout all blue things print "9." aBox.takeout() print "Nothing should have changed" aBox.listContents() print print "10." print "Removing everything blue" aBox.takeout(color = "blue") print "Removed all the blue, so should be empty" print aBox print # put everything back in the box print "11." aBox.putin(newSquare) aBox.putin(yellowSquare) aBox.putin(blueTriangle) aBox.putin(redPentagon) aBox.listContents() print print "12." print "Changing to red..." blueTriangle.paint("red") #polygons can be modified in the box since its a reference! aBox.listContents() print print "13." print "Removing the red" aBox.takeout(area = 4, color = "red") print str(aBox)