Download shapes.py from the website and extend it with class Circle, class Polygon, and method area. Classes Circle and Polygon should inherit from Shape. For circles, at creation the radius has to be specified and the str method should return a string in the same style as for Line and Rectangle: For rectangles, at creation the list of vertices has to be follow the example below precisely. Add method area to classes Line, Rectangle, Circle. The area of a line is zero and the areas of rectangles and circles should be computed in the
usual way. For this, import pi from math. The area of a non-self-intersecting polygon with vertices is defined as
where each with represents a vertex (“corner”) of a polygon, and and . Note that the area of a convex polygon is positive if the points are in counterclockwise order and negative if they are in clockwise order. (The Wikipedia page takes the absolute value to avoid negative areas. Don’t do this here.)
Here are some test cases:
g = Group()
l = Line(1, 2, 5, 6); g.add(l); print(l)
Line from (1, 2) to (5, 6)
r = Rectangle(-2, -3, 2, 3); g.add(r); print(r)
Rectangle at (-2, -3), width 2, height 3
c = Circle(1, -5, 1); g.add(c); print(c)
Circle at (1, -5), radius 1
p = Polygon([(0, 0), (4, 0), (2, 2), (4, 4), (0, 4), (2, 2)]); g.add(p); print(p)
Polygon with [(0, 0), (4, 0), (2, 2), (4, 4), (0, 4), (2, 2)]
print(g); g.move(1, 1); print(g)
Group with:
Circle at (1, -5), radius 1
Rectangle at (-2, -3), width 2, height 3
Line from (1, 2) to (5, 6)
Polygon with [(0, 0), (4, 0), (2, 2), (4, 4), (0, 4), (2, 2)]
Group with:
Circle at (2, -4), radius 1
Rectangle at (-1, -2), width 2, height 3
Line from (2, 3) to (6, 7)
Polygon with [(1, 1), (5, 1), (3, 3), (5, 5), (1, 5), (3, 3)]
l.area(), r.area(), c.area(), p.area(), g.area()
(0, 6, 3.141592653589793, 8.0, 17.141592653589793)
I'm very new at Python and don't know how to approach this whatsoever. Any help would be appreciated! Thanks!