Search code examples
objective-calgorithmgraphicsgeometryconvex-polygon

How to draw an outline of a group of multiple rectangles?


I need to draw an enclosing polygon of a group of rectangles that are placed next to each other.

Let's think of text fields that share at least one edge (or part of it) with at least one of the other rectangles. I can get the rectangles points coordinates, and so I basically have any data I need about them.

Can you think of a simple algorithm / procedure to draw a polygon (connected straight paths) around these objects.

Here's a demonstration of different potential cases (A, B, C, etc...). In example A I also drew a blue polygon which is the path that I need to draw, outlining the group of rectangles.

All different cases

I've read here about convex hull and stuff like that but really, this looks like a far simpler problem. One (beginning of) solution I thought of was that the points I actually need to draw through are only ones that are not shared by any pair of rectangles, meaning points that are vertices of more than one rectangle are redundant. What I couldn't find out was the order by which I need to draw lines from one to the next.

I currently work on objective c, but any other language or algo would be appreciated, including pseudo.

Thanks!


Solution

  • IMHO it should be like this. Make a list of edged and see if some are overlaying: This should be simple if the rectangles are aligned with the x,y axis. You just find the edges that have the vertexes on the same x or y and the other coordinates need to be in between. After this the remaining edges should form the outline.

    Another method to find common edges is to break all rectangles along each x and y axis where you have vertices. This should look as if you are growing all lines to infinity. After this all common edges will have common vertices and can be eliminated.