# MCS 260 Project Four due 7 Nov 2007 : writeps.py # # This module exports functions to write a random walk # to a postscript file. def start(name,rows,columns): """ Writes to a file with the given name a postscript picture, with given number of rows and columns, returns the file object. """ XOFFSET = 54 YOFFSET = 288 SCALE = 432 file = open(name + '.ps','w') file.write('%!PS\n') file.write('%%BoundingBox: 0 300 500 720\n') file.write('/picstr %d string def\n' % rows) file.write('%d %d translate\n' % (XOFFSET,YOFFSET)) file.write('%d %d scale\n' % (SCALE,SCALE)) file.write('%d %d %d\n' % (columns,rows,8)) file.write('[%d 0 0 -%d 0 %d]\n' % (columns,rows,rows)) file.write('{currentfile picstr readhexstring pop}\n') file.write('image\n') return file def write_image(file,rows,columns,points,fade): """ Writes the coordinates of the points to file, into an image with the given number of rows and columns. The parameter fade determines how fast the pixels become white. """ for i in range(0,rows): for j in range(0,columns): if i==0 or j==0 or i==rows-1 or j==columns-1: file.write('%.2x' % 0) elif (i,j) in points: k = min(fade*points.index((i,j)),255) file.write('%.2x' % k ) else: file.write('%.2x' % 255) file.write('\n') def finish(file): """ Finishes writing to file and closes the file. """ file.write('showpage\n') file.close()