Math::ConvexHull(3pm)
π Math::ConvexHull π
okay bestie, welcome to Math::ConvexHull(3pm) β the lil Perl module that does Calculate convex hulls using Graham's scan (n*log(n)). yes that's the whole name, we're keeping it, it's iconic. π¦
β¨ SYNOPSIS β¨
use Math::ConvexHull qw/convex_hull/;
$hull_array_ref = convex_hull(\@points);
that's it. that's the whole vibe. two lines and you're hull-ing. π
πΈ DESCRIPTION πΈ
so Math::ConvexHull is a simple little module that calculates convex hulls from a set of points in 2D space. it's a straightforward implementation of the algorithm known as Graham's scan which, with complexity of O(n*log(n)), is literally the fastest known method of finding the convex hull of an arbitrary set of points. no cap, that's the record.
there are some methods of eliminating points that cannot be part of the convex hull. those may or may not be implemented in a future version β so like, don't hold your breath, bestie. π
β οΈ big important warning: 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 β and this is the part that gets you β 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. no shortcuts, no vibes, just do it. π
π EXPORT π
None by default. but you may choose to have the convex_hull() subroutine exported to your namespace using standard Exporter semantics. your namespace, your rules, slay. β¨
π§ convex_hull() subroutine π§
Math::ConvexHull implements exactly one public subroutine which, surprisingly, is called convex_hull(). one subroutine. main character energy only. π
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 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 convex_hull() does not return copies of the points but instead returns the same array references that were passed in. same refs, same energy, zero cloning. π
π SEE ALSO π
new versions of this module can be found on http://steffen-mueller.net or CPAN. go get the fresh ones bestie. π
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. honestly? respect. π
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. glow-up achieved. β¨
<http://en.wikipedia.org/wiki/Graham_scan>
one of these days, somebody should implement Chan's algorithm instead... not me tho. not today. π
π AUTHOR π
Steffen Mueller, <smueller@cpan.org>
π 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.
okay that's the whole page, you made it to the end, so proud of you π₯Ή this was Math::ConvexHull(3pm) as documented by perl v5.32.0 on 2021-01-09 β shoutout to the perl gang for the docs, and remember: hull responsibly, dedupe your points, and stay iconic. bye bestie!! πβ¨ππ¦πΈπ
π«Ά
π¦ where this page comes from
- debian bookworm: libmath-convexhull-perl 1.4-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmath-convexhull-perl 1.4-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libmath-convexhull-perl 1.4-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmath-convexhull-perl 1.4-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms