Algorithm-RandomPointGenerator

 view release on metacpan or  search on metacpan

lib/Algorithm/RandomPointGenerator.pm  view on Meta::CPAN

    }
}

sub plot_histogram_lineplot {                                             
    my $self = shift;
    my $pause_time = shift;
    die "\nyou must call make_output_histogram_for_generated_points() before you can call\n" .   
        "plot_histogram_lineplot()$!\n" unless $self->{_output_histogram};
    my $hist = $self->{_output_histogram};
    my @plot_points = ();
    foreach my $y (0..@$hist-1) {
        foreach my $x (0..@{$hist->[0]}-1) {
            push @plot_points, [$x, $y, $hist->[$y][$x]];
            push @plot_points, [$x+1, $y, $hist->[$y][$x]];
            push @plot_points, [$x, $y+1, $hist->[$y][$x]];
            push @plot_points, [$x+1, $y+1, $hist->[$y][$x]];
        }
    }
    @plot_points = sort {$a->[0] <=> $b->[0]} @plot_points;
    @plot_points = sort {$a->[1] <=> $b->[1] if $a->[0] == $b->[0]} @plot_points;
    my $master_file_basename = basename($self->{_hist_file}, ('.csv', '.dat', '.txt'));
    my $temp_file = "__temp_$master_file_basename.dat";
    open(OUTFILE , ">$temp_file") or die "Cannot open temporary file: $!";
    my ($first, $oldfirst);
    $oldfirst = $plot_points[0]->[0];
    foreach my $sample (@plot_points) {
        $first = $sample->[0];
        if ($first == $oldfirst) {
            my @out_sample;
            $out_sample[0] =  $self->{_bounding_box}->[0][0] +  $sample->[0] * 
                                                               $self->{_bin_width_for_output_hist};
            $out_sample[1] =  $self->{_bounding_box}->[1][0] +  $sample->[1] * 
                                                              $self->{_bin_height_for_output_hist};
            $out_sample[2] =  $sample->[2];
            print OUTFILE "@out_sample\n";
        } else {
            print OUTFILE "\n";             
        }
        $oldfirst = $first;
    }
    print OUTFILE "\n";             
    close OUTFILE;                                                 
my $argstring = <<"END";
set hidden3d
splot "$temp_file" with lines
END
    my $plot;
    if (!defined $pause_time) {
        $plot = Graphics::GnuplotIF->new( persist => 1 );
    } else {
        $plot = Graphics::GnuplotIF->new();
    }
#    my $plot = Graphics::GnuplotIF->new(persist => 1);
    $plot->gnuplot_cmd( $argstring );
    $plot->gnuplot_pause( $pause_time ) if defined $pause_time;
}

sub DESTROY {
    my $self = shift;
    my $master_file_basename = basename($self->{_hist_file}, ('.csv', '.dat', '.txt'));
    unlink glob "__temp_$master_file_basename*";
}

sub deep_copy_AoA {
    my $ref_in = shift;
    my $ref_out;
    foreach my $i (0..@{$ref_in}-1) {
        foreach my $j (0..@{$ref_in->[$i]}-1) {
            $ref_out->[$i]->[$j] = $ref_in->[$i]->[$j];
        }
    }
    return $ref_out;
}

# from perl docs:
sub fisher_yates_shuffle {                
    my $arr =  shift;                
    my $i = @$arr;                   
    while (--$i) {                   
        my $j = int rand( $i + 1 );  
        @$arr[$i, $j] = @$arr[$j, $i]; 
    }
}

sub check_for_illegal_params {
    my @params = @_;
    my @legal_params = qw / input_histogram_file
                            bounding_box_file
                            number_of_points
                            how_many_to_discard
                            proposal_density_width
                            y_axis_pos_direction
                            output_hist_bins_along_x
                            command_line_mode
                            debug
                          /;
    my $found_match_flag;
    foreach my $param (@params) {
        foreach my $legal (@legal_params) {
            $found_match_flag = 0;
            if ($param eq $legal) {
                $found_match_flag = 1;
                last;
            }
        }
        last if $found_match_flag == 0;
    }
    return $found_match_flag;
}

1;

=pod

=head1 NAME

Algorithm::RandomPointGenerator -- This module generates a set of random points in a
2D plane according to a user-specified probability distribution that is provided to
the module in the form of a 2D histogram.




( run in 1.026 second using v1.01-cache-2.11-cpan-d8267643d1d )