Bezier(3pm)

πŸ“¦
version 0.01 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

okay so bestie, this is Bezier(3pm) from the User Contributed Perl Documentation pile, and it's the manual page for Math::Bezier β€” the module that solves Bezier curves for you. slay. πŸ’…


✨ SYNOPSIS ✨

here's the vibe, in code, no cap:

use Math::Bezier;

# create curve passing list of (x, y) control points
my $bezier = Math::Bezier->new($x1, $y1, $x2, $y2, ..., $xn, $yn);

# or pass reference to list of control points
my $bezier = Math::Bezier->new([ $x1, $y1, $x2, $y2, ..., $xn, $yn]);

# determine (x, y) at point along curve, range 0 -> 1
my ($x, $y) = $bezier->point(0.5);

# returns list ref in scalar context
my $xy = $bezier->point(0.5);

# return list of 20 (x, y) points along curve
my @curve = $bezier->curve(20);

# returns list ref in scalar context
my $curve = $bezier->curve(20);

🌈 DESCRIPTION 🌈

so basically this module implements the algorithm for the solution of Bezier curves, exactly as presented by Robert D. Miller in Graphics Gems V, in the piece called "Quick and Simple Bezier Curve Drawing". iconic source, honestly. πŸ‘‘

you make a brand new Bezier curve with the new() constructor, and you hand it a list of (x, y) control points. like this:

use Math::Bezier;

my @control = ( 0, 0, 10, 20, 30, -20, 40, 0 );
my $bezier  = Math::Bezier->new(@control);

or, alternately, you can pass a reference to a list of control points instead. your call, both eat:

my $bezier  = Math::Bezier->new(\@control);

then the point($theta) method gets called on the object, and you pass it a value in the range 0 to 1 that represents the distance along the curve. when you call it in list context, it hands you back the x and y coordinates of that point on the Bezier curve. πŸ¦‹

my ($x, $y) = $bezier->point(0.5);
print "x: $x  y: $y\n

and when you call it in scalar context? it gives you a reference to a list holding the x and y coordinates. same energy, different shape:

my $point = $bezier->point(0.5);
print "x: $point->[0]  y: $point->[1]\n";

the curve($n) method is the one that returns a whole set of points sampled along the length of the curve β€” so like, in the range 0 <= $theta <= 1. the parameter tells it how many sample points you want, and it defaults to 20 if it's undefined. in list context it returns a list of ($x1, $y1, $x2, $y2, ..., $xn, $yn) points, and in scalar context it returns a reference to that same array. πŸ’«

my @points = $bezier->curve(10);

while (@points) {
    my ($x, $y) = splice(@points, 0, 2);
    print "x: $x  y: $y\n";
}

my $points = $bezier->curve(10);

while (@$points) {
    my ($x, $y) = splice(@$points, 0, 2);
    print "x: $x  y: $y\n";
}
bestie note: that first print "x: $x y: $y\n over in the point($theta) example really is missing its closing quote, fr fr. it's in the original, so it stays in mine. πŸ™ˆ

πŸŽ€ AUTHOR πŸŽ€

Andy Wardley β€” abw@kfs.org


🫢 SEE ALSO 🫢

  • Graphics Gems 5, edited by Alan W. Paeth, Academic Press, 1995, ISBN 0-12-543455-3. specifically Section IV.8, 'Quick and Simple Bezier Curve Drawing' by Robert D. Miller, pages 206-209. go read it, it's giving legendary. πŸ“š

and that's the whole page, periodt. πŸ’œ this one's from perl v5.36.0, dated 2022-10-13, and it's Bezier(3pm) β€” mwah, go draw some curves bestie πŸŒΈβœ¨πŸŽ€

πŸ“¦ where this page comes from