python - Manhatten distance between rectangles -
i have set of rectangles , need calculate manhatten distance between them. tried implement it, code blow , did not work well.
maybe me smart (and efficient) formulas used calculate distance between 2 rectangles?
examples:
the distance between a
, b
length of line 1
. distance between a
, c
length of line 2
. etc.
i use python implement everything. if function exists (e.g. in scipy) , knows it, great.
thank you
i suggest, work central points of rectangles , rectangle widths compute distances. have figure out corners (edges) of rectangles use computation. else simple. quick example:
class rect: def __init__(self,cpt,w,h): self.x = cpt[0] self.y = cpt[1] self.w = w self.h = h def dist(self,other): #overlaps in x or y: if abs(self.x - other.x) <= (self.w + other.w): dx = 0; else: dx = abs(self.x - other.x) - (self.w + other.w) # if abs(self.y - other.y) <= (self.h + other.h): dy = 0; else: dy = abs(self.y - other.y) - (self.h + other.h) return dx + dy #example: = rect((0,0),2,1) b = rect((4,5),1,2) c = rect((-1,-5),1,1) print(a.dist(c)) print(a.dist(b)) print(b.dist(c))
Comments
Post a Comment