Might also try inlining Determinant and some common sub-expression elimination...
sub SegmentIntersection { my @points = @{$_[0]}; my @p1 = @{$points[0]}; # p1,p2 = segment 1 my @p2 = @{$points[1]}; my @p3 = @{$points[2]}; # p3,p4 = segment 2 my @p4 = @{$points[3]}; my $a = $p2[0] - $p1[0]; my $b = $p3[1] - $p4[1]; my $c = $p2[1] - $p1[1]; my $d = $p3[0] - $p4[0]; my $det = $a*$b - $c*$d; return 0 if (abs($det) < $delta); # parallel my $e = $p3[1]-$p1[1]; my $f = $p3[0]-$p1[0]; my $n1 = $f*$b - $e*$d my $n2 = $a*$e - $c*$f; if ($det > 0) { return $n1 < $det && $n2 < $det && $n1 > 0 && $n2 > 0; } else { return $n1 > $det && $n2 > $det && $n1 < 0 && $n2 < 0; } }

In reply to Re^2: Millions of line segment intersection calcs: Looking for speed tips by Anonymous Monk
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.