-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrectangle_overlap.py2
64 lines (59 loc) · 1.72 KB
/
rectangle_overlap.py2
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
#!/usr/bin/python
from sys import argv
try:
FILE = argv[1]
except NameError:
FILE = 'tests/70'
DATA = open(FILE, 'r').read().splitlines()
class myRectangle:
# coords = []
# shorts = []
def __init__(self, points):
ux,uy,lx,ly = points
self.coords = [ (ux,ly),(ux,uy),(lx,uy),(lx,ly) ]
self.bounds = { 'ux': ux,'uy': uy,'lx':lx,'ly':ly }
def getcoords(self):
print self.coords
def xcoords(self):
return [(x) for x,y in self.coords]
def ycoords(self):
return [(y) for x,y in self.coords]
def compare(self, aRect):
# print 'our rect', self.bounds
# print 'their rekt', other
a = self.bounds
b = aRect.bounds
# overlap=True
if a['lx'] == b['lx']: #shared points
overlap=True
elif a['lx'] < b['lx']: #a is left of b
if a['ux'] < b['lx']:
overlap=False
else:
overlap=True
else: # b is left of a
if b['ux'] < a['lx']:
overlap=False
else:
overlap=True
if overlap: return overlap
if a['ly'] == b['ly']: #shared points
overlap=True
elif a['ly'] < b['ly']: #a is lower of b
if a['uy'] < b['ly']:
overlap=False
else:
overlap=True
else: # b is lower of a
if b['uy'] < a['ly']:
overlap=False
else:
overlap=True
# print line
return overlap
for line in DATA:
if not line:
continue
R1 = myRectangle([int(s) for s in line.split(',')][:4])
R2 = myRectangle([int(s) for s in line.split(',')][4:])
print R1.compare(R2)