Math::Bezier - Find the coordinates of a Bezier curve with Perl

Use the Math::Bezier module to find the coordinates of a Bezier curve in Perl. You can specify a control point and get the coordinates of the curve along it by dividing it.

use Math::Bezier;
 
# (x, y) Create a curve by passing a list of control points
my $bezier = Math::Bezier->new($x1, $y1, $x2, $y2, ..., $xn, $yn);
 
# Pass the reference instead
my $bezier = Math::Bezier->new([$x1, $y1, $x2, $y2, ..., $xn, $yn]);
 
# Determine the point(x, y) in the curve. Specify a range from 0 to 1.
my ($x, $y) = $bezier->point(0.5);
 
# Returns a list in scalar context
my $xy = $bezier->point(0.5);
 
# Returns the (x, y) points divided into 20 along the curve.
my @curve = $bezier->curve(20);
 
# Receive in reference
my $curve = $bezier->curve(20);

A example that draws a 3D Bezier curve and divides it into 100 points

Bezier curves are often used in 3D, so here is an example of drawing a 3D Bezier curve and dividing it into 100 points.

The control points are (0, 0), (10, 10), (20, 40). For a 3D Bezier curve, specify three points as control points.

The straight line connecting the first control point and the second control point is the tangent to the curve at the first control point.

The straight line connecting the second control point and the third control point is the tangent to the curve at the third control point.

use strict;
use warnings;

use Math::Bezier;

# List of control points (x, y)
my $bezier = Math::Bezier->new(0, 0, 10, 10, 20, 40);

my $curve_points = $bezier->curve(100);

for (my $i = 0; $i < @$curve_points; $i += 2) {
  my $x = $curve_points->[$i];
  my $y = $curve_points->[$i + 1];
  
  print "($x, $y)\n";
}

The output will show 100 points along the 3D Bezier curve.

(0, 0)
(0.202020202020202, 0.204060810121416)
(0.404040404040404, 0.412202836445261)
(0.606060606060606, 0.624426078971533)
(0.808080808080808, 0.840730537700235)
(1.01010101010101, 1.06111621263136)
(1.21212121212121, 1.28558310376492)
(1.41414141414141, 1.51413121110091)
(1.61616161616162, 1.74676053463932)
(1.81818181818182, 1.98347107438017)
(2.02020202020202, 2.22426283032344)
(2.22222222222222, 2.46913580246914)
(2.42424242424242, 2.71808999081726)
(2.62626262626263, 2.97112539536782)
(2.82828282828283, 3.2282420161208)
(3.03030303030303, 3.48943985307622)
(3.23232323232323, 3.75471890623406)
(3.43434343434343, 4.02407917559433)
(3.63636363636364, 4.29752066115702)
(3.83838383838384, 4.57504336292215)
(4.04040404040404, 4.8566472808897)
(4.24242424242424, 5.14233241505969)
(4.44444444444444, 5.4320987654321)
(4.64646464646465, 5.72594633200694)
(4.84848484848485, 6.02387511478421)
(5.05050505050505, 6.3258851137639)
(5.25252525252525, 6.63197632894603)
(5.45454545454545, 6.94214876033058)
(5.65656565656566, 7.25640240791756)
(5.85858585858586, 7.57473727170697)
(6.06060606060606, 7.89715335169881)
(6.26262626262626, 8.22365064789307)
(6.46464646464647, 8.55422916028977)
(6.66666666666667, 8.88888888888889)
(6.86868686868687, 9.22762983369044)
(7.07070707070707, 9.57045199469442)
(7.27272727272727, 9.91735537190083)
(7.47474747474747, 10.2683399653097)
(7.67676767676768, 10.6234057749209)
(7.87878787878788, 10.9825528007346)
(8.08080808080808, 11.3457810427507)
(8.28282828282828, 11.7130905009693)
(8.48484848484848, 12.0844811753903)
(8.68686868686869, 12.4599530660137)
(8.88888888888889, 12.8395061728395)
(9.09090909090909, 13.2231404958678)
(9.29292929292929, 13.6108560350985)
(9.49494949494949, 14.0026527905316)
(9.6969696969697, 14.3985307621671)
(9.8989898989899, 14.7984899500051)
(10.1010101010101, 15.2025303540455)
(10.3030303030303, 15.6106519742883)
(10.5050505050505, 16.0228548107336)
(10.7070707070707, 16.4391388633813)
(10.9090909090909, 16.8595041322314)
(11.1111111111111, 17.283950617284)
(11.3131313131313, 17.7124783185389)
(11.5151515151515, 18.1450872359963)
(11.7171717171717, 18.5817773696562)
(11.9191919191919, 19.0225487195184)
(12.1212121212121, 19.4674012855831)
(12.3232323232323, 19.9163350678502)
(12.5252525252525, 20.3693500663198)
(12.7272727272727, 20.8264462809917)
(12.9292929292929, 21.2876237118661)
(13.1313131313131, 21.752882358943)
(13.3333333333333, 22.2222222222222)
(13.5353535353535, 22.6956433017039)
(13.7373737373737, 23.173145597388)
(13.9393939393939, 23.6547291092746)
(14.1414141414141, 24.1403938373635)
(14.3434343434343, 24.6301397816549)
(14.5454545454545, 25.1239669421488)
(14.7474747474747, 25.621875318845)
(14.9494949494949, 26.1238649117437)
(15.1515151515152, 26.6299357208448)
(15.3535353535354, 27.1400877461483)
(15.5555555555556, 27.6543209876543)
(15.7575757575758, 28.1726354453627)
(15.959595959596, 28.6950311192735)
(16.1616161616162, 29.2215080093868)
(16.3636363636364, 29.7520661157025)
(16.5656565656566, 30.2867054382206)
(16.7676767676768, 30.8254259769411)
(16.969696969697, 31.3682277318641)
(17.1717171717172, 31.9151107029895)
(17.3737373737374, 32.4660748903173)
(17.5757575757576, 33.0211202938476)
(17.7777777777778, 33.5802469135802)
(17.979797979798, 34.1434547495154)
(18.1818181818182, 34.7107438016529)
(18.3838383838384, 35.2821140699929)
(18.5858585858586, 35.8575655545352)
(18.7878787878788, 36.4370982552801)
(18.989898989899, 37.0207121722273)
(19.1919191919192, 37.608407305377)
(19.3939393939394, 38.2001836547291)
(19.5959595959596, 38.7960412202836)
(19.7979797979798, 39.3959800020406)
(20, 40)

Math::Bezier documentation

Math::Bezier documentation

Related Informatrion