# This is an answer to Exercise 15.4 done using objects. from World import World class Point(object): '''represents a point in 2-D space attributes: x, y. ''' class Rectangle(object): '''represents a rectangle. attributes: width, height, corner, color. ''' class Circle(object): '''represents a circle. attributes: center, radius, color. ''' class Polygon(object): '''represents a polygon. attributes: points, color. ''' def draw_rectangle(canvas, rect): '''Draws a reprentation of the Rectangle rect on the Canvas canvas.''' bbox = [[rect.corner.x, rect.corner.y], [rect.corner.x + rect.width, rect.corner.y + rect.height]] canvas.rectangle(bbox, outline='black', width=2, fill=rect.color) def draw_circle(canvas, circle): '''Draws a reprentation of the Circle circle on the Canvas canvas.''' canvas.circle([circle.center.x, circle.center.y], circle.radius, fill=circle.color) def draw_polygon(canvas, pgon): '''Draws a reprentation of the Polygon pgon on the Canvas canvas.''' canvas.polygon([[pt.x, pt.y] for pt in pgon.points], fill=pgon.color) ########## Bangladesh flag def bangladesh(): world = World() canvas = world.ca(width=500, height=500, background='white') rect = Rectangle() rect.width = 300 rect.height = 200 rect.corner = Point() rect.corner.x, rect.corner.y = -150, -100 rect.color = 'green4' draw_rectangle(canvas, rect) circle = Circle() circle.center = Point() circle.center.x = -25 circle.center.y = 0 circle.radius = 70 circle.color = 'red' draw_circle(canvas, circle) world.mainloop() ########## Czech flag def czech(): world = World() canvas = world.ca(width=500, height=500, background='white') rect = Rectangle() rect.width = 300 rect.height = 200 rect.corner = Point() rect.corner.x, rect.corner.y = -150, -100 rect.color = 'white' draw_rectangle(canvas, rect) center = Point() center.x, center.y = 0, 0 ul = Point() ul.x, ul.y = -150, 100 ll = Point() ll.x, ll.y = -150, -100 mr = Point() mr.x, mr.y = 150, 0 lr = Point() lr.x, lr.y = 150, -100 pgon1 = Polygon() pgon1.points = [ul, ll, center] pgon1.color = 'blue4' draw_polygon(canvas, pgon1) pgon2 = Polygon() pgon2.points = [ll, center, mr, lr] pgon2.color = 'red3' draw_polygon(canvas, pgon2) world.mainloop()