from Gui import * class Point(object): '''represents a point in 2-D space attributes: x, y. ''' def __init__(self, x, y): self.x = x self.y = y class Rectangle(object): '''represents a rectangle. attributes: width, height, (lower left) corner, color. ''' def __init__(self, width, height, corner, color): self.width = width self.height = height self.corner = corner self.color = color def draw(self, canvas): '''Draw self on canvas.''' bbox = [[self.corner.x, self.corner.y], [self.corner.x + self.width, self.corner.y + self.height]] canvas.rectangle(bbox, outline='black', width=2, fill=self.color) class Circle(object): '''represents a circle. attributes: center, radius, color. ''' def __init__(self, center, radius, color): self.center = center self.radius = radius self.color = color def draw(self, canvas): '''Draw self on canvas.''' canvas.circle([self.center.x, self.center.y], self.radius, fill=self.color) class Polygon(object): '''represents a polygon. attributes: points, color. ''' def __init__(self, points, color): self.points = points self.color = color def draw(self, canvas): '''Draw self on canvas.''' canvas.polygon([[pt.x, pt.y] for pt in self.points], fill=self.color) class Composite(object): '''represents a composite of images. attributes: (list of) images.''' def __init__(self, images): self.images = images def draw(self, canvas): '''Draw self on canvas.''' for image in self.images: image.draw(canvas) def draw(image): '''Draw image.''' g = Gui() canvas = g.ca(width=500, height=500, background='white') image.draw(canvas) g.mainloop() def bangladesh(): '''return the image of the Bangladesh flag''' rect = Rectangle(width=300, height=200, corner=Point(-150, -100), color='green4') circle = Circle(center=Point(-25,0), radius=70, color='red') return Composite([rect, circle]) def czech(): '''return the image of the Czech flag''' whiteRect = Rectangle(width=300, height=200, corner=Point(-150, -100), color='white') redRect = Rectangle(width=300, height=100, corner=Point(-150, -100), color='red') triangle = Polygon(points = [Point(-150, 100), Point(-150, -100), Point(0, 0)], color = 'blue4') return Composite([whiteRect, redRect, triangle])