This file is indexed.

/usr/share/perl5/Math/ConvexHull.pm is in libmath-convexhull-perl 1.4-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package Math::ConvexHull;
use 5.006;
use strict;
use warnings;

use constant PI => 3.1415926535897932384626433832795;

require Exporter;

our $VERSION = '1.04';


our @ISA = qw(Exporter);

our @EXPORT_OK = qw(
        convex_hull
);

our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );


sub convex_hull {
        my $points = shift;
        
        my $start_index = _find_start_point($points); # O(n)

        my $angles_points = _calculate_angles($points, $start_index); # O(n)
        my $start = splice(@$angles_points, $start_index, 1);

        @$angles_points =
                        sort {
                                $a->[0] <=> $b->[0] ||
                                $a->[1][0] <=> $b->[1][0] ||
                                $a->[1][1] <=> $b->[1][1]
                        }
                        @$angles_points;             # O(n*log(n))

        unshift @$angles_points, $start;

        # remove duplicates (O(n))
        # At the same time, drop the angle
        my $prev = $angles_points->[0][1];
        my @hull;
        push @hull, $prev;
        for my $r (@$angles_points) {
                my $p = $r->[1];
                push @hull, $p
                        if (   $p->[0]+1e-15 <= $prev->[0] || $p->[0]-1e-15 >= $prev->[0]
                            || $p->[1]+1e-15 <= $prev->[1] || $p->[1]-1e-15 >= $prev->[1]);
                $prev = $p;
        }

        # copy of the reference point as sentinel to stop loop
        unshift @hull, $hull[0];

        my $n_in_hull = 2;
        # O(n)
        for (my $i = 3; $i < @hull; ++$i) {
                while (
                        _ccw(
                                $hull[$n_in_hull-1],
                                $hull[$n_in_hull],
                                $hull[$i]
                        ) <= 0
                ) {
                        if ($n_in_hull == 2) {
                                ($hull[$i], $hull[$n_in_hull]) = (@hull[$n_in_hull, $i]);
                                ++$i;
                        }
                        else {
                                --$n_in_hull;
                        }
                }
                ++$n_in_hull;
                ($hull[$i], $hull[$n_in_hull]) = (@hull[$n_in_hull, $i]);
        }

        # return points in hull
        return [@hull[1..$n_in_hull]];
}

sub _ccw {
        my $p1 = shift;
        my $p2 = shift;
        my $p3 = shift;
        
        return(
                ($p2->[0] - $p1->[0])*($p3->[1] - $p1->[1])
                -
                ($p2->[1] - $p1->[1])*($p3->[0] - $p1->[0])
        );
}

sub _calculate_angles {
        my $points = shift;
        my $start  = shift;

        my $s_x = $points->[$start]->[0];
        my $s_y = $points->[$start]->[1];

        my $angles = [];
        
        my $p_no = 0;
        foreach my $p (@$points) {
                my $angle;
                if ($p_no == $start) {
                        $angle = 0;
                }
                else {
                        my $x_diff = $p->[0] - $s_x;
                        my $y_diff = $p->[1] - $s_y;

                        $angle = atan2($y_diff, $x_diff);
                        $angle = PI-$angle if $angle < 0;
                }

                push @$angles, [$angle, $p];
                $p_no++;
        }

        return $angles;
}



# Returns the index of the starting point.
sub _find_start_point {
        my $points = shift;
        
        # Looking for the lowest, then leftmost point.
        
        my $s_point = 0;

        for (my $i = 1; $i < @$points; $i++) {
                my ($p0, $p1)   = @{ $points->[$i] };
                my ($sp0, $sp1) = @{ $points->[$s_point] };
                if (
                        $p1 <= $sp1 and
                        $p1 < $sp1 ||
                        $p0 < $sp0
                ) {
                        $s_point = $i;
                }
        }

        return $s_point;
}



1;
__END__

=head1 NAME

Math::ConvexHull - Calculate convex hulls using Graham's scan (n*log(n))

=head1 SYNOPSIS

  use Math::ConvexHull qw/convex_hull/;
  $hull_array_ref = convex_hull(\@points);

=head1 DESCRIPTION

C<Math::ConvexHull> is a simple module that calculates convex hulls from a set
of points in 2D space. It is a straightforward implementation of the algorithm
known as Graham's scan which, with complexity of O(n*log(n)), is the fastest
known method of finding the convex hull of an arbitrary set of points.
There are some methods of eliminating points that cannot be part of the
convex hull. These may or may not be implemented in a future version.

The implementation cannot deal with duplicate points. Therefore, points
which are very, very close (think floating point close) to the
previous point are dropped since version 1.02 of the module.
However, if you pass in randomly ordered data which contains duplicate points,
this safety measure might not help you. In that case, you will have to
remove duplicates yourself.

=head2 EXPORT

None by default, but you may choose to have the C<convex_hull()> subroutine
exported to your namespace using standard Exporter semantics.

=head2 convex_hull() subroutine

C<Math::ConvexHull> implements exactly one public subroutine which, surprisingly,
is called C<convex_hull()>. C<convex_hull()> expects an array reference to an array
of points and returns an array reference to an array of points in the convex
hull.

In this context, a point is considered to be a reference to an
array containing an x and a y coordinate. So an example use of
C<convex_hull()> would be:

  use Data::Dumper;
  use Math::ConvexHull qw/convex_hull/;
  
  print Dumper convex_hull(
  [
    [0,0],     [1,0],
    [0.2,0.9], [0.2,0.5],
    [0,1],     [1,1],
  ]
  );
  
  # Prints out the points [0,0], [1,0], [0,1], [1,1].

Please note that C<convex_hull()> does not return I<copies> of the points but
instead returns the same array references that were passed in.

=head1 SEE ALSO

New versions of this module can be found on http://steffen-mueller.net or CPAN.

After implementing the algorithm from my CS notes, I found the exact same
implementation in the German translation of
Orwant et al, "Mastering Algorithms with Perl". Their code reads better than
mine, so if you looked at the module sources and don't understand
what's going on, I suggest you have a look at the book.

In early 2011, much of the module was rewritten to use the formulation of
the algorithm that was shown on the Wikipedia article on Graham's scan at
the time. This takes care of issues with including collinear points in the
hull.

L<http://en.wikipedia.org/wiki/Graham_scan>

One of these days, somebody should implement Chan's algorithm instead...

=head1 AUTHOR

Steffen Mueller, E<lt>smueller@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2003-2011 by Steffen Mueller

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.6 or,
at your option, any later version of Perl 5 you may have available.

=cut