-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolygons.py
More file actions
33 lines (25 loc) · 714 Bytes
/
polygons.py
File metadata and controls
33 lines (25 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import math
class RegularPolygon:
def __init__(self, sides, length):
self.sides = sides
self.length = length
@property
def perimeter(self):
return self.sides * self.length
@property
def apothem(self):
tan = math.tan(math.pi / self.sides)
return self.length / (2 * tan)
@property
def area(self):
return (self.apothem * self.perimeter) / 2
class Square(RegularPolygon):
def __init__(self, length):
super().__init__(4, length)
@property
def area(self):
return self.length ** 2
hexagon = RegularPolygon(6, 10)
print('hexagon: ' + str(hexagon.area))
square = Square(10)
print('square: ' + str(square.area))