# Starmap - plot a file of star coordinates # # This program plots a field of stars from the stars.txt taken from # http://nifty.stanford.edu/2009/reid-starmap/ # #Uses Calico and included Graphics library from Graphics import * #Opens the specified file and reads in the X and Y coordinates of each star #Returns a list of tuples with the X and Y coordinates. def readStarData(filename): starList = [] myFile = open(filename, "r") starData = myFile.readlines() print("length of starData is:", len(starData) ) for starLine in starData: tempList = starLine.split() xStr = tempList[0] yStr = tempList[1] x = float( xStr) y = float( yStr) starTuple = (x,y) starList.append( starTuple ) return( starList) #Converts between starfile cordinates (-1.0 to 1.0) to # window coordinates (0 to windowSize) def convertPoint(starFileX, starFileY, windowSize): halfWinSize = windowSize // 2 windowX = int( (halfWinSize * starFileX) + halfWinSize ) windowY = int( (halfWinSize * starFileY) + halfWinSize ) return (windowX, windowY) # Creates a Window and draws all stars into it. Stars in the starData # parameter are assumed to be in the format generated by the readStarData # function (above) def plotStarData(starData, winSize): myWin = Window("StarMap", winSize,winSize) myWin.setBackground( Color("black") ) for star in starData: X = star[0] Y = star[1] #This will convert coordinate spaces X,Y = convertPoint(X,Y,winSize) #Draw the dot. d = Dot(X,Y) d.outline = Color("white") d.fill = Color("white") d.draw(myWin) print("All finished plotting stars!") #To draw a star map, you have to do two things: #First, read the data: myStarList = readStarData("stars.txt") #Then, plot the data! plotStarData( myStarList,700)