Area of rectangle-rectangle intersection

后端 未结 6 1012
夕颜
夕颜 2020-11-29 06:26

Below are 2 rectangles. Given the coordinates of the rectangle vertices - (x1, y1)...(x8, y8), how can the area of the overlapping region (white in the figure below

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 07:24

    There is another way that you may find interesting, but maybe not applicable in this case, and that is:

    1. determine the minimum rectangle( whose sides are parallel to coordinate axes ) that contains both of the given rectangles, lets call that new one a bounding box.
    2. pick a random dot that is in the bounding box and check whether it is in both rectangles or not
    3. repeat step 2 for as long as you want( it depends on the precision you want for your result ), and have two counters, one to keep track of the number of dots inside both of the rectangles, and the other which is the number of repetitions of step 2
    4. the final solution is the area of the bounding box multiplied by the number of dots inside both rectangles and then divided by number of repetitions of step 2, or in a form of a formula:

      intersection_area = bounding_box_area * num_of_dots_inside_both / num_of_repetitions

    The result will, of course, be more precise when the number of repetitions is larger. By the way, this method is called Monte Carlo method.

提交回复
热议问题