blib/lib/Math/Bezier.pm

filter: Statementstmt 100% 64 / 64 Branchbran 95.8% 23 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 96.0% 96 / 100 CC 13 SCAR 14.2
CC
13
Cov
99%
new
7.0
point
4.0
curve
4.0
CRAP 4.1
↑ Summary
1 #========================================================================
2 # Math::Bezier
3 #
4 # Module for the solution of Bezier curves based on the algorithm
5 # presented by Robert D. Miller in Graphics Gems V, "Quick and Simple
6 # Bezier Curve Drawing".
7 #
8 # Andy Wardley <abw@kfs.org>
9 #
10 # Copyright (C) 2000 Andy Wardley. All Rights Reserved.
11 #
12 # This module is free software; you can redistribute it and/or
13 # modify it under the same terms as Perl itself.
14 #
15 #========================================================================
16  
17 package Math::Bezier;
18  
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 $VERSION = '0.01';
23  
24 1 ▶use constant X => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use constant Y => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use constant CX => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant CY => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 #------------------------------------------------------------------------
31 # new($x1, $y1, $x2, $y2, ..., $xn, $yn)
32 #
33 # Constructor method to create a new Bezier curve form.
34 #------------------------------------------------------------------------
35  
36 2 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod undocumented
37 2 my $class = shift;
38 2 ▶ my @points = ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 1 2
39 2 my $size = scalar @points;
40 2 my @ctrl;
41  
42 2 ▶ die "invalid control points, expects (x1, y1, x2, y2, ..., xn, yn)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $size % 2 0 2 2
43 if $size % 2;
44  
45 2 ▶ while (@points) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points) 8 2 10
46 8 push(@ctrl, [ splice(@points, 0, 2) ]);
47 }
48 2 $size = scalar @ctrl;
49  
50 2 my $n = $size - 1;
51 2 my $choose;
52  
53 2 ▶ for (my $k = 0; $k <= $n; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $n) 8 2 10
54 8 ▶ if ($k == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k == 0) { } 2 6 8
elsif ($k == 1) { } 2 4 6
55 2 $choose = 1;
56 }
57 elsif ($k == 1) {
58 2 $choose = $n;
59 }
60 else {
61 4 $choose *= ($n - $k + 1) / $k;
62 }
63 8 $ctrl[$k]->[CX] = $ctrl[$k]->[X] * $choose;
64 8 $ctrl[$k]->[CY] = $ctrl[$k]->[Y] * $choose;
65 }
66  
67 2 bless \@ctrl, $class;
68 }
69  
70  
71 #------------------------------------------------------------------------
72 # point($theta)
73 #
74 # Calculate (x, y) point on curve at position $theta (in the range 0 - 1)
75 # along the curve. Returns a list ($x, $y) or reference to a list
76 # [$x, $y] when called in list or scalar context respectively.
77 #------------------------------------------------------------------------
78  
79 62 ▶sub point {
Subroutine point: called CC=4 SCAR=13.9
Pod undocumented
80 62 my ($self, $t) = @_;
81 62 my $size = scalar @$self;
82 62 my (@points, $point);
83  
84 62 my $n = $size - 1;
85 62 my $u = $t;
86  
87 62 push(@points, [ $self->[0]->[CX], $self->[0]->[CY] ]);
88  
89 62 ▶ for (my $k = 1; $k <= $n; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $n) 186 62 248
90 186 push(@points, [ $self->[$k]->[CX] * $u, $self->[$k]->[CY] * $u ]);
91 186 $u *= $t;
92 }
93  
94 62 $point = [ @{ $points[$n] } ];
95 62 my $t1 = 1 - $t;
96 62 my $tt = $t1;
97  
98 62 ▶ for (my $k = $n - 1; $k >= 0; $k--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k >= 0) 186 62 248
99 186 $point->[X] += $points[$k]->[X] * $tt;
100 186 $point->[Y] += $points[$k]->[Y] * $tt;
101 186 $tt = $tt * $t1;
102 }
103  
104 62 ▶ return wantarray ? (@$point) : $point;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 51 11 62
105 }
106  
107  
108 #------------------------------------------------------------------------
109 # curve($npoints)
110 #
111 # Sample curve at $npoints points. Returns a list or reference to a list
112 # of (x, y) points along the curve, when called in list or scalar context
113 # respectively.
114 #------------------------------------------------------------------------
115  
116 2 ▶sub curve {
Subroutine curve: called CC=4 SCAR=13.9
Pod undocumented
117 2 my ($self, $npoints) = @_;
118 2 ▶ $npoints = 20 unless defined $npoints;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $npoints 1 1 2
119 2 my @points;
120 2 $npoints--;
121 2 ▶ foreach (my $t = 0; $t <= $npoints; $t++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t <= $npoints) 40 2 42
122 40 push(@points, ($self->point($t / $npoints)));
123 }
124 2 ▶ return wantarray ? (@points) : \@points;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
125 }
126  
127 1 1;
128  
129 __END__
↑ Summary