-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.py
222 lines (182 loc) · 4.81 KB
/
shape.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import numpy
from vertex import V
from math import pi,sin,cos,sqrt
"""
Shape class:
stores basic information regarding 2D shapes
"""
class Shape(object):
def __init__(self,type,v):
#guard against lack of sleep
if(len(v) == 0):
raise ValueError("Empty Input")
self.type = type #circle or polygon
self.v = v #list of vertices
#TODO: self.center = self.compute_center()
#print function
def __repr__(self):
s = ""
for vertex in self.v:
s += str(vertex)
return "type: " + self.type + " v: " + s
#get type
def get_type(self):
return self.type
#get list of vertices
def get_vertices(self):
return self.v
#rotation
#TODO: def rotate(self,angle)
#linear transformation
def translate(self,vector):
for vertex in self.v:
vertex.move(vector)
#get position
def get_pos(self):
flat_list = []
for vertex in self.v:
t = vertex.get_pos()
flat_list.extend(list(t))
return tuple(flat_list)
#=============================================================
"""
class Circle(Shape):
def __init__(self,c,r):
if(r <= 0):
raise ValueError("Invalid radius")
Shape.__init__(self,"circle",[c]) #send center of circle in
self.radius = r
#print statement
def __repr__(self):
s = Shape.__repr__(self)
s += " rardius: "+ str(self.radius)
return s
#retrieve radius
def get_radius(self):
return self.radius
#return list of vertices at the circumference
#it's an approximation of a circle
def get_pos(self):
pos = self.v[0]
p = int(self.radius*2)
angle = 0
increment = (pi * 2) / p
coords = []
for i in range(0, p):
x = self.radius * sin(angle) + pos.x
y = self.radius * cos(angle) + pos.y
coords.append(x)
coords.append(y)
angle += increment
return tuple(coords)
#TODO: how do I represent circle in quadtree?
"""
class Polygon(Shape):
def __init__(self,v):
Shape.__init__(self,"polygon",v)
"""
Rectangle class
"""
class Rectangle(Polygon):
def __init__(self,*args, **kwargs):
self.width = 0;
self.height = 0;
if(len(args)==4):
Polygon.__init__(self,list(args))
v1,v2,v3,v4 = args
x1,y1 = (v1.x,v1.y)
x2,y2 = (v2.x,v2.y)
x3,y3 = (v3.x,v3.y)
x4,y4 = (v4.x,v4.y)
self.width = max(x1,x2,x3,x4) - min(x1,x2,x3,x4)
self.height = max(y1,y2,y3,y4) - min(y1,y2,y3,y4)
else:
self.width = kwargs.get('width',0)
self.height = kwargs.get('height',0)
lbcV = args[0]
lucV = V(lbcV.x, lbcV.y + self.height)
rbcV = V(lbcV.x + self.width, lbcV.y)
rucV = V(lbcV.x + self.width, lbcV.y + self.height)
Polygon.__init__(self,[lbcV,rbcV,rucV,lucV])
#self.type = "rectangle"
def get_width(self):
return self.width
def get_height(self):
return self.height
def get_center(self):
v1,v2,v3,v4 = self.get_vertices()
x1,y1 = (v1.x,v1.y)
x2,y2 = (v2.x,v2.y)
x3,y3 = (v3.x,v3.y)
x4,y4 = (v4.x,v4.y)
return V(min(x1,x2,x3,x4)+self.width/2, min(y1,y2,y3,y4)+self.height/2)
def length_V(v1,v2):
return abs(sqrt((v1.x-v2.x)**2 + (v1.y-v2.y)**2))
"""
returns length of a vector
"""
def length(v1,v2):
return abs(sqrt((v1[0]-v2[0])**2 + (v1[1]-v2[1])**2 ))
"""
checks if point is inside a rectangle
"""
def p_inside_rect(p,rect):
p_rect = Rectangle(p)
#print("inside p_inside_rect: "+str(p)+" "+str(rect))
return check_xy_overlap(p_rect,rect)
'''
function checks if bounding box of x and y direction overlap
'''
def check_xy_overlap(s1,s2):
vertices1 = s1.get_vertices()
vertices2 = s2.get_vertices()
x_max_1,x_max_2,y_max_1,y_max_2 = 0,0,0,0
x_min_1,y_min_1,x_min_2,y_min_2 = float('inf'),float('inf'),float('inf'),float('inf')
for v in vertices1:
x_max_1 = max(x_max_1,v.x)
x_min_1 = min(x_min_1,v.x)
y_max_1 = max(y_max_1,v.y)
y_min_1 = min(y_min_1,v.y)
for v in vertices2:
x_max_2 = max(x_max_2,v.x)
x_min_2 = min(x_min_2,v.x)
y_max_2 = max(y_max_2,v.y)
y_min_2 = min(y_min_2,v.y)
#checks for x and y directions
x_dir = not(x_max_2 < x_min_1 or x_max_1 < x_min_2)
y_dir = not(y_max_2 < y_min_1 or y_max_1 < y_min_2)
direction=[]
#shape 2 is on the left of shape 1
if x_max_2 <= x_min_1:
direction.append("left")
if x_min_2 >= x_max_1:
direction.append("right")
if y_min_2 >= y_max_1:
direction.append("up")
if y_max_2 <= y_min_1:
direction.append("down")
return (x_dir and y_dir), direction
if __name__ == '__main__':
v1 = V(0,0)
v2 = V(0,2)
v3 = V(2,0)
shape1 = Shape("polygon",[v1,v2,v3])
print(shape1)
print("translate by 1,1")
shape1.translate([1,1])
print(shape1)
print(shape1.get_pos())
print("========end of testing shape======")
#c1 = Circle(V(1,1),1.5)
#print(c1)
#print(c1.get_pos())
print("========end of testing circle=======")
p1 = Polygon([V(0,0),V(1,0),V(1,1),V(0,1)])
print(p1)
print("test rectangle")
r1 = Rectangle(V(0,0),V(1,0),V(1,1),V(0,1))
r2 = Rectangle(V(3,0), width=1,height=2)
print(r1.get_height())
print(r2.get_width())
print(r2)
print(check_xy_overlap(r1,r2))