| 1 | package Compass::Bearing; | ||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use base qw{Package::New}; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use Geo::Functions qw{deg_rad round}; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | our $VERSION = '0.08'; | |||||||||||
| 8 | |||||||||||||
| 9 | =head1 NAME | ||||||||||||
| 10 | |||||||||||||
| 11 | Compass::Bearing - Convert angle to text bearing (aka heading) | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||
| 14 | |||||||||||||
| 15 | use Compass::Bearing; | ||||||||||||
| 16 | my $cb = Compass::Bearing->new(3); | ||||||||||||
| 17 | my $angle = 12; | ||||||||||||
| 18 | printf "Bearing: %s deg => %s\n", $angle, $cb->bearing($angle); #prints NNE | ||||||||||||
| 19 | |||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||
| 21 | |||||||||||||
| 22 | Convert angle to text bearing (aka heading) | ||||||||||||
| 23 | |||||||||||||
| 24 | =head1 CONSTRUCTOR | ||||||||||||
| 25 | |||||||||||||
| 26 | =head2 new | ||||||||||||
| 27 | |||||||||||||
| 28 | The new() constructor may be called with any parameter that is appropriate to the set method. | ||||||||||||
| 29 | |||||||||||||
| 30 | my $obj = Compass::Bearing->new(); | ||||||||||||
| 31 | |||||||||||||
| 32 | =head1 METHODS | ||||||||||||
| 33 | |||||||||||||
| 34 | =cut | ||||||||||||
| 35 | |||||||||||||
| 36 | 2 | ▶ | sub initialize { | ||||||||||
|
Subroutine
initialize: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 37 | 2 | my $self = shift; | |||||||||||
| 38 | 2 | ▶ | my $param = shift || 3; | ||||||||||
|
Condition50%1/2
shift() || 3
MC/DC0%0/1
shift() || 3
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 39 | 2 | $self->set($param); | |||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | =head2 bearing | ||||||||||||
| 43 | |||||||||||||
| 44 | Method returns a text string based on bearing | ||||||||||||
| 45 | |||||||||||||
| 46 | my $bearing=$obj->bearing($degrees_from_north); | ||||||||||||
| 47 | |||||||||||||
| 48 | =cut | ||||||||||||
| 49 | |||||||||||||
| 50 | 171 | ▶ | sub bearing { | ||||||||||
|
Subroutine
bearing: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 51 | 171 | my $self = shift; | |||||||||||
| 52 | 171 | ▶ | my $angle = shift || 0; #degrees | ||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 53 | 171 | ▶ | $angle += 360 while ($angle < 0); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 171 | my @data = $self->data; | |||||||||||
| 55 | 171 | return $data[round($angle/360 * @data) % @data]; | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | =head2 bearing_rad | ||||||||||||
| 59 | |||||||||||||
| 60 | Method returns a text string based on bearing | ||||||||||||
| 61 | |||||||||||||
| 62 | my $bearing=$obj->bearing_rad($radians_from_north); | ||||||||||||
| 63 | |||||||||||||
| 64 | =cut | ||||||||||||
| 65 | |||||||||||||
| 66 | 15 | ▶ | sub bearing_rad { | ||||||||||
|
Subroutine
bearing_rad: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 67 | 15 | my $self = shift; | |||||||||||
| 68 | 15 | ▶ | my $angle = deg_rad(shift()||0); #degrees | ||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 69 | 15 | return $self->bearing($angle); | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | =head2 set | ||||||||||||
| 73 | |||||||||||||
| 74 | Method sets and returns key for the bearing text data structure. | ||||||||||||
| 75 | |||||||||||||
| 76 | my $key = $self->set; | ||||||||||||
| 77 | my $key = $self->set(1); | ||||||||||||
| 78 | my $key = $self->set(2); | ||||||||||||
| 79 | my $key = $self->set(3); #default value | ||||||||||||
| 80 | |||||||||||||
| 81 | =cut | ||||||||||||
| 82 | |||||||||||||
| 83 | 177 | ▶ | sub set { | ||||||||||
|
Subroutine
set: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||
| 84 | 177 | my $self = shift; | |||||||||||
| 85 | 177 | my $param = shift; | |||||||||||
| 86 | 177 | ▶ | if (defined $param) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 5 | my %data = $self->_dataraw; | |||||||||||
| 88 | 5 | my @keys = sort keys %data; | |||||||||||
| 89 | 5 | ▶ | if (exists $data{$param}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 5 | $self->{'set'} = $param; | |||||||||||
| 91 | } else { | ||||||||||||
| 92 | 0 | die(qq{Error: "$param" is not a valid parameter to the set method. Try }. join(", ", map {qq{"$_"}} @keys). ".\n") | |||||||||||
| 93 | } | ||||||||||||
| 94 | } | ||||||||||||
| 95 | 177 | return $self->{'set'}; | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | =head2 data | ||||||||||||
| 99 | |||||||||||||
| 100 | Method returns an array of text values. | ||||||||||||
| 101 | |||||||||||||
| 102 | my $data=$self->data; | ||||||||||||
| 103 | |||||||||||||
| 104 | =cut | ||||||||||||
| 105 | |||||||||||||
| 106 | 171 | ▶ | sub data { | ||||||||||
|
Subroutine
data: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 107 | 171 | my $self = shift; | |||||||||||
| 108 | 171 | my $data = $self->_dataraw; | |||||||||||
| 109 | 171 | my $return = $data->{$self->set}; | |||||||||||
| 110 | 171 | ▶ | return wantarray ? @{$return} : $return; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | 176 | ▶ | sub _dataraw { | ||||||||||
|
Subroutine
_dataraw: called CC=2 SCAR=6.9
| |||||||||||||
| 114 | 176 | my %data=( | |||||||||||
| 115 | 1 => [qw{N E S W}], | ||||||||||||
| 116 | 2 => [qw{N NE E SE S SW W NW}], | ||||||||||||
| 117 | 3 => [qw{N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW}] | ||||||||||||
| 118 | ); | ||||||||||||
| 119 | 176 | ▶ | return wantarray ? %data : \%data; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | =head1 BUGS | ||||||||||||
| 123 | |||||||||||||
| 124 | Please log on GitHub | ||||||||||||
| 125 | |||||||||||||
| 126 | =head1 AUTHOR | ||||||||||||
| 127 | |||||||||||||
| 128 | Michael R. Davis | ||||||||||||
| 129 | |||||||||||||
| 130 | =head1 LICENSE | ||||||||||||
| 131 | |||||||||||||
| 132 | MIT License | ||||||||||||
| 133 | |||||||||||||
| 134 | Copyright (c) 2022 Michael R. Davis | ||||||||||||
| 135 | |||||||||||||
| 136 | =head1 SEE ALSO | ||||||||||||
| 137 | |||||||||||||
| 138 | L<Ham::Resources::Utils> compass method | ||||||||||||
| 139 | |||||||||||||
| 140 | =cut | ||||||||||||
| 141 | |||||||||||||
| 142 | 2 | 1; | |||||||||||