Area of rectangle-rectangle intersection

后端 未结 6 1021
夕颜
夕颜 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:15

    It may help to think of the problem in terms of triangles instead of rectangles. Finding the area of a triangle given three points in space is relatively straight forward.

    You can find the intersecting area by subtracting the rectangle area by the sum of the areas of the triangles as shown in the image below.

    Triangulation

    Essentially it becomes a triangulation problem.

    There is a good intro here with some pointers on data structures and algorithms.

    EDIT:

    There are some free triangulation libraries that you could reuse.

    If you know the area of the two triangles you are starting off with you can find the total area of the union of the rectangles, so the intersection would be the total area of both rectangles - the union area.

提交回复
热议问题