first see:
its similar task so it might get you some inspiration.
After some thinking you could try simple brute force:
definitions
let polygon points be stored in p[]
array and hole points in h[]
array.
try every combination if bridge edge between p[]
and h[]
such that it does not intersect any previous edge not any of the polygons. In case of intersection use shorter edge and remove the longer ones.
remove non "parallel" bridges
above solution should lead to triangulated like polygon with holes so we need to remove all edges but one that share the same point so:
- sort edges by used first point (index)
- remove all lines that share the same first point leave the smallest one
- sort edges by used second point (index)
- remove all lines that share the same second point leave the smallest one
In code the first 2 bullets would look like this:
int i,j,k,e;
line l;
List<point> p,h; // input points
List<line> P,H; // input polygon and holes lines
List<line> B; // output bridge lines
for (i=0;i<p.size;i++)
for (j=0;j<h.size;j++)
{
l=line(p[i],h[j]); e=0;
// first test if new edge l intersects P,H
if (!e) for (k=0;k<P.size;i++) if (intersekt(l,P[k])){ e=1; break; }
if (!e) for (k=0;k<H.size;i++) if (intersekt(l,H[k])){ e=1; break; }
// if not test if intersect already found bridges
if (!e) for (k=0;k<B.size;i++) if (intersekt(l,B[k]))
{
if (l.length()<=B[k]) B.del(k); // remove bigger edge from B
else { e=1; break; } // or throw away l for good
}
if (!e) B.add(l);
}
Beware I have not test this just wrote the code directly in Answer editor so it might need some tweaking especially the last loop (maybe you will have to find first if l
should be deleted or not and only then remove edges from B
...