File: Vector2.pm

package info (click to toggle)
algotutor 0.8.6-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 580 kB
  • sloc: perl: 2,563; makefile: 41; php: 24; sh: 1
file content (52 lines) | stat: -rw-r--r-- 1,014 bytes parent folder | download | duplicates (2)
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;