Math::...n::Clip(3pm)

πŸ’– Math::...n::Clip β€” bestie edition πŸ’–

okay so real talk, this page lives in the User Contributed Perl Documentation pile, and it's about taking a polygon and ✨shoving it into a box✨. no cap.


🌸 NAME 🌸

Math::Polygon::Clip β€” frame a polygon in a square πŸ–ΌοΈ

like… picture a polygon that's being a little too much, going places it shouldn't. this module gives it a box to stay in. iconic behavior, honestly.


🧬 INHERITANCE 🧬

Math::Polygon::Clip
  is an Exporter

so yeah, it's an Exporter, which means it hands you functions directly β€” you don't gotta do the whole Math::Polygon::Clip:: song and dance, iykyk 🫢


✨ SYNOPSIS ✨

here's the vibe in code form, copy-paste energy:

my @poly  = ( [1,2], [2,4], [5,7], [1, 2] );
my @box   = ( $xmin, $ymin, $xmax, $ymax );

my $boxed = polygon_clip \@box, @poly;

that's it. that's the whole glow-up. your polygon walks in messy, walks out framed πŸ’…


πŸ“ DESCRIPTION πŸ“

Cut-off all parts of the polygon which are outside the box.

literally just that, bestie. anything hanging outside the box? gone. βœ‚οΈ bye.


πŸ› οΈ FUNCTIONS πŸ› οΈ

okay this is the good part, the main character section πŸ‘‘

  • polygon_fill_clip1(\@box, @points)

Clipping a polygon into rectangles can be done in various ways β€” there's options, there's range, we love that for us. With this algorithm (which the author designed himself, but it may not be new, humble king behavior 😌), the parts of the polygon which are outside the @box are mapped on the borders. The polygon stays in one piece. 🧩

Returned is one list of points, which is cleaned from double points, spikes and superfluous intermediate points. so it comes out tidy, no stray crumbs left behind πŸŽ€

  • polygon_fill_clip2(\@box, @points)

To be implemented. πŸ‘€ not done yet, bestie, it's giving "coming soon" energy.

The plan: the polygon falls apart in fragments, which are not connected: paths which are followed in two directions are removed. This is required by some applications, like polygons used in geographical context (country contours and such). 🌍 like when you're doing map stuff and you need the pieces to actually come apart properly.

  • polygon_fill_clip3( \@box, $out-$poly, [$in-$polys] )

To be implemented. πŸ‘€ also not done yet.

The plan here: A surrounding polygon, with possible inclussions. (that's how the original spells it, we keep it exactly as-is, no editing the queen's spelling ✍️)

  • polygon_line_clip(\@box, @points)

Returned is a list of ARRAYS (possibly 0 long) containing line pieces from the input polygon (or line). so sometimes you get zero, sometimes you get a bunch β€” it's a whole range, it's flexible, we respect it 🌈

example:

my @points = ( [1,2], [2,3], [2,0], [1,-1], [1,2] );
my @bbox   = ( 0, -2, 2, 2 );
my @l      = polygon_line_clip \@bbox, @points;
print scalar @l;      # 1, only one piece found
my @first = @{$l[0]}; # first is [2,0], [1,-1], [1,2]

see that? one piece found, and the first one is [2,0], [1,-1], [1,2]. ate. πŸ”₯


πŸ‘€ SEE ALSO πŸ‘€

This module is part of Math-Polygon version 2.00, built on September 04, 2025. Website: http://perl.overmeer.net/CPAN/

go visit, it's the source, it's the origin story πŸ’œ


πŸ“œ LICENSE πŸ“œ

For contributors see file ChangeLog.

This software is copyright (c) 2004-2025 by Mark Overmeer.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

free software!! the way that it's free 🫢 use it, remix it, live your truth.


<div align="center">

and that's a wrap, bestie πŸŽ€ this whole page was served up by perl v5.40.1, dated 2025-10-04, under the name Math::...n::Clip(3pm). love that for us. mwah πŸ’‹βœ¨

</div>

πŸ“¦ where this page comes from