Construction/Circle.pm

filter: Statementstmt 89.4% 119 / 133 Branchbran 79.1% 38 / 48 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 19 / 19 Podpod 50.0% 3 / 6 totaltotal 85.6% 185 / 216 CC 28 SCAR 17.0
CC
28
Cov
86%
_calculate_boundary_positions
11.3
draw
8.4
radius
5.1
CRAP 5.5
« Construction/Derivate/PointOnLine.pm ↑ Summary Construction/Role/AlternativeSources.pm »
1 package Math::Geometry::Construction::Circle;
2 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 10 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Math::Geometry::Construction::Point;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Math::Geometry::Construction::Types qw(Point);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶use overload 'x' => '_intersect',
Subroutine BEGIN: called CC=1 SCAR=0.0
12 # '.' => '_point_on',
13 10 ▶ 'bool' => sub { return 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 C<Math::Geometry::Construction::Circle> - circle by center and point
18  
19 =head1 VERSION
20  
21 Version 0.024
22  
23 =cut
24  
25 10 our $VERSION = '0.024';
26  
27  
28 ###########################################################################
29 # #
30 # Class Variables and Methods #
31 # #
32 ###########################################################################
33  
34 10 our $ID_TEMPLATE = 'C%09d';
35  
36 54 ▶sub id_template { return $ID_TEMPLATE }
Subroutine id_template: called CC=1 SCAR=0.0
Pod documented
37  
38 ###########################################################################
39 # #
40 # Accessors #
41 # #
42 ###########################################################################
43  
44 10 with 'Math::Geometry::Construction::Role::Object';
45 10 with 'Math::Geometry::Construction::Role::PositionSelection';
46 10 with 'Math::Geometry::Construction::Role::Output';
47 10 with 'Math::Geometry::Construction::Role::PointSet';
48 10 with 'Math::Geometry::Construction::Role::Input';
49  
50 10 has 'center' => (isa => Point,
51 is => 'ro',
52 required => 1);
53  
54 10 has 'support' => (isa => Point,
55 is => 'ro',
56 required => 1);
57  
58 10 has '_fixed_radius' => (isa => 'Num',
59 is => 'rw',
60 init_arg => 'radius',
61 trigger => \&_fixed_radius_trigger);
62  
63 10 has 'partial_draw' => (isa => 'Bool',
64 is => 'rw',
65 builder => '_build_partial_draw',
66 lazy => 1);
67  
68 10 has 'min_gap' => (isa => 'Num',
69 is => 'rw',
70 builder => '_build_min_gap',
71 lazy => 1);
72  
73 57 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=4 SCAR=14.2
Pod documented
74 57 my ($class, %args) = @_;
75
76 # implicitly checks $args{construction}
77 $args{center} = $class->import_point($args{construction},
78 57 $args{center});
79  
80 57 ▶ if(exists($args{support})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'support'}) { } 38 19 57
elsif (exists $args{'radius'}) { } 17 2 19
81 38 ▶ if(exists($args{radius})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"radius"}) 0 38 38
82 0 warn sprintf("Ignoring ircle init parameter radius.\n");
83 0 delete $args{radius};
84 }
85  
86 $args{support} = $class->import_point($args{construction},
87 38 $args{support});
88 }
89 elsif(exists($args{radius})) {
90 $args{support} = $args{construction}->add_derived_point
91 ('TranslatedPoint',
92 {input => [$args{center}],
93 17 translator => [$args{radius}, 0]},
94 {hidden => 1});
95 }
96
97 57 return \%args;
98 }
99  
100 55 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
101 55 my ($self, $args) = @_;
102  
103 55 ▶ $self->style('stroke', 'black') unless($self->style('stroke'));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->style("stroke") 53 2 55
104 55 ▶ $self->style('fill', 'none') unless($self->style('fill'));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->style("fill") 53 2 55
105  
106 # The following call also makes sure that the support has been
107 # set or can be built.
108 55 $self->register_point($self->support);
109 }
110  
111 3 ▶sub _build_partial_draw {
Subroutine _build_partial_draw: called CC=1 SCAR=0.0
112 3 my ($self) = @_;
113  
114 3 return $self->construction->partial_circles;
115 }
116  
117 7 ▶sub _build_min_gap {
Subroutine _build_min_gap: called CC=1 SCAR=0.0
118 7 my ($self) = @_;
119  
120 7 return $self->construction->min_circle_gap;
121 }
122  
123 18 ▶sub _fixed_radius_trigger {
Subroutine _fixed_radius_trigger: called CC=2 SCAR=6.9
124 18 my ($self, $new, $old) = @_;
125  
126 18 ▶ if(@_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 1 17 18
127 # change of value, not init
128 1 $self->support->derivate->translator([$new, 0]);
129 }
130 }
131  
132 ###########################################################################
133 # #
134 # Retrieve Data #
135 # #
136 ###########################################################################
137  
138 7 ▶sub positions {
Subroutine positions: called CC=1 SCAR=0.0
Pod undocumented
139 7 my ($self) = @_;
140  
141 7 return map { $_->position } $self->points;
142 }
143  
144 51 ▶sub radius {
Subroutine radius: called CC=5 SCAR=16.3
Pod documented
145 51 my ($self, @args) = @_;
146  
147 51 ▶ if(@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 2 49 51
148 2 ▶ if(defined($self->_fixed_radius)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_fixed_radius) { } 1 1 2
149 1 $self->_fixed_radius($args[0]);
150 }
151 else {
152 1 croak sprintf('Refusing to set radius on circle %s without '.
153 'fixed radius', $self->id);
154 }
155 }
156  
157 50 my $center_p = $self->center->position;
158 50 my $support_p = $self->support->position;
159  
160 50 ▶ return if(!$center_p or !$support_p);
Branch: 1 branch
BranchTrueFalseTotal
unless $center_p and $support_p 0 50 50
Condition33%1/3
$center_p and $support_p
!ll&&!rl&&r
0050
MC/DC0%0/2
$center_p and $support_p
$center_p $support_p
Truth table33%1/3
A and B
A= $center_pB= $support_p
ABresult
0X0
100
111
161 50 return(abs($support_p - $center_p));
162 }
163  
164 7 ▶sub _calculate_boundary_positions {
Subroutine _calculate_boundary_positions: called CC=11 SCAR=24.3
165 7 my ($self, %args) = @_;
166  
167 7 my @positions = grep { defined($_) } $self->positions;
168 7 ▶ return([undef, undef], [undef, undef]) if(@positions < 2);
Branch: 1 branch
BranchTrueFalseTotal
if @positions < 2 1 6 7
169  
170 # sort positions around the circle; note that @sorted_positions
171 # contains arrayrefs with position and angle
172 6 my $center_position = $self->center->position; # known to be def
173 6 my @rich_positions = ();
174 6 foreach(@positions) {
175 22 my $relative = $_ - $center_position;
176 22 my $angle = atan2($relative->[1], $relative->[0]);
177 22 ▶ $angle += 6.28318530717959 if($angle < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $angle < 0 9 13 22
178 22 push(@rich_positions, [$_, $angle]);
179 }
180 6 my @sorted_positions = sort { $a->[1] <=> $b->[1] } @rich_positions;
181  
182 6 my $n = @sorted_positions;
183 6 my @max = (undef, undef);
184 6 ▶ for(my $i=0;$i<$n;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 22 6 28
185 22 my $diff;
186 22 ▶ if($i + 1 < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < $n) { } 16 6 22
187 16 $diff = $sorted_positions[$i + 1]->[1]
188 - $sorted_positions[$i]->[1];
189 }
190 else {
191 6 $diff = $sorted_positions[0]->[1] + 6.28318530717959
192 - $sorted_positions[$i]->[1];
193 }
194 22 ▶ @max = ($i, $diff) if(!defined($max[1]) or $diff > $max[1]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max[1] or $diff > $max[1] 14 8 22
Condition100%3/3
not defined $max[1] or $diff > $max[1]
l!l&&r!l&&!r
688
MC/DC100%2/2
not defined $max[1] or $diff > $max[1]
not defined $max[1] $diff > $max[1]
Truth table100%3/3
not(A) or B
A= not defined $max[1]B= $diff > $max[1]
ABresult
1X1
011
000
195 }
196  
197 6 my $extend = $self->extend;
198 6 my $radius = $self->radius; # known to be non-zero
199  
200 # if the gap is two small we return nothing
201 6 ▶ if($max[1] - ($extend->[0] + $extend->[1]) / $radius
Branch: 1 branch
BranchTrueFalseTotal
if ($max[1] - ($extend->[0] + $extend->[1]) / $radius < $self->min_gap) 1 5 6
202 < $self->min_gap)
203 {
204 1 return([undef, undef], [undef, undef]);
205 }
206  
207 # calculate the boundary positions; note that the order needs to
208 # be reversed because we now deal with the part that needs to be
209 # drawn, not the gap
210 5 my @boundary_positions = ();
211 5 my $j = $max[0];
212 5 my $i = ($j + 1) % $n;
213 5 ▶ if($extend->[0] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extend->[0] == 0) { } 5 0 5
214 5 push(@boundary_positions, $sorted_positions[$i]->[0]);
215 }
216 else {
217 0 my $phi = $sorted_positions[$i]->[1] - $extend->[0] / $radius;
218 0 my $boundary = $center_position +
219 [$radius * cos($phi), $radius * sin($phi)];
220 0 push(@boundary_positions, $boundary);
221 }
222 5 ▶ if($extend->[1] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extend->[1] == 0) { } 5 0 5
223 5 push(@boundary_positions, $sorted_positions[$j]->[0]);
224 }
225 else {
226 0 my $phi = $sorted_positions[$j]->[1] + $extend->[1] / $radius;
227 0 my $boundary = $center_position +
228 [$radius * cos($phi), $radius * sin($phi)];
229 0 push(@boundary_positions, $boundary);
230 }
231  
232 5 return @boundary_positions;
233 }
234  
235 6 ▶sub draw {
Subroutine draw: called CC=6 SCAR=21.3
Pod undocumented
236 6 my ($self, %args) = @_;
237 6 ▶ return undef if $self->hidden;
Branch: 1 branch
BranchTrueFalseTotal
if $self->hidden 0 6 6
238  
239 6 my $center_position = $self->center->position;
240 6 my $support_position = $self->support->position;
241  
242 6 ▶ if(!$center_position) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($center_position) 0 6 6
243 0 warn sprintf("Undefined center of circle %s, ".
244 "nothing to draw.\n", $self->id);
245 0 return undef;
246 }
247 6 ▶ if(!$support_position) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($support_position) 0 6 6
248 0 warn sprintf("Undefined support of circle %s, ".
249 "nothing to draw.\n", $self->id);
250 0 return undef;
251 }
252  
253 6 my $radius = $self->radius;
254 6 ▶ if(!$radius) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($radius) 0 6 6
255 0 warn sprintf("Radius of circle %s vanishes, ".
256 "nothing to draw.\n", $self->id);
257 0 return undef;
258 }
259  
260 6 ▶ my @boundary_positions = $self->partial_draw
Branch: 1 branch
BranchTrueFalseTotal
$self->partial_draw ? : 0 6 6
261 ? $self->_calculate_boundary_positions(%args)
262 : ([undef, undef], [undef, undef]);
263  
264 # currently, we just draw the full circle
265 6 $self->construction->draw_circle
266 (cx => $center_position->[0],
267 cy => $center_position->[1],
268 r => $radius,
269 x1 => $boundary_positions[0]->[0],
270 y1 => $boundary_positions[0]->[1],
271 x2 => $boundary_positions[1]->[0],
272 y2 => $boundary_positions[1]->[1],
273 style => $self->style_hash,
274 id => $self->id);
275  
276 6 $self->draw_label('x' => $support_position->[0],
277 'y' => $support_position->[1]);
278 }
279  
280 ###########################################################################
281 # #
282 # Overloading #
283 # #
284 ###########################################################################
285  
286 3 ▶sub _intersect {
Subroutine _intersect: called CC=3 SCAR=11.0
287 3 my ($self, $intersector) = @_;
288 3 my $class;
289
290 3 $class = 'Math::Geometry::Construction::Circle';
291 3 ▶ if(eval { $intersector->isa($class) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $intersector->isa($class) } }) 2 1 3
292 2 return $self->construction->add_derived_point
293 ('IntersectionCircleCircle', {input => [$self, $intersector]});
294 }
295  
296 1 $class = 'Math::Geometry::Construction::Line';
297 1 ▶ if(eval { $intersector->isa($class) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $intersector->isa($class) } }) 1 0 1
298 1 return $self->construction->add_derived_point
299 ('IntersectionCircleLine', {input => [$self, $intersector]});
300 }
301 }
302  
303 10 1;
304  
305  
306 __END__
« Construction/Derivate/PointOnLine.pm ↑ Summary Construction/Role/AlternativeSources.pm »