How does the set of "unintersected edges" computed by your algorithm differ from a Delaunay triangulation? If the difference is insignificant for your application (or can be quickly computed), you might be better off starting with a fast Delaunay triangulation (e.g., using Qhull) and adapting its output to your needs.

Update: Take a look at Aurenhammer and Xu's "Optimal Triangulations". It points out that the greedy triangulation – what you are computing – is not necessarily the minimum-weight triangulation (MWT). The paper also notes, however, that for uniformly distributed point sets both the greedy and Delaunay triangulations yield satisfactory approximations to the MWT.

Second update: The following code uses Qhull and computes an (approximate) DT-derived solution almost instantly:

#!/usr/bin/perl # solve.pl use warnings; use strict; use YAML; use File::Temp 'tempfile'; use List::Util 'sum'; my $points = YAML::Load( do { local $/; <> } ); my $triangles = compute_delaunay_triangulation( $points ); my $segs = unique_segments( $triangles ); my @seg_endpoints = map [ @{$points}[@$_] ], @$segs; my @seg_lengths = map seg_length($_), @seg_endpoints; my $count = @$segs; my $mean = sum(@seg_lengths) / $count; print "edge count = $count\n"; print "average edge length = $mean\n\n"; print "edge endpoints follow in YAML format\n\n"; print YAML::Dump( @seg_endpoints ); sub compute_delaunay_triangulation { # use Qhull's qdelaunay for this my ($points) = @_; my $fh = tempfile(); die "cannot open tempfile: $!" unless $fh; my $pid = open my $write_to_child, "|-"; die "cannot fork: $!" unless defined $pid; if ($pid) { # parent print $write_to_child "2\n", scalar @$points, "\n", map "@$_\n", @$points; close $write_to_child || warn "kid exited $?"; } else { # child open STDOUT, '>&', $fh or die "could not redirect stdout: $!" +; close $fh or die "error on close of tempfile: $! +"; exec qw(qdelaunay Qt i) or die "could not exec qdelaunay: $!"; } wait; seek $fh, 0, 0 or die "could not seek to start of tempfile: $!"; my $results = do { local $/; <$fh> }; close $fh; (undef, my @triangles) = split /\n/, $results; return [ map [split], @triangles ]; } sub unique_segments { my ($triangles) = @_; my %segs; for my $tri (@$triangles) { for ([0,1],[1,2],[2,0]) { my @points = sort @{$tri}[@$_]; $segs{"@points"} = \@points; } } [ values %segs ]; } sub seg_length { my ($seg) = @_; my ($x,$y,$x1,$y1) = ( $seg->[0][0], $seg->[0][1] , $seg->[1][0], $seg->[1][1] ); sqrt( ($x - $x1) ** 2 + ($y - $y1) ** 2); }

Example: solve dump_301.yml. Output:

edge count = 889 average edge length = 123.835184709118 edge endpoints follow in YAML format --- - - 2590 - 3183 - - 2707 - 3257 --- - - 2560 - 2850 - - 2586 - 2869 ... other edges omitted ...

Cheers,
Tom

Tom Moertel : Blog / Talks / CPAN / LectroTest / PXSL / Coffee / Movie Rating Decoder


In reply to Delaunay triangulation by tmoertel
in thread Millions of line segment intersection calcs: Looking for speed tips by drewhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.