Looks like everything (and everyone) points into direction of GPC and its Perl bindings. In case its license is OK (if not, whoever brave enough follow links on that page and bind to e.g. Fortran lib), but here's for a start. Your requirements are a bit odd, in that you want union if there is intersection or touch. It's solved with check for only single sub-polyline in union (assuming you neither use nor create "holes" or self-intersecting shapes). The "cleanup" is to "remove colinear points", but note that start point is not duplicated as end point.
use strict;
use warnings;
use feature 'say';
use Data::Dump 'dd';
my @sets = (
[[0,0],[0,2],[2,2],[2,0],[0,0]],
[[0,0],[3,0],[3,1],[0,1],[0,0]],
[[3,0],[3,2],[4,2],[4,0],[3,0]],
[[0,2],[0,3],[1,3],[1,2],[0,2]],
);
########################################
#use Math::Geometry::Planar::GPC::PolygonXS;
# ... skipped, don't need it
########################################
use Math::Geometry::Planar;
my $poly = Math::Geometry::Planar-> new;
$poly-> points( $sets[ 0 ]);
for ( 1 .. 3 ) {
say "********* case #$_:";
my $clip = Math::Geometry::Planar-> new;
$clip-> points( $sets[ $_ ]);
my $gpc = GpcClip(
'UNION',
$poly-> convert2gpc,
$clip-> convert2gpc,
);
my @pgons = Gpc2Polygons( $gpc );
dd $pgons[ 0 ]-> cleanup unless $#pgons
}
__END__
********* case #1:
[[[2, 2], [0, 2], [0, 0], [3, 0], [3, 1], [2, 1]]]
********* case #2:
********* case #3:
[[[1, 3], [0, 3], [0, 0], [2, 0], [2, 2], [1, 2]]]
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.