blib/lib/GD/Graph/Polar.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package GD::Graph::Polar;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw{Package::New};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Geo::Constants qw{PI};
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Geo::Functions qw{rad_deg};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use GD qw{gdSmallFont};
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use List::Util qw{first};
9  
10 our $VERSION = '0.19';
11  
12 =head1 NAME
13  
14 GD::Graph::Polar - Perl package to create polar graphs using GD package
15  
16 =head1 SYNOPSIS
17  
18 use GD::Graph::Polar;
19 my $obj = GD::Graph::Polar->new(size=>480, radius=>100);
20 $obj->addPoint (50=>25);
21 $obj->addPoint_rad (50=>3.1415);
22 $obj->addGeoPoint (75=>25);
23 $obj->addGeoPoint_rad (75=>3.1415);
24 $obj->addLine($r0=>$t0, $r1=>$t1);
25 $obj->addLine_rad($r0=>$t0, $r1=>$t1);
26 $obj->addGeoLine($r0=>$t0, $r1=>$t1);
27 $obj->addGeoLine_rad($r0=>$t0, $r1=>$t1);
28 $obj->addArc($r0=>$t0, $r1=>$t1);
29 $obj->addArc_rad($r0=>$t0, $r1=>$t1);
30 $obj->addGeoArc($r0=>$t0, $r1=>$t1);
31 $obj->addGeoArc_rad($r0=>$t0, $r1=>$t1);
32 $obj->addString($r=>$t, "Hello World!");
33 $obj->addString_rad($r=>$t, "Hello World!");
34 $obj->addGeoString($r=>$t, "Hello World!");
35 $obj->addGeoString_rad($r=>$t, "Hello World!");
36 $obj->font(gdSmallFont); #sets the current font from GD exports
37 $obj->color("blue"); #sets the current color from Graphics::ColorNames
38 $obj->color([0,0,0]); #sets the current color [red,green,blue]
39 print $obj->draw;
40  
41 =head1 DESCRIPTION
42  
43 This package is a wrapper around GD to produce polar graphs with an easy interface. I use this package to plot antenna patterns on a graph with data from the L<RF::Antenna::Planet::MSI::Format> package.
44  
45 =head1 CONSTRUCTOR
46  
47 =head2 new
48  
49 The new constructor.
50  
51 my $obj = GD::Graph::Polar->new( #default values
52 size => 480, #width and height in pixels
53 radius => 1, #scale of the radius
54 ticks => 10, #number of major ticks
55 border => 2, #pixel border around graph
56 rgbfile => "/usr/X11R6/lib/X11/rgb.txt"
57 );
58  
59 =head1 METHODS
60  
61 =head2 addPoint
62  
63 Method to add a point to the graph.
64  
65 $obj->addPoint(50=>25);
66  
67 =cut
68  
69 sub addPoint {
70 my $self = shift;
71 my $r = shift;
72 my $t = rad_deg(shift());
73 return $self->addPoint_rad($r,$t);
74 }
75  
76 =head2 addPoint_rad
77  
78 Method to add a point to the graph.
79  
80 $obj->addPoint_rad(50=>3.1415);
81  
82 =cut
83  
84 sub addPoint_rad {
85 my $self = shift;
86 my $r = shift;
87 my $t = shift;
88 my ($x, $y) = $self->_imgxy_rt_rad($r,$t);
89 my $icon = 7;
90 return $self->gdimage->arc($x,$y,$icon,$icon,0,360,$self->color);
91 }
92  
93 =head2 addGeoPoint
94  
95 Method to add a point to the graph.
96  
97 $obj->addGeoPoint(75=>25);
98  
99 =cut
100  
101 sub addGeoPoint {
102 my $self = shift;
103 my $r = shift;
104 my $t = rad_deg(shift());
105 return $self->addGeoPoint_rad($r,$t);
106 }
107  
108 =head2 addGeoPoint_rad
109  
110 Method to add a point to the graph.
111  
112 $obj->addGeoPoint_rad(75=>3.1415);
113  
114 =cut
115  
116 sub addGeoPoint_rad {
117 my $self = shift;
118 my $r = shift;
119 my $t = PI()/2-shift();
120 return $self->addPoint_rad($r,$t);
121 }
122  
123 =head2 addLine
124  
125 Method to add a line to the graph.
126  
127 $obj->addLine(50=>25, 75=>35);
128  
129 =cut
130  
131 sub addLine {
132 my $self = shift;
133 my $r0 = shift;
134 my $t0 = rad_deg(shift());
135 my $r1 = shift;
136 my $t1 = rad_deg(shift());
137 return $self->addLine_rad($r0=>$t0, $r1=>$t1);
138 }
139  
140 =head2 addLine_rad
141  
142 Method to add a line to the graph.
143  
144 $obj->addLine_rad(50=>3.14, 75=>3.45);
145  
146 =cut
147  
148 sub addLine_rad {
149 my $self = shift;
150 my $r0 = shift;
151 my $t0 = shift;
152 my $r1 = shift;
153 my $t1 = shift;
154 my ($x0=>$y0) = $self->_imgxy_rt_rad($r0=>$t0);
155 my ($x1=>$y1) = $self->_imgxy_rt_rad($r1=>$t1);
156 return $self->gdimage->line($x0, $y0, $x1, $y1, $self->color);
157 }
158  
159 =head2 addGeoLine
160  
161 Method to add a line to the graph.
162  
163 $obj->addGeoLine(50=>25, 75=>35);
164  
165 =cut
166  
167 sub addGeoLine {
168 my $self = shift;
169 my $r0 = shift;
170 my $t0 = rad_deg(shift());
171 my $r1 = shift;
172 my $t1 = rad_deg(shift());
173 return $self->addGeoLine_rad($r0=>$t0, $r1=>$t1);
174 }
175  
176 =head2 addGeoLine_rad
177  
178 Method to add a line to the graph.
179  
180 $obj->addGeoLine_rad(50=>3.14, 75=>3.45);
181  
182 =cut
183  
184 sub addGeoLine_rad {
185 my $self = shift;
186 my $r0 = shift;
187 my $t0 = PI()/2-shift();
188 my $r1 = shift;
189 my $t1 = PI()/2-shift();
190 return $self->addLine_rad($r0=>$t0, $r1=>$t1);
191 }
192  
193 =head2 addArc
194  
195 Method to add an arc to the graph.
196  
197 $obj->addArc(50=>25, 75=>35);
198  
199 =cut
200  
201 sub addArc {
202 my $self = shift;
203 my $r0 = shift;
204 my $t0 = rad_deg(shift());
205 my $r1 = shift;
206 my $t1 = rad_deg(shift());
207 return $self->addArc_rad($r0=>$t0, $r1=>$t1);
208 }
209  
210 =head2 addArc_rad
211  
212 Method to add an arc to the graph.
213  
214 $obj->addArc_rad(50=>3.14, 75=>3.45);
215  
216 =cut
217  
218 sub addArc_rad {
219 my $self = shift;
220 my $r0 = shift;
221 my $t0 = shift;
222 my $r1 = shift;
223 my $t1 = shift;
224 my $m = ($r1-$r0) / ($t1-$t0);
225 my $inc = 0.02; #is this good?
226 my $steps = int(($t1-$t0) / $inc);
227 my @array = ();
228 foreach (0 .. $steps) {
229 my $t = $_ / $steps * ($t1-$t0) + $t0;
230 my $r = $r0 + $m * ($t-$t0);
231 push @array, [$r=>$t];
232 }
233 my @return = ();
234 foreach (1 .. $steps) {
235 push @return, $self->addLine_rad(@{$array[$_-1]}, @{$array[$_]});
236 }
237 return \@return;
238 }
239  
240 =head2 addGeoArc
241  
242 Method to add an arc to the graph.
243  
244 $obj->addGeoArc(50=>25, 75=>35);
245  
246 =cut
247  
248 sub addGeoArc {
249 my $self = shift;
250 my $r0 = shift;
251 my $t0 = rad_deg(shift());
252 my $r1 = shift;
253 my $t1 = rad_deg(shift());
254 return $self->addGeoArc_rad($r0=>$t0, $r1=>$t1);
255 }
256  
257 =head2 addGeoArc_rad
258  
259 Method to add an arc to the graph.
260  
261 $obj->addGeoArc_rad(50=>25, 75=>35);
262  
263 =cut
264  
265 sub addGeoArc_rad {
266 my $self = shift;
267 my $r0 = shift;
268 my $t0 = PI()/2-shift();
269 my $r1 = shift;
270 my $t1 = PI()/2-shift();
271 return $self->addArc_rad($r0=>$t0, $r1=>$t1);
272 }
273  
274 =head2 addString
275  
276 Method to add a string to the graph.
277  
278 =cut
279  
280 sub addString {
281 my $self = shift;
282 my $r = shift;
283 my $t = rad_deg(shift());
284 my $string = shift;
285 return $self->addString_rad($r=>$t, $string);
286 }
287  
288 =head2 addString_rad
289  
290 Method to add a string to the graph.
291  
292 =cut
293  
294 sub addString_rad {
295 my $self = shift;
296 my $r = shift;
297 my $t = shift;
298 my $string = shift;
299 my ($x=>$y) = $self->_imgxy_rt_rad($r=>$t);
300 return $self->gdimage->string($self->font, $x, $y, $string, $self->color);
301 }
302  
303 =head2 addGeoString
304  
305 Method to add a string to the graph.
306  
307 =cut
308  
309 sub addGeoString {
310 my $self = shift;
311 my $r = shift;
312 my $t = rad_deg(shift());
313 my $string = shift;
314 return $self->addGeoString_rad($r=>$t, $string);
315 }
316  
317 =head2 addGeoString_rad
318  
319 Method to add a string to the graph.
320  
321 =cut
322  
323 sub addGeoString_rad {
324 my $self = shift;
325 my $r = shift;
326 my $t = PI()/2-shift();
327 my $string = shift;
328 return $self->addString_rad($r=>$t, $string);
329 }
330  
331 =head1 Objects
332  
333 =head2 gdimage
334  
335 Returns a L<GD> object
336  
337 =cut
338  
339 sub gdimage {
340 my $self = shift;
341 $self->{'gdimage'} = shift if @_; #set a base chart or watermark
342 unless ($self->{'gdimage'}) {
343 $self->{'gdimage'} = GD::Image->new($self->size, $self->size);
344  
345 # make the background transparent and interlaced
346 $self->gdimage->transparent($self->color([255,255,255]));
347 $self->gdimage->interlaced('true');
348
349 # Put a frame around the picture
350 $self->gdimage->rectangle(0, 0, $self->size - 1, $self->size - 1, $self->color([0,0,0]));
351
352 if ($self->ticks > 0) {
353 $self->color([192,192,192]);
354 foreach (0 .. $self->ticks) {
355 my $c = $self->size / 2;
356 my $r = $self->_width * $_ / $self->ticks;
357 $self->gdimage->arc($c,$c,$r,$r,0,360,$self->color);
358 }
359 }
360
361 $self->color([192,192,192]);
362 $self->gdimage->line($self->size/2,
363 $self->border,
364 $self->size/2,
365 $self->size-$self->border,
366 $self->color);
367 $self->gdimage->line($self->border,
368 $self->size/2,
369 $self->size-$self->border,
370 $self->size/2,
371 $self->color);
372  
373 #default to black pen color
374 $self->color([0,0,0]);
375 }
376 return $self->{'gdimage'};
377 }
378  
379 =head2 gcnames
380  
381 Returns a L<Graphics::ColorNames> object.
382  
383 =cut
384  
385 sub gcnames {
386 my $self = shift;
387 unless (defined $self->{'gcnames'}) {
388 eval 'use Graphics::ColorNames';
389 if ($@) {
390 die('Error: Cannot load Graphics::ColorNames');
391 } else {
392 my $file = $self->rgbfile; #stringify for object support
393 $self->{'gcnames'} = Graphics::ColorNames->new("$file");
394 }
395 }
396 return $self->{'gcnames'};
397 }
398  
399 =head1 Properties
400  
401 =head2 color
402  
403 Method to set or return the current drawing color
404  
405 my $colorobj = $obj->color("blue"); #if Graphics::ColorNames available
406 my $colorobj = $obj->color([77,82,68]); #rgb=>[decimal,decimal,decimal]
407 my $colorobj = $obj->color;
408  
409 Default: [0,0,0] (i.e., black)
410  
411 =cut
412  
413 sub color {
414 my $self = shift;
415 if (@_) {
416 my $color = shift;
417 if (ref($color) eq 'ARRAY') {
418 my ($r, $g, $b) = @$color;
419 $self->{'color'} = $self->{'colors'}->{$r}->{$g}->{$b}||=$self->gdimage->colorAllocate(@$color);
420 } else {
421 if ($self->gcnames) {
422 my @rgb = $self->gcnames->rgb($color);
423 @rgb = (0,0,0) unless scalar(@rgb) == 3;
424 $self->{'color'} = $self->color(\@rgb);
425 } else {
426 $self->{'color'} = $self->color([0,0,0]);
427 }
428 }
429 }
430 return $self->{'color'};
431 }
432  
433 =head2 font
434  
435 Method to set or return the current drawing font (only needed by the very few)
436  
437 use GD qw(gdGiantFont gdLargeFont gdMediumBoldFont gdSmallFont gdTinyFont);
438 $obj->font(gdSmallFont); #the default
439 $obj->font;
440  
441 Default: gdSmallFont
442  
443 =cut
444  
445 sub font {
446 my $self = shift;
447 $self->{'font'} = shift if @_;
448 $self->{'font'} = gdSmallFont unless $self->{'font'};
449 return $self->{'font'};
450 }
451  
452 =head2 size
453  
454 Sets or returns the width and height of the image in pixels.
455  
456 Default: 480
457  
458 =cut
459  
460 sub size {
461 my $self = shift;
462 $self->{'size'} = shift if @_;
463 $self->{'size'} = 480 unless $self->{'size'};
464 return $self->{'size'};
465 }
466  
467 =head2 radius
468  
469 Sets or returns the radius of the graph which sets the scale of the maximum value of the graph.
470  
471 Default: 1
472  
473 =cut
474  
475 sub radius {
476 my $self = shift;
477 $self->{'radius'} = shift if @_;
478 $self->{'radius'} = 1 unless $self->{'radius'};
479 return $self->{'radius'};
480 }
481  
482 =head2 border
483  
484 Sets and returns the number of pixels that border the graph on the image.
485  
486 Default: 2
487  
488 =cut
489  
490 sub border {
491 my $self = shift;
492 $self->{'border'} = shift if @_;
493 $self->{'border'} = 2 unless defined($self->{'border'});
494 return $self->{'border'};
495 }
496  
497 =head2 ticks
498  
499 Sets and returns the number of ticks on the graph.
500  
501 Default: 10
502  
503 =cut
504  
505 sub ticks {
506 my $self = shift;
507 $self->{'ticks'} = shift if @_;
508 $self->{'ticks'} = 10
509 unless defined($self->{'ticks'});
510 return $self->{'ticks'};
511 }
512  
513 =head2 rgbfile
514  
515 Sets or returns an RGB file.
516  
517 Note: This method will search in a few locations for a file.
518  
519 =cut
520  
521 sub rgbfile {
522 my $self = shift;
523 $self->{'rgbfile'} = shift if @_;
524 unless (defined $self->{'rgbfile'}) {
525 $self->{'rgbfile'} = 'rgb.txt';
526 my $rgb = first {-r} (qw{/etc/X11/rgb.txt /usr/share/X11/rgb.txt /usr/X11R6/lib/X11/rgb.txt ../rgb.txt});
527 $self->{'rgbfile'} = $rgb if $rgb;
528 }
529 return $self->{'rgbfile'};
530 }
531  
532 =head2 draw
533  
534 Method returns a PNG binary blob.
535  
536 my $png_binary = $obj->draw;
537  
538 =cut
539  
540 sub draw {
541 my $self = shift;
542 return $self->gdimage->png;
543 }
544  
545 #=head2 _scale
546 #
547 #Method returns the parameter scaled to the image.
548 #
549 #=cut
550  
551 sub _scale {
552 my $self = shift;
553 my $r = shift;
554 return $self->_width / 2 / $self->radius * $r;
555 }
556  
557 #=head2 _width
558 #
559 #Method returns the width of the graph.
560 #
561 #=cut
562  
563 sub _width {
564 my $self = shift;
565 return $self->size - $self->border * 2;
566 }
567  
568 #=head2 _imgxy_xy
569 #
570 #Method to convert xy to imgxy cordinates
571 #
572 # $obj->addPoint_rad(50=>3.1415);
573 #
574 #=cut
575  
576 sub _imgxy_xy {
577 my $self = shift;
578 my $x = shift();
579 my $y = shift();
580 my $sz = $self->_width;
581 $x = $sz/2 + $x + $self->border;
582 $y = $sz/2 - $y + $self->border;
583 return ($x, $y);
584 }
585  
586 #=head2 _xy_rt_rad
587 #
588 #Method to convert polar cordinate to Cartesian cordinates.
589 #
590 #=cut
591  
592 sub _xy_rt_rad {
593 my $self = shift;
594 my $r = shift;
595 my $t = shift;
596 my $x = $r*cos($t);
597 my $y = $r*sin($t);
598 return ($x, $y);
599 }
600  
601 #=head2 _imgxy_rt_rad
602 #
603 #Method to convert polar cordinate to Cartesian cordinates.
604 #
605 #=cut
606  
607 sub _imgxy_rt_rad {
608 my $self = shift;
609 my $r = shift;
610 my $t = shift;
611 my ($x,$y) = $self->_xy_rt_rad($self->_scale($r), $t);
612 return $self->_imgxy_xy($x, $y);
613 }
614  
615 =head1 LICENSE
616  
617 MIT License
618  
619 Copyright (c) 2022 Michael R. Davis
620  
621 =head1 SEE ALSO
622  
623 L<GD>, L<Geo::Constants>, L<Geo::Functions>, L<Graphics::ColorNames>
624  
625 =cut
626  
627 1;
↑ Summary