# ============================================================ # File: state.py # Description: defines a state object # Author: Emily G Allen # Utilizes polygon data assembled by D. Kumar (statedata.txt file) # Date: April 2008 # ============================================================ # ============================================================ # Required Libraries # ============================================================ from graphics import * # ============================================================ # ============================================================ # State Class definition # ============================================================ # ============================================================ class State: # constructor def _init_(self): self.name = '' self.abbrev = '' self.capital = '' self.capitalCoord = [] self.polygon = [] self.value = -500 # data value plotted # ============================================================ # Modifiers # ============================================================ def setValue(self, value): self.value = value def updateValues(self, name, abbrev, capital, capitalCoords, polygon): self.name = name self.abbrev = abbrev self.capital = capital self.capitalCoords = capitalCoords self.polygon = polygon # ============================================================ # Accessors # ============================================================ def getName(self): return self.name def getAbbrev(self): return self.abbrev def getCapital(self): return self.capital def getCapitalCoord(self): return self.capitalCoord def getPolygon(self): return self.polygon def getValue(self): return self.value # ============================================================ # Drawing # ============================================================ def draw(self, window, color = 'white'): self.polygon.setFill(color) self.polygon.draw(window) def drawCapital(self, window, radius = 2): c = Circle(self.capitalCoords, radius) c. setFill ('black') c.draw(window) def undraw(self): self.polygon.undraw() # ============================================================ # ============================================================ # Loading State Data (creating a list of states) # ============================================================ # ============================================================ # ============================================================ # Function: createPolygon # Description: converts list of strings containing coordinates # of a polygon into a polygon object # Inputs: a list of strings containing coordinates for a polygon # in format ['x1', 'y1', 'x2', 'y2',...,'xn', yn'] # Outputs: a polygon object # ============================================================ def createPolygon(coords): pList = [] i = 0 # convert each element in the list to a numeric value # create a point out of each pair of x,y values # append each point to a list while i < len(coords): pList.append(Point(eval(coords[i]), eval(coords[i + 1]))) i = i + 2 # use the point list to define a polygon polygon = Polygon(pList) return polygon # ============================================================ # Function: loadStateData # Description: loads data defining state polygons from a file # Inputs: none # Outputs: a list of state objects # ============================================================ def loadStateData(): infile = open('statedata.txt', 'r') # open data file # iterate through the data file stateList = [] text = infile.readline() # read in name, abbreviation, capital name, capital coordinates while text != '': # while ! EOF text = text.split(',') # split at commas coords = infile.readline() # read in polygon coordinates coords = coords.split(',') # split at commas polygon = createPolygon(coords) # make a polygon out of coordinate list # create a new state and assign loaded values state = State() state.updateValues(text[0], text[1], text[2], Point(eval(text[3]), eval(text[4])), polygon) stateList.append(state) # append to state list # move on to next state text = infile.readline() # read in name, abbreviation, capital name, capital coordinates infile.close() # close in file return stateList # ============================================================ # Function: getStateData # Description: iterates through states and retrieves data # stored # Inputs: the list of states # Outputs: a list of stored values # ============================================================ def getStateData(stateList): # get values out of stateList data = [] for state in stateList: data.append(state.getValue()) return data