summaryrefslogtreecommitdiff
path: root/python-classes/main.py
blob: 06cc9704b4271db64bf4d04a741988d7d4efe9c1 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import math
import time

from pygame.locals import *
import pygame


class Colors:
    BLACK = Color("black")
    RED = Color("red")
    GREEN = Color("green")
    BLUE = Color("blue")
    YELLOW = Color("yellow")
    CYAN = Color("cyan")


class Shape:
    def perimeter(self):
        raise NotImplementedError()

    def area(self):
        raise NotImplementedError()


class Showable:
    def draw(self, surface):
        raise NotImplementedError()

    def show(self):
        pygame.init()
        self.w, self.h = size = 200, 200
        screen = pygame.display.set_mode(size)

        running = True
        while running:
            screen.fill(Colors.BLACK)

            self.draw(screen)

            pygame.display.flip()

            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    running = False
                elif event.type == KEYDOWN:
                    if event.key in [K_ESCAPE, K_q]:
                        pygame.quit()
                        running = False


class Point(Shape, Showable):
    def perimeter(self):
        return 0

    def area(self):
        return 0

    def draw(self, surface):
        center = (self.w // 2, self.h // 2)
        pygame.draw.circle(surface, Colors.GREEN, center, radius=2)


class Circle(Shape, Showable):
    def __init__(self, radius):
        self.r = radius

    def perimeter(self):
        return 2 * math.pi * self.r
    
    def area(self):
        return math.pi * self.r * self.r
    
    def draw(self, surface):
        center = (self.w // 2, self.h // 2)
        pygame.draw.circle(surface, Colors.RED, center, radius=self.r)


class Square(Shape, Showable):
    def __init__(self, side):
        self.side = side

    def perimeter(self):
        return self.side * 4

    def area(self):
        return self.side * self.side

    def draw(self, surface):
        center_x = self.w // 2
        center_y = self.h // 2
        left_top = (center_x - self.side // 2, center_y - self.side // 2)
        size = (self.side, self.side)
        pygame.draw.rect(surface, Colors.BLUE, Rect(left_top, size))


class Rectangle(Shape, Showable):
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def perimeter(self):
        return (self.a + self.b) * 2

    def area(self):
        return self.a * self.b

    def draw(self, surface):
        center_x = self.w // 2
        center_y = self.h // 2
        left_top = (center_x - self.a // 2, center_y - self.b // 2)
        size = (self.a, self.b)
        pygame.draw.rect(surface, Colors.CYAN, Rect(left_top, size))


class EquilateralPolygon(Shape, Showable):
    def __init__(self, sides, radius):
        self.n = sides
        self.r = radius

    def perimeter(self):
        a = 2 * self.r * math.sin(math.pi / self.n)
        return a * self.n
    
    def area(self):
        return self.n / 2 * self.r * self.r * math.sin(2 * math.pi / self.n)
    
    def draw(self, surface):
        points = []
        xc = self.w // 2
        yc = self.h // 2
        for i in range(self.n):
            points.append((
                xc + self.r * math.cos(2 * math.pi * i / self.n),
                yc + self.r * math.sin(2 * math.pi * i / self.n)
            ))
        
        pygame.draw.polygon(surface, Colors.YELLOW, points)


def main():
    print("===== Point =====")

    p = Point()
    print(p.perimeter())
    print(p.area())
    p.show()

    print("===== Circle =====")

    c = Circle(40)
    print(c.perimeter())
    print(c.area())
    c.show()

    print("===== Square =====")

    sq = Square(120)
    print(sq.perimeter())
    print(sq.area())
    sq.show()

    print("===== Rectangle =====")

    r = Rectangle(120, 60)
    print(r.perimeter())
    print(r.area())
    r.show()

    print("===== EquilateralPolygon =====")

    poly = EquilateralPolygon(10, 60)
    print(poly.perimeter())
    print(poly.area())
    poly.show()


if __name__ == "__main__":
    main()