| 1 | # | ||||||||||
| 2 | # This file is part of Games-RailRoad | ||||||||||
| 3 | # | ||||||||||
| 4 | # This software is copyright (c) 2008 by Jerome Quelin. | ||||||||||
| 5 | # | ||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||
| 8 | # | ||||||||||
| 9 | 18 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 18 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 18 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | package Games::RailRoad::Vector; | ||||||||||
| 14 | 18 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 18 | $Games::RailRoad::Vector::VERSION = '1.101330'; | |||||||||
| 16 | } | ||||||||||
| 17 | # ABSTRACT: an opaque vector class. | ||||||||||
| 18 | |||||||||||
| 19 | 18 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 18 | ▶ | use MooseX::Has::Sugar; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 18 | ▶ | use MooseX::SemiAffordanceAccessor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 18 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | use overload | ||||||||||
| 25 | 18 | ▶ | '=' => \©, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | '+' => \&_add, | ||||||||||
| 27 | '-' => \&_substract, | ||||||||||
| 28 | 'neg' => \&_invert, | ||||||||||
| 29 | '+=' => \&_add_inplace, | ||||||||||
| 30 | '-=' => \&_substract_inplace, | ||||||||||
| 31 | '<=>' => \&_compare, | ||||||||||
| 32 | 18 | '""' => \&as_string; | |||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | # -- attributes | ||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | 18 | has posx => ( rw, isa=>'Int', default=>0 ); | |||||||||
| 40 | 18 | has posy => ( rw, isa=>'Int', default=>0 ); | |||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | # -- private vars | ||||||||||
| 44 | |||||||||||
| 45 | 18 | Readonly my %coords2dir => ( | |||||||||
| 46 | '(-1,-1)' => 'nw', | ||||||||||
| 47 | '(-1,0)' => 'w', | ||||||||||
| 48 | '(-1,1)' => 'sw', | ||||||||||
| 49 | '(0,-1)' => 'n', | ||||||||||
| 50 | '(0,1)' => 's', | ||||||||||
| 51 | '(1,-1)' => 'ne', | ||||||||||
| 52 | '(1,0)' => 'e', | ||||||||||
| 53 | '(1,1)' => 'se', | ||||||||||
| 54 | ); | ||||||||||
| 55 | 18 | Readonly my %dir2coords => ( | |||||||||
| 56 | 'nw' => [-1,-1], | ||||||||||
| 57 | 'w' => [-1, 0], | ||||||||||
| 58 | 'sw' => [-1, 1], | ||||||||||
| 59 | 'n' => [ 0,-1], | ||||||||||
| 60 | 's' => [ 0, 1], | ||||||||||
| 61 | 'ne' => [ 1,-1], | ||||||||||
| 62 | 'e' => [ 1, 0], | ||||||||||
| 63 | 'se' => [ 1, 1], | ||||||||||
| 64 | ); | ||||||||||
| 65 | |||||||||||
| 66 | |||||||||||
| 67 | # -- constructors & initializers | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | # provided by moose | ||||||||||
| 71 | |||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | 0 | ▶ | sub new_dir { | ||||||||
|
Subroutine
new_dir: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 75 | 0 | my ($pkg, $dir) = @_; | |||||||||
| 76 | 0 | my ($x, $y) = @{ $dir2coords{$dir} }; | |||||||||
| 77 | 0 | return $pkg->new( { posx=>$x, posy=>$y } ); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | |||||||||||
| 81 | |||||||||||
| 82 | 0 | ▶ | sub copy { | ||||||||
|
Subroutine
copy: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 83 | 0 | my $vec = shift; | |||||||||
| 84 | 0 | return bless {%$vec}, ref $vec; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | # -- PUBLIC METHODS | ||||||||||
| 89 | |||||||||||
| 90 | #- accessors | ||||||||||
| 91 | |||||||||||
| 92 | # | ||||||||||
| 93 | # my $str = $vec->as_string; | ||||||||||
| 94 | # my $str = "$vec"; | ||||||||||
| 95 | # | ||||||||||
| 96 | # Return the stringified form of $vec. For instance, a vector might look | ||||||||||
| 97 | # like "(1,2)". | ||||||||||
| 98 | # | ||||||||||
| 99 | 0 | ▶ | sub as_string { | ||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 100 | 0 | my $self = shift; | |||||||||
| 101 | 0 | return '(' . $self->posx . ',' . $self->posy . ')'; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | # | ||||||||||
| 106 | # my $str = $vec->as_dir; | ||||||||||
| 107 | # | ||||||||||
| 108 | # Return the cardinal direction (n, sw, etc.) of $vec if it's a unit | ||||||||||
| 109 | # vector (ok, (1,1) is not a unit vector but you see my point). | ||||||||||
| 110 | # | ||||||||||
| 111 | 0 | ▶ | sub as_dir { | ||||||||
|
Subroutine
as_dir: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 112 | 0 | my $self = shift; | |||||||||
| 113 | 0 | return $coords2dir{"$self"}; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | |||||||||||
| 117 | # -- PRIVATE METHODS | ||||||||||
| 118 | |||||||||||
| 119 | #- math ops | ||||||||||
| 120 | |||||||||||
| 121 | # | ||||||||||
| 122 | # my $vec = $v1->_add($v2); | ||||||||||
| 123 | # my $vec = $v1 + $v2; | ||||||||||
| 124 | # | ||||||||||
| 125 | # Return a new GRV object, which is the result of $v1 plus $v2. | ||||||||||
| 126 | # | ||||||||||
| 127 | 0 | ▶ | sub _add { | ||||||||
|
Subroutine
_add: not called CC=1 SCAR=6.9
| |||||||||||
| 128 | 0 | my ($v1, $v2) = @_; | |||||||||
| 129 | 0 | my $rv = ref($v1)->new; | |||||||||
| 130 | 0 | $rv->set_posx( $v1->posx + $v2->posx ); | |||||||||
| 131 | 0 | $rv->set_posy( $v1->posy + $v2->posy ); | |||||||||
| 132 | 0 | return $rv; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | |||||||||||
| 136 | # | ||||||||||
| 137 | # my $vec = $v1->_substract($v2); | ||||||||||
| 138 | # my $vec = $v1 - $v2; | ||||||||||
| 139 | # | ||||||||||
| 140 | # Return a new GRV object, which is the result of $v1 minus $v2. | ||||||||||
| 141 | # | ||||||||||
| 142 | 0 | ▶ | sub _substract { | ||||||||
|
Subroutine
_substract: not called CC=1 SCAR=6.9
| |||||||||||
| 143 | 0 | my ($v1, $v2) = @_; | |||||||||
| 144 | 0 | my $rv = ref($v1)->new; | |||||||||
| 145 | 0 | $rv->set_posx( $v1->posx - $v2->posx ); | |||||||||
| 146 | 0 | $rv->set_posy( $v1->posy - $v2->posy ); | |||||||||
| 147 | 0 | return $rv; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | |||||||||||
| 151 | |||||||||||
| 152 | # | ||||||||||
| 153 | # my $v2 = $v1->_invert; | ||||||||||
| 154 | # my $v2 = -$v1; | ||||||||||
| 155 | # | ||||||||||
| 156 | # Subtract $v1 from the origin. Effectively, gives the inverse of the | ||||||||||
| 157 | # original vector. The new vector is the same distance from the origin, | ||||||||||
| 158 | # in the opposite direction. | ||||||||||
| 159 | # | ||||||||||
| 160 | 0 | ▶ | sub _invert { | ||||||||
|
Subroutine
_invert: not called CC=1 SCAR=6.9
| |||||||||||
| 161 | 0 | my ($v1) = @_; | |||||||||
| 162 | 0 | my $rv = ref($v1)->new; | |||||||||
| 163 | 0 | $rv->set_posx( - $v1->posx ); | |||||||||
| 164 | 0 | $rv->set_posy( - $v1->posy ); | |||||||||
| 165 | 0 | return $rv; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | |||||||||||
| 169 | #- inplace math ops | ||||||||||
| 170 | |||||||||||
| 171 | # | ||||||||||
| 172 | # $v1->_add_inplace($v2); | ||||||||||
| 173 | # $v1 += $v2; | ||||||||||
| 174 | # | ||||||||||
| 175 | # | ||||||||||
| 176 | 0 | ▶ | sub _add_inplace { | ||||||||
|
Subroutine
_add_inplace: not called CC=1 SCAR=6.9
| |||||||||||
| 177 | 0 | my ($v1, $v2) = @_; | |||||||||
| 178 | 0 | $v1->set_posx( $v1->posx + $v2->posx ); | |||||||||
| 179 | 0 | $v1->set_posy( $v1->posy + $v2->posy ); | |||||||||
| 180 | 0 | return $v1; | |||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | |||||||||||
| 184 | # | ||||||||||
| 185 | # $v1->_substract_inplace($v2); | ||||||||||
| 186 | # $v1 -= $v2; | ||||||||||
| 187 | # | ||||||||||
| 188 | # Substract $v2 to $v1, and stores the result back into $v1. | ||||||||||
| 189 | # | ||||||||||
| 190 | 0 | ▶ | sub _substract_inplace { | ||||||||
|
Subroutine
_substract_inplace: not called CC=1 SCAR=6.9
| |||||||||||
| 191 | 0 | my ($v1, $v2) = @_; | |||||||||
| 192 | 0 | $v1->set_posx( $v1->posx - $v2->posx ); | |||||||||
| 193 | 0 | $v1->set_posy( $v1->posy - $v2->posy ); | |||||||||
| 194 | 0 | return $v1; | |||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | |||||||||||
| 198 | #- comparison | ||||||||||
| 199 | |||||||||||
| 200 | # | ||||||||||
| 201 | # my $bool = $v1->_compare($v2); | ||||||||||
| 202 | # my $bool = $v1 <=> $v2; | ||||||||||
| 203 | # | ||||||||||
| 204 | # Check whether the vectors both point at the same spot. Return 0 if they | ||||||||||
| 205 | # do, 1 if they don't. | ||||||||||
| 206 | # | ||||||||||
| 207 | 0 | ▶ | sub _compare { | ||||||||
|
Subroutine
_compare: not called CC=3 SCAR=24.8
| |||||||||||
| 208 | 0 | my ($v1, $v2) = @_; | |||||||||
| 209 | 0 | ▶ | return 1 if $v1->posx != $v2->posx; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 210 | 0 | ▶ | return 1 if $v1->posy != $v2->posy; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 211 | 0 | return 0; | |||||||||
| 212 | } | ||||||||||
| 213 | |||||||||||
| 214 | |||||||||||
| 215 | 18 | 1; | |||||||||
| 216 | |||||||||||
| 217 | |||||||||||
| 218 | =pod | ||||||||||
| 219 | |||||||||||
| 220 | =head1 NAME | ||||||||||
| 221 | |||||||||||
| 222 | Games::RailRoad::Vector - an opaque vector class. | ||||||||||
| 223 | |||||||||||
| 224 | =head1 VERSION | ||||||||||
| 225 | |||||||||||
| 226 | version 1.101330 | ||||||||||
| 227 | |||||||||||
| 228 | =head1 SYNOPSIS | ||||||||||
| 229 | |||||||||||
| 230 | my $v1 = Games::RailRoad::Vector->new( \%params ); | ||||||||||
| 231 | |||||||||||
| 232 | =head1 DESCRIPTION | ||||||||||
| 233 | |||||||||||
| 234 | This class abstracts basic vector manipulation. It lets you pass around | ||||||||||
| 235 | one argument to your functions, do vector arithmetic and various string | ||||||||||
| 236 | representation. | ||||||||||
| 237 | |||||||||||
| 238 | =head1 ATTRIBUTES | ||||||||||
| 239 | |||||||||||
| 240 | =head2 posx | ||||||||||
| 241 | |||||||||||
| 242 | The x coordinate of the vector. Default to 0. | ||||||||||
| 243 | |||||||||||
| 244 | =head2 posy | ||||||||||
| 245 | |||||||||||
| 246 | The y coordinate of the vector. Default to 0. | ||||||||||
| 247 | |||||||||||
| 248 | =head1 METHODS | ||||||||||
| 249 | |||||||||||
| 250 | =head2 my $vec = GR::Vector->new( \%params ); | ||||||||||
| 251 | |||||||||||
| 252 | Create and return a new vector. Accept a hash reference with the | ||||||||||
| 253 | attribute values. | ||||||||||
| 254 | |||||||||||
| 255 | =head2 my $vec = GR::Vector->new_dir( $dir ); | ||||||||||
| 256 | |||||||||||
| 257 | Create a new vector, from a given direction. The recognized directions | ||||||||||
| 258 | are C<e>, C<n>, C<ne>, C<nw>, C<s>, C<se>, C<sw>, C<w>. | ||||||||||
| 259 | |||||||||||
| 260 | =head2 my $vec = $v->copy; | ||||||||||
| 261 | |||||||||||
| 262 | Return a new GRV object, which has the same coordinates as C<$v>. | ||||||||||
| 263 | |||||||||||
| 264 | =head1 PUBLIC METHODS | ||||||||||
| 265 | |||||||||||
| 266 | =head2 my $str = $vec->as_string; | ||||||||||
| 267 | |||||||||||
| 268 | Return the stringified form of C<$vec>. For instance, a Befunge vector | ||||||||||
| 269 | might look like C<(1,2)>. | ||||||||||
| 270 | |||||||||||
| 271 | This method is also applied to stringification, ie when one forces | ||||||||||
| 272 | string context (C<"$vec">). | ||||||||||
| 273 | |||||||||||
| 274 | =head2 my $str = $vec->as_dir; | ||||||||||
| 275 | |||||||||||
| 276 | Return the cardinal direction (n, sw, etc.) of $vec if it's a unit | ||||||||||
| 277 | vector (ok, (1,1) is not a unit vector but you see my point). | ||||||||||
| 278 | |||||||||||
| 279 | =head1 MATHEMATICAL OPERATIONS | ||||||||||
| 280 | |||||||||||
| 281 | =head2 Standard operations | ||||||||||
| 282 | |||||||||||
| 283 | One can do some maths on the vectors. Addition and substraction work as | ||||||||||
| 284 | expected: | ||||||||||
| 285 | |||||||||||
| 286 | my $v = $v1 + $v2; | ||||||||||
| 287 | my $v = $v1 - $v2; | ||||||||||
| 288 | |||||||||||
| 289 | Either operation return a new GRV object, which is the result of C<$v1> | ||||||||||
| 290 | plus / minus C<$v2>. | ||||||||||
| 291 | |||||||||||
| 292 | The inversion is also supported: | ||||||||||
| 293 | my $v2 = -$v1; | ||||||||||
| 294 | |||||||||||
| 295 | will subtracts C<$v1> from the origin, and effectively, gives the | ||||||||||
| 296 | inverse of the original vector. The new vector is the same distance from | ||||||||||
| 297 | the origin, in the opposite direction. | ||||||||||
| 298 | |||||||||||
| 299 | =head2 Inplace operations | ||||||||||
| 300 | |||||||||||
| 301 | GRV objects also supports inplace mathematical operations: | ||||||||||
| 302 | |||||||||||
| 303 | $v1 += $v2; | ||||||||||
| 304 | $v1 -= $v2; | ||||||||||
| 305 | |||||||||||
| 306 | effectively adds / substracts C<$v2> to / from C<$v1>, and stores the | ||||||||||
| 307 | result back into C<$v1>. | ||||||||||
| 308 | |||||||||||
| 309 | =head2 Comparison | ||||||||||
| 310 | |||||||||||
| 311 | Finally, GRV objects can be tested for equality, ie whether two vectors | ||||||||||
| 312 | both point at the same spot. | ||||||||||
| 313 | |||||||||||
| 314 | print "same" if $v1 == $v2; | ||||||||||
| 315 | print "differ" if $v1 != $v2; | ||||||||||
| 316 | |||||||||||
| 317 | =head1 AUTHOR | ||||||||||
| 318 | |||||||||||
| 319 | Jerome Quelin | ||||||||||
| 320 | |||||||||||
| 321 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 322 | |||||||||||
| 323 | This software is copyright (c) 2008 by Jerome Quelin. | ||||||||||
| 324 | |||||||||||
| 325 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 326 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 327 | |||||||||||
| 328 | =cut | ||||||||||
| 329 | |||||||||||
| 330 | |||||||||||
| 331 | __END__ | ||||||||||