in reply to Re: Millions of line segment intersection calcs: Looking for speed tips
in thread Millions of line segment intersection calcs: Looking for speed tips
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; } }
|
---|