Question: Python using matplotlib TODO Write a script named roomba.py For this problem, create a script named roomba.py . 1- In roomba.py create a performTrial() function.performTrial()

Python using matplotlib

TODO Write a script named roomba.py

For this problem, create a script named roomba.py.

1- In roomba.py create a performTrial() function.performTrial() will create a Roomba object and return its x,y coordinates each time it makes a move.

2- In the provided code below, modify the CHANGE ME sections to complete this script.

3- Pylab needs two lists to graph a point, one for x-coords and one for y-coords. Your new and improved performTrial() function should return a tuple containing a list of 500 xcoords and a list of 500 ycoords obtained by moving a Roomba object 500 times.

4- Use the Location objects getCoords() method to collect the x,y cooordinates for each move.

5- Change the plot color to any color other than black. Review the example code to see where you would make that change. See the Pyplot tutorial for extensive examples that show how to make the necessary changes.

6- Indicate the Start and Finish points for the roomba by using the Matplotlib.pyplot.annotate method. Examples showing how to use /matplotlib.pyplot.annotate here: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.annotate.html

Heres some code to get you started. If you have questions, bring them to the class forums.

# Douglas Putnam, CCSF # roomba.py, derived from random_walk.py by # John Guttag, Eric Grimson, 6.00 Introduction to Computer Science # Massachusetts Institute of Technology: MIT OpenCouseWare), # http://ocw.mit.edu (Accessed [Date]). # License: Creative Commons BY-NC-SA import math, random, pylab class Location(object): def __init__(self, x, y): self.x = float(x) self.y = float(y) def move(self, xc, yc): return Location(self.x+float(xc), self.y+float(yc)) def getCoords(self): return self.x, self.y def getDist(self, other): ox, oy = other.getCoords() xDist = self.x - ox yDist = self.y - oy return math.sqrt(xDist**2 + yDist**2) class CompassPt(object): possibles = ('N', 'S', 'E', 'W') def __init__(self, pt): if pt in self.possibles: self.pt = pt else: raise ValueError('in CompassPt.__init__') def move(self, dist): if self.pt == 'N': return (0, dist) elif self.pt == 'S': return (0, -dist) elif self.pt == 'E': return (dist, 0) elif self.pt == 'W': return (-dist, 0) else: raise ValueError('in CompassPt.move') class Field(object): def __init__(self, roomba, loc): self.roomba = roomba self.loc = loc def move(self, cp, dist): oldLoc = self.loc xc, yc = cp.move(dist) self.loc = oldLoc.move(xc, yc) def getLoc(self): return self.loc def getRoomba(self): return self.roomba class Roomba(object): def __init__(self, name): self.name = name def move(self, field, time = 1): if field.getRoomba() != self: raise ValueError('Roomba.move called with roomba not in field') for i in range(time): pt = CompassPt(random.choice(CompassPt.possibles)) field.move(pt, 1) ######################################################################## # FINISH THIS FUNCTION # REPLACE THE "TODO" and "CHANGE ME" LABELS IN YOUR SOLUTION. # Make srue to remove the TODO and CHANGE ME labels. ######################################################################## def performTrial(time, f): """performTrial() takes two arguments: time in seconds and a Pylab Field object.""" start = f.getLoc() # TODO for t in range(1, time + 1): # TODO return xcoords,ycoords ######################################################################## # END ######################################################################## roomba = Roomba('CHANGE ME : Your Choice of Names Goes Here') for i in range(1): f = Field(roomba, Location(0, 0)) # Get the x,y coordinates tuple of lists coords = performTrial(500, f) # Feed the x,y lists to pylab.plot pylab.plot(coords[0],coords[1],marker='^',linestyle=':',color='CHANGE ME') pylab.title('CHANGE ME --- A TITLE GOES HERE') pylab.xlabel('CHANGE ME --- A LABEL GOES HERE') pylab.ylabel('CHANGE ME --- A LABEL GOES HERE') pylab.show() 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!