Calculate bounding rectangle automatically