-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangular.py
More file actions
152 lines (128 loc) · 4.82 KB
/
rectangular.py
File metadata and controls
152 lines (128 loc) · 4.82 KB
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
from datetime import datetime
class Rectangular():
def __init__(self, x, y, width, height,time=None):
"""
description:
difine a rectangular in X-Y coordinate
args:
x: horizontal coordinate
y: vertical coordinate
width: the segment between x, x+ width
height: the segment between y, y+height
time: timeStamp of the creation of this object
"""
self.x = x
self.y = y
self.width = width
self.height = height
self.area = width * height
now = datetime.now()
dt_string = now.strftime("%Y-%m-%d %H:%M:%S")
self.time = dt_string
def filter_accepted_rectangular(self, others):
"""
description:
Select thoses rectangular that has intersection with main rectangular
args:
Take a instace of Rectangular Class
Return:
List of dictionay of accepted Rectangular
"""
accepted_list = []
for item in others:
if self._has_ratio(item):
accepted_list.append(item.json_able())
return accepted_list
def _has_ratio(self, other):
#indicting which one in the right or left
if self.x <= other.x:
left_rect = self
right_rect = other
else:
left_rect = other
right_rect = self
#indicting which one in the top or bottom
if self.y >= other.y:
top_rect = self
bottom_rect = other
else:
top_rect = other
bottom_rect = self
#calculating overlap width
#No overlap
if left_rect.x + left_rect.width <= right_rect.x:
self.overlap_width = 0
#fully overlap
elif (left_rect.x + left_rect.width) >= (right_rect.x+right_rect.width):
self.overlap_width = right_rect.width
#partialy overlap
else:
self.overlap_width = (left_rect.x + left_rect.width) - right_rect.x
#==========================================================#
#==========================================================#
#calculating overlap height
#No overlap
if bottom_rect.y + bottom_rect.height <= top_rect.y :
self.overlap_height = 0
#fully overlap
elif (bottom_rect.y + bottom_rect.height) >= (top_rect.y + top_rect.height):
self.overlap_height = right_rect.height
#partialy overlap
else:
self.overlap_height = (bottom_rect.y + bottom_rect.height) - top_rect.y
if self.overlap_width > 0 and self.overlap_height > 0:
self.intersection_area = self.overlap_width * self.overlap_height / self.area
other.intersection_area = self.overlap_width * self.overlap_height / self.area
# print("product",self.overlap_width * self.overlap_height)
return self.intersection_area
else:
return None
def json_able(self):
"""
description:
it return just a dictionary of class attributes
"""
ret = {"x":self.x, "y":self.y, "width":self.width,
"height":self.height,"ratio":self.intersection_area,"time":self.time}
return ret
if __name__ == "__main__":
r1 = Rectangular(0,0,10,20)
r2 = Rectangular(2,18,5,4)
r3 = Rectangular(12,18,5,4)
r4 = Rectangular(-1,-1,5,4)
r5 = Rectangular(3,2,5,10)
r6 = Rectangular(4,10,1,1)
r7 = Rectangular(9,10,5,4)
area11 = r1._has_ratio(r1)
area12 = r1._has_ratio(r2)
area13 = r1._has_ratio(r3)
area14 = r1._has_ratio(r4)
area56 = r5._has_ratio(r6)
area57 = r5._has_ratio(r7)
for area in [area11,area12,area13,area14,area56,area57]:
if area :
print(area)
# def _has_intersection(self,other):
# """
# description:
# Check that the intersection of two objects(Intersection is symmetric).
# if just of vertex in second rectangular in main rectangular
# then they have an intersection.
# WARNING: this definition excludes the boundary.
# args:
# Take a instace of Rectangular Class
# Return(boolen):
# True if there exist a intersection Area
# """
# x1 = (self.x < other.x < (self.x+ self.width))
# x2 = (self.x < (other.x + other.width) < (self.x+ self.width))
# y1 = (self.y < other.y < (self.y + self.height))
# y2 = (self.y < (other.y + other.height) < (self.y + self.height))
# #these are four boolean that indcate the existence of vertices in main rectangular
# x1y1 = (x1 and y1)
# x2y1 = (x2 and y1)
# x1y2 = (x1 and y2)
# x2y2 = (x2 and y2)
# if any([x1y1, x2y1, x1y2, x2y2]):
# return True
# return False