Created
December 12, 2015 12:17
-
-
Save damiankao/b457d38161b3b81580ee to your computer and use it in GitHub Desktop.
Python functions to find union of multiple coordinates and intersection of multiple coordinates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def tileCoords(coords): | |
formatCoords = [] | |
for coord in coords: | |
coord = [int(coord[0]),int(coord[1])] | |
formatCoords.append(('s',min(coord))) | |
formatCoords.append(('e',max(coord))) | |
formatCoords.sort(key = lambda x : x[0], reverse = True) | |
formatCoords.sort(key = lambda x : x[1]) | |
count = 0 | |
posA = 0 | |
out = [] | |
for pos in formatCoords: | |
if count == 0: | |
posA = pos[1] | |
if pos[0] == 's': | |
count += 1 | |
if pos[0] == 'e': | |
count -=1 | |
if count == 0: | |
out.append((posA, pos[1])) | |
return out | |
def intersectCoords(coords): | |
formatCoords = [] | |
for coord in coords: | |
coord = [int(coord[0]),int(coord[1])] | |
formatCoords.append(('s',min(coord))) | |
formatCoords.append(('e',max(coord))) | |
formatCoords.sort(key = lambda x : x[0], reverse = True) | |
formatCoords.sort(key = lambda x : x[1]) | |
count = 0 | |
posA = 0 | |
level = 1 | |
out = [] | |
for pos in formatCoords: | |
if pos[0] == 's': | |
count = 1 | |
level += 1 | |
posA = pos[1] | |
if pos[0] == 'e': | |
level -=1 | |
count -=1 | |
if count == 0: | |
out.append((posA, pos[1], level)) | |
return out | |
tileCoords([[5,20],[10,40]]) #output [5,40] | |
intersectCoords([[5,20],[10,40]]) #output [10,20],2, 10,20 is where the intersection occurs, and there are 2 pairs that overlap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment