numpy - Calculate overlapped area between two rectangles -

i want calculate overlapped area "the gray region" between red , blue rectangles.
each rectangle defined 4 corner coordinates. resulted unit of overlapped area unit square.
i not imagine how can it?
any creative comments appreciated.
this type of intersection done "min of maxes" , "max of mins" idea. write out 1 needs specific notion rectangle, and, make things clear i'll use namedtuple:
from collections import namedtuple rectangle = namedtuple('rectangle', 'xmin ymin xmax ymax') ra = rectangle(3., 3., 5., 5.) rb = rectangle(1., 1., 4., 3.5) # intersection here (3, 3, 4, 3.5), or area of 1*.5=.5 def area(a, b): # returns none if rectangles don't intersect dx = min(a.xmax, b.xmax) - max(a.xmin, b.xmin) dy = min(a.ymax, b.ymax) - max(a.ymin, b.ymin) if (dx>=0) , (dy>=0): return dx*dy print area(ra, rb) # 0.5 if don't namedtuple notation, use:
dx = max(a[0], b[0]) - min(a[2], b[2]) etc, or whatever notation prefer.
Comments
Post a Comment