Issue
I have a problem with printing lined up rectangles. The task is to implement function that generates an infinite sequence (however the professor said it's not necessarily, could be, for instance, 10 rectangles) of not intersecting equal rectangles with different coordinates. It should look like the the lower straight line of hexagons (but I need rectangles) on the picture (with equal spaces).
For now I have this code:
import matplotlib.pyplot as plt
class Figure:
def __init__(self):
self.x = []
self.y = []
def show(self):
plt.plot(self.x, self.y)
plt.axis([-0.2, 10, -0.2, 5])
plt.show()
class Rectangle(Figure):
def __init__(self):
self.x = [0, 0, 2, 2, 0]
self.y = [0, 1, 1, 0, 0]
def show():
plot = Rectangle()
plot.show()
show()
I'm not allowed to use matplotlib to create figures, it should be done with my own classes and functions. Coordinates of the first rectangle are already in the code. Coordinates of the second one should be:
x = [3, 3, 5, 5, 3]
y = [0, 1, 1, 0, 0]
Of the third one:
x = [6, 6, 8, 8, 6]
y = [0, 1, 1, 0, 0]
And so on. I see that to every item in x 3 is added. But I don't understand how to make a function (or what library to use) that does it and shows all rectangles in one plot. Thanking you in advance for all the help!
Solution
You could use for
-loop to repeate the same code few times but with different values - plot.x = ...
- and you could use value from for
to add 3
- 3*i
And you should remove plt.show()
from class and use it only once - after plotting all rectangles - and this will show all rectangles on one plot.
import matplotlib.pyplot as plt
class Figure:
def __init__(self):
self.x = []
self.y = []
def show(self):
plt.plot(self.x, self.y)
plt.axis([-0.2, 10, -0.2, 5])
#plt.show()
class Rectangle(Figure):
def __init__(self):
self.x = [0, 0, 2, 2, 0]
self.y = [0, 1, 1, 0, 0]
def show():
for i in range(10):
plot = Rectangle()
plot.x = [x+3*i for x in plot.x]
plot.show()
plt.show()
show()
Answered By - furas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.