MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Polygon Union

  • To: mathgroup at
  • Subject: [mg100600] Re: Polygon Union
  • From: Daniel Lichtblau <danl at>
  • Date: Tue, 9 Jun 2009 03:54:08 -0400 (EDT)
  • References: <> <h0d6qt$sp6$>

On Jun 6, 2:45 am, "Scot T. Martin" <smar... at> wrote:
> I've also been thinking about this problem. I use the continental maps
> that are available through CountryData[], and I'd rather have a continent
> without an internal political boundaries shown. I've been surprised that
> this possibility is not a built-in option.

See last remark below.

> On Fri, 5 Jun 2009, David Skulsky wrote:
> > I am looking for a Mathematica function which generates the union of
> > two (not necessarily convex) polygons.  I found a package on the web
> > (the Imtek library, I believe) which includes a convex polygon
> > intersection function, but not a polygon *union* function.  Does
> > anyone know if such a function is available or, if not, can anyone
> > suggest an algorithm to implement in Mathematica?
> > Thanks in advance,
> > David Skulsky

Consider a pair of polygons p1 and p2. For the case where boundaries
might transversally intersect, a reasonable tactic is to iterate over
vertices of p2 to see which lie in p1, and vice versa. Then look at
neighbors of such points (that are not themselves inside one and
bounding the other) to find intersecting segments. This will allow to
recast as one polygon. Reasonably efficient code for doing the inside/
outside tests can be found in the MathGroup archives:

For the case of internal boundaries, you can look for segments that
get repeated. This should usually work, unless you have, say, Ohio and
Kentucky both claiming a river right up to the land boundary on the
other state's side (in that case, instead of looking for segments you
might look for militias. Actually you can treat this as the
transversal intersection case.)

A related issue is when you try to merge polygons that have common
boundaries, but vertices disagree due to small numeric error. In such
cases it is useful to do comparisons at precision lower than that used
in specifying the boundary vertices.

Daniel Lichtblau
Wolfram Research

  • References:
  • Prev by Date: Re: Integrate Bug
  • Next by Date: Re: Re: Re: directionfields from StreamPlot
  • Previous by thread: Polygon Union
  • Next by thread: Re: Re: Polygon Union