Given a dictionary 'bboxes' whose keys are bounding-box names and whose values are dictionaries with keys 'left', 'top', 'width', and 'height' (all floats), along with the overall canvas width and height, this function checks for: 1) An overlap between any two bounding boxes (it
(bboxes, overall_width, overall_height)
| 590 | return total |
| 591 | |
| 592 | def check_bounding_boxes(bboxes, overall_width, overall_height): |
| 593 | """ |
| 594 | Given a dictionary 'bboxes' whose keys are bounding-box names and whose values are |
| 595 | dictionaries with keys 'left', 'top', 'width', and 'height' (all floats), |
| 596 | along with the overall canvas width and height, this function checks for: |
| 597 | |
| 598 | 1) An overlap between any two bounding boxes (it returns a tuple of their names). |
| 599 | 2) A bounding box that extends beyond the overall width or height (it returns a tuple |
| 600 | containing just that bounding box's name). |
| 601 | |
| 602 | It stops upon finding the first error: |
| 603 | - If an overlap is found first, it returns (name1, name2). |
| 604 | - Otherwise, if an overflow is found, it returns (name,). |
| 605 | - If nothing is wrong, it returns (). |
| 606 | |
| 607 | Parameters: |
| 608 | bboxes (dict): e.g. { |
| 609 | "box1": {"left": 10.0, "top": 10.0, "width": 50.0, "height": 20.0}, |
| 610 | "box2": {"left": 55.0, "top": 15.0, "width": 10.0, "height": 10.0}, |
| 611 | ... |
| 612 | } |
| 613 | overall_width (float): The total width of the available space. |
| 614 | overall_height (float): The total height of the available space. |
| 615 | |
| 616 | Returns: |
| 617 | tuple: Either (box1, box2) if an overlap is found, |
| 618 | (box,) if a bounding box overflows, |
| 619 | or () if no problem is found. |
| 620 | """ |
| 621 | |
| 622 | # Convert bboxes into a list of (name, left, top, width, height) for easier iteration. |
| 623 | box_list = [] |
| 624 | for name, coords in bboxes.items(): |
| 625 | left = coords["left"] |
| 626 | top = coords["top"] |
| 627 | width = coords["width"] |
| 628 | height = coords["height"] |
| 629 | box_list.append((name, left, top, width, height)) |
| 630 | |
| 631 | # Helper function to check overlap between two boxes |
| 632 | def boxes_overlap(box_a, box_b): |
| 633 | # Unpack bounding-box data |
| 634 | name_a, left_a, top_a, width_a, height_a = box_a |
| 635 | name_b, left_b, top_b, width_b, height_b = box_b |
| 636 | |
| 637 | # Compute right and bottom coordinates |
| 638 | right_a = left_a + width_a |
| 639 | bottom_a = top_a + height_a |
| 640 | right_b = left_b + width_b |
| 641 | bottom_b = top_b + height_b |
| 642 | |
| 643 | # Rectangles overlap if not separated along either x or y axis |
| 644 | # If one box is completely to the left or right or above or below the other, |
| 645 | # there's no overlap. |
| 646 | no_overlap = (right_a <= left_b or # A is completely left of B |
| 647 | right_b <= left_a or # B is completely left of A |
| 648 | bottom_a <= top_b or # A is completely above B |
| 649 | bottom_b <= top_a) # B is completely above A |
no test coverage detected