1Math::ConvexHull(3) User Contributed Perl Documentation Math::ConvexHull(3)
2
3
4
6 Math::ConvexHull - Calculate convex hulls using Graham's scan
7 (n*log(n))
8
10 use Math::ConvexHull qw/convex_hull/;
11 $hull_array_ref = convex_hull(\@points);
12
14 "Math::ConvexHull" is a simple module that calculates convex hulls from
15 a set of points in 2D space. It is a straightforward implementation of
16 the algorithm known as Graham's scan which, with complexity of
17 O(n*log(n)), is the fastest known method of finding the convex hull of
18 an arbitrary set of points. There are some methods of eliminating
19 points that cannot be part of the convex hull. These may or may not be
20 implemented in a future version.
21
22 The implementation cannot deal with duplicate points. Therefore, points
23 which are very, very close (think floating point close) to the previous
24 point are dropped since version 1.02 of the module. However, if you
25 pass in randomly ordered data which contains duplicate points, this
26 safety measure might not help you. In that case, you will have to
27 remove duplicates yourself.
28
29 EXPORT
30 None by default, but you may choose to have the "convex_hull()"
31 subroutine exported to your namespace using standard Exporter
32 semantics.
33
34 convex_hull() subroutine
35 "Math::ConvexHull" implements exactly one public subroutine which,
36 surprisingly, is called "convex_hull()". "convex_hull()" expects an
37 array reference to an array of points and returns an array reference to
38 an array of points in the convex hull.
39
40 In this context, a point is considered to be a reference to an array
41 containing an x and a y coordinate. So an example use of
42 "convex_hull()" would be:
43
44 use Data::Dumper;
45 use Math::ConvexHull qw/convex_hull/;
46
47 print Dumper convex_hull(
48 [
49 [0,0], [1,0],
50 [0.2,0.9], [0.2,0.5],
51 [0,1], [1,1],
52 ]
53 );
54
55 # Prints out the points [0,0], [1,0], [0,1], [1,1].
56
57 Please note that "convex_hull()" does not return copies of the points
58 but instead returns the same array references that were passed in.
59
61 New versions of this module can be found on http://steffen-mueller.net
62 or CPAN.
63
64 After implementing the algorithm from my CS notes, I found the exact
65 same implementation in the German translation of Orwant et al,
66 "Mastering Algorithms with Perl". Their code reads better than mine, so
67 if you looked at the module sources and don't understand what's going
68 on, I suggest you have a look at the book.
69
70 In early 2011, much of the module was rewritten to use the formulation
71 of the algorithm that was shown on the Wikipedia article on Graham's
72 scan at the time. This takes care of issues with including collinear
73 points in the hull.
74
75 <http://en.wikipedia.org/wiki/Graham_scan>
76
77 One of these days, somebody should implement Chan's algorithm
78 instead...
79
81 Steffen Mueller, <smueller@cpan.org>
82
84 Copyright (C) 2003-2011 by Steffen Mueller
85
86 This library is free software; you can redistribute it and/or modify it
87 under the same terms as Perl itself, either Perl version 5.6 or, at
88 your option, any later version of Perl 5 you may have available.
89
90
91
92perl v5.36.0 2022-07-27 Math::ConvexHull(3)