6

I have a big polygon (Pa). Inside the polygon there are a lot of small "holes", as shown:

Here are a few condition for the holes:

  1. The holes cannot overlap one another
  2. The holes cannot go outside the outer polygon
  3. However, the holes can touch the outer polygon edge

How to obtain the remaining polygon ( or the polygon list) in an efficient manner? The easiest way ( brute force way) is to take the Pa, and gradually computing the remaining polygon by subtracting out the holes. Although this idea is feasible, but I suspect that there is a more efficient algorithm.

Edit: I'm not asking about how to perform polygon clipping ( or subtraction) algorithm! In fact that's something I would do by brute force. I'm asking in addition to the polygon clipping method ( take the main polygon and then gradually clip the holes out), is there other more efficient way?

Graviton
  • 81,782
  • 146
  • 424
  • 602
  • Have you looked at the good old `Region` class in `System.Drawing`? Maybe `GraphicsPath` could be of help too. – leppie Dec 16 '10 at 08:03
  • @leppie, the problem is that I would not be drawing my polygon using the class in `System.Drawing`-- I am drawing it on somewhere else. – Graviton Dec 16 '10 at 08:05
  • Soon Hui: I do realize that it is a bit GDI specific, but that has never stopped me using it in a web app, etc. – leppie Dec 16 '10 at 08:10
  • @leppie, I think the algorithm behind the `Region`-- minus the GDI code, of course-- would be what I need. I'm not sure how you can use it in a web app, or OpenGL environment, or whatever. – Graviton Dec 16 '10 at 08:13
  • Soon Hui: You can get a `Graphics` instance from a `Bitmap`. Just create one. As a bonus, you can dump the output as a normal image file :) – leppie Dec 16 '10 at 08:14

4 Answers4

3

This is very hard to do in a general manner. You can find source code for a solution here:

General Polygon Clipper (GPC)

Graviton
  • 81,782
  • 146
  • 424
  • 602
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
2

Well, if you use the right representation for your polygon you would not need to do anything. Just append the list of edges of the holes to the list of edges of Pa.

The only consideration you should have is that if some hole vertex or edge can touch Pa edge, you will have to perform some simplification there.

A different problem is rendering that polygon into a bitmap!

salva
  • 9,943
  • 4
  • 29
  • 57
1

I agree with salva, but my post is going to address the drawing part. Basically, you can add up all lines of the main and the hole polygons together and thereby get a single complex polygon.

The algorithm itself is not very complicted and it is nicely explained in the Polygon Fill Teaching Tool.

Lucero
  • 59,176
  • 9
  • 122
  • 152
0

You can do like this.

  1. Draw the main polygon with a color in a bitmap.
  2. Draw the holes with another color in the same bitmap.
  3. Then extract the polygon by running marching square algorithm with the main polygons color as threshold.
  4. The output will contain all the points that belong to that polygon.
  5. You can sort the points if you want it as a continous closed polygon.
ferosekhanj
  • 1,086
  • 6
  • 11