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