java - merge adiacent rectangles into a polygon

后端 未结 2 468
醉话见心
醉话见心 2021-02-06 10:32

I have a set of rectangles that have the same width and height and are always adiacent. I know the position of all the vertices, each one having only 4. (because is a square).

2条回答
  •  粉色の甜心
    2021-02-06 10:56

    Because your shape consists of rectangles only and they are always adjacent, the algorithm of merging is much simpler than it would be without those assumptions.

    • Create a list of ALL edges from your rectangles. One rectangle has 4 edges.
    • Let the Edge be a class with properly defined compareTo() and equals().
    • Sort the edges list (uses compareTo).
    • Iterate through the list. If the same edge is present in the list TWICE, remove them both from the list.
    • The remaining edges are the edges of your polygon.

提交回复
热议问题