1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
# Author: Chao-Kuei Hung
# For more info, including license, please see doc/index.html
package Vector2;
# 2-d Vector
use strict;
use Carp;
use vars qw(@ISA);
@ISA = qw(Vector);
use Vector;
#sub slope {
# my ($self) = @_;
# return $self->[1]/$self->[0];
#}
sub signed_area {
my ($a, $b) = @_;
return $a->x*$b->y - $a->y*$b->x;
}
sub signed_turn {
my ($a, $b) = @_;
return atan2($a->signed_area($b), $a->dot($b));
}
#sub makebasis {
# my ($self) = @_;
# my ($l) = $self->norm();
# my ($c, $s) = ($self->[0]/$l, $self->[1]/$l);
# return [Vector->new($c,-$s),Vector->new($s,$c)];
#}
if ($0 =~ /Vector2.pm$/) {
# being tested as a stand-alone program, so run test code.
my ($p, $q);
$p = Vector2->new(4,-3);
$q = Vector2->new(5,12);
print $p+$q, ",", $p-$q, "\n";
# ans: -0.6435
print $p->signed_area($q), "\n";
# ans: 63
$p = Vector2->new(-sqrt(3),1);
$q = Vector2->new(-1,-1);
print $p->signed_turn($q), "\n";
# ans: 1.309
}
1;
|