Python using matplotlib TODO Write a script named roomba.py For this problem, cr
ID: 3915492 • Letter: P
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() will create a Roomba object and return it’s 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 object’s 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
Here’s some code to get you started. If you have questions, bring them to the class forums.
Explanation / Answer
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
#####################################################################
def performTrial(time, f):
"""performTrial() takes two arguments: time in seconds and a Pylab Field object."""
start = f.getLoc()
xcoords = []
ycoords = []
# TODO
for t in range(1, time + 1):
# TODO
f.getRoomba().move(f)
newLoc = f.getLoc()
x, y = newLoc.getCoords()
xcoords.append(x)
ycoords.append(y)
return xcoords, ycoords
#####################################################################
# END
#####################################################################
roomba = Roomba('Roombie')
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='red')
pylab.title('Roombie's Adventure')
pylab.xlabel('time')
pylab.ylabel('distance from origin')
pylab.show()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.