0
votes

I want to detect if arbitrarily rotated Text instances overlap.

Using the technique described HERE I can get the axis aligned bounding box (AABB) of a Text instance. If the text is axis aligned, then that AABB is very tight to the actual text. But once you start rotating the text, the area covered by the AABB becomes much greater than the actual area occupied by text:

Text Bboxes

So AABB are not a very accurate way to detect overlap of rotated text.

Here's a way I can imagine doing it:

  1. For each text instance t, use the AABB to calculate a set of 4 points defining the arbitrarily oriented bounding box (OBB) B_t that tightly contains the text.
  2. Test each t for intersection with each other with 2 steps:

Is the above process a good approach, or is there a better way to do this using some functionality already existing within Matplotlib?

1

1 Answers

1
votes

If you don't want to spend a lot of time thinking on this and aren't too concerned about performance, I would import shapely and create your bounding boxes as polygon objects. Then it is trivial to find overlap.

from shapely.geometry import Polygon
p1=Polygon([(0,0),(1,1),(1,0)])
p2=Polygon([(0,1),(1,0),(1,1)])
print p1.intersects(p2)

It may even help finding the OBB if you can create your polygon prior to rotating the text. (just make sure you apply the same transformations to the polygon as you do to the text.)