class Point(object): """represents a point in 2-D space""" def __init__(self, x, y): self.x = x self.y = y def __add__(self, other): return Point(self.x + other.x, self.y + other.y) class Rectangle(object): """represent a rectangle. attributes: width, height, corner (lower left), color. """ def __init__(self, width, height, corner, color): self.width = width self.height = height self.corner = corner self.color = color def draw(r, canvas): """Draws r on canvas.""" bbox = [[r.corner.x, r.corner.y], [r.corner.x + r.width, r.corner.y + r.height]] canvas.rectangle(bbox, outline='black', width=2, fill=r.color) def move(self, displacement): self.corner = self.corner + displacement 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(c, canvas): canvas.circle([c.center.x,c.center.y], c.radius, outline=None, fill=c.color) def move(self, displacement): self.center = self.center + displacement class Polygon(object): """attributes: a list of points and a color""" def __init__(self, points, color): self.points = points self.color = color def draw(p, canvas): canvas.polygon ([[pt.x, pt.y] for pt in p.points], fill= p.color) def move(self, displacement): self.points = [pt + displacement for pt in self.points] class Composite(object): """atributes: a list of constituent shapes""" def __init__(self, shapes): self.shapes = shapes def draw(self, canvas): for shape in self.shapes: shape.draw(canvas) def move(self, displacement): for shape in self.shapes: shape.move(displacement) from World import World def show(image): w = World() canvas = w.ca(width=500, height=500, background='white') image.draw(canvas) w.mainloop() def flagOfBangladesh(): rect = Rectangle(color='green4', corner=Point(-150,-100), height=200, width=300) c = Circle(radius=70, color='red', center=Point(-25,0)) return Composite([rect, c]) def flagOfCzechRepublic(): redRect = Rectangle(color='red', corner=Point(-150,-100), height=200, width=300) whiteRect = Rectangle(color='white', corner=Point(-150,0), height=100, width=300) background = Composite([redRect, whiteRect]) triangle = Polygon(points=[Point(-150,-100),Point(-150,100),Point(-10,0)], color = 'blue') return Composite([background, triangle])