Ray-casting algorithm: Difference between revisions

Content added Content deleted
Line 658: Line 658:
my ($point, $segment) = @_;
my ($point, $segment) = @_;


my $A = $segment->[0];
my ($A, $B) = @$segment;
my $B = $segment->[1];


my @P = @$point; # copy it
my @P = @$point; # copy it


($A, $B) = ($B, $A) if ( $A->[1] > $B->[1] );
($A, $B) = ($B, $A) if $A->[1] > $B->[1];


$P[1] += $eps if ( ($P[1] == $A->[1]) || ($P[1] == $B->[1]) );
$P[1] += $eps if ($P[1] == $A->[1]) || ($P[1] == $B->[1]);


return 0 if ( ($P[1] < $A->[1]) || ( $P[1] > $B->[1]) || ($P[0] > max($A->[0],$B->[1]) ) );
return 0 if ($P[1] < $A->[1]) || ( $P[1] > $B->[1]) || ($P[0] > max($A->[0],$B->[1]) );
return 1 if ( $P[0] < min($A->[0], $B->[0]) );
return 1 if $P[0] < min($A->[0], $B->[0]);


my $m_red = ($A->[0] != $B->[0]) ? ( $B->[1] - $A->[1] )/($B->[0] - $A->[0]) : $inf;
my $m_red = ($A->[0] != $B->[0]) ? ( $B->[1] - $A->[1] )/($B->[0] - $A->[0]) : $inf;
Line 681: Line 680:
sub point
sub point
{
{
return [shift, shift];
[shift, shift];
}
}
sub create_polygon
sub create_polygon
{
{
my ($pts, $sides) = @_;
my ($pts, $sides) = @_;
my @poly = ();
my @poly;
for(my $i = 0; $i < (scalar(@$sides)-1); $i += 2) {
for(my $i = 0; $i < $#$sides; $i += 2) {
push @poly, [ $pts->[$sides->[$i]-1], $pts->[$sides->[$i+1]-1] ];
push @poly, [ $pts->[$sides->[$i]-1], $pts->[$sides->[$i+1]-1] ];
}
}
return @poly;
@poly;
}
}


Line 707: Line 706:


foreach my $pol ( "squared", "squaredhole", "strange", "exagon" ) {
foreach my $pol ( "squared", "squaredhole", "strange", "exagon" ) {
no strict 'refs';
print "$pol\n";
print "$pol\n";
my @rp = eval('@' . $pol);
my @rp = @{$pol};
foreach my $tp ( @p ) {
foreach my $tp ( @p ) {
print "\t($tp->[0],$tp->[1]) " .
print "\t($tp->[0],$tp->[1]) " .