-
Notifications
You must be signed in to change notification settings - Fork 0
/
rectangle.py
65 lines (53 loc) · 2 KB
/
rectangle.py
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
# stdlib
import random
from math import sqrt
class Rectangle():
"""
Class responsible for representing a rectangle object. Each rectangle
has coordinates for its four corners as well as a unique id
"""
counter = 0
def __init__(self, upper_left_coord, lower_right_coord, is_opposite=False):
if not isinstance(upper_left_coord, tuple) and \
not isinstance(lower_right_coord, tuple):
raise Exception("Rectangle arguments must be tuples")
if not isinstance(upper_left_coord[0], int) and \
not isinstance(upper_left_coord[1], int) and \
not isinstance(lower_right_coord[0], int) and \
not isinstance(lower_right_coord[1], int):
raise Exception("Tuple entries must be integers")
self.is_opposite = is_opposite
self.x1 = upper_left_coord[0]
self.y1 = upper_left_coord[1]
self.x2 = lower_right_coord[0]
self.y2 = upper_left_coord[1]
self.x3 = lower_right_coord[0]
self.y3 = lower_right_coord[1]
self.x4 = upper_left_coord[0]
self.y4 = lower_right_coord[1]
self.id = Rectangle.counter
self.params = {
'id': Rectangle.counter
}
Rectangle.counter += 1
colors = ['white',
'black',
'red',
'blue',
'yellow']
def __repr__(self):
return f'{self.__class__.__name__} : {self.id}'
def __getitem__(self, key):
return self.params[key]
def update_all(self, params):
for key, value in params.items():
setattr(self, key, value)
self.params[key] = value
def calculate_area(self):
side_one = sqrt((self.x2 - self.x1)**2 + (self.y2 - self.y1)**2)
side_two = sqrt((self.x3 - self.x2)**2 + (self.y3 - self.y2)**2)
return side_one * side_two
def calculate_center(self):
middle_x = (self.x1 + self.x2) / 2
middle_y = (self.y2 + self.y3) / 2
return (middle_x, middle_y)