Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
14-liner: general purpose (float numbers and large integers) solution in Clear category for Rectangles Union by przemyslaw.daniel
def rectangles_union(rectangles):
xs = sorted(sum(rectangles, [])[::2])
ys = sorted(sum(rectangles, [])[1::2])
boxes = [(xs[i], ys[j], xs[i+1], ys[j+1])
for i in range(len(xs)-1)
for j in range(len(xs)-1)]
result = 0
for (x1, y1, x2, y2) in boxes:
for (x3, y3, x4, y4) in rectangles:
if x3 <= x1 <= x4 and x3 <= x2 <= x4 and \
y3 <= y1 <= y4 and y3 <= y2 <= y4:
result += abs(x1-x2)*abs(y1-y2)
break
return result
Oct. 19, 2018
Comments: