64 lines
1.2 KiB
Perl
64 lines
1.2 KiB
Perl
package Manticore::Geometry::Vector;
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
sub new {
|
|
my ($pkg, $x, $y) = @_;
|
|
return bless {
|
|
x=>Math::BigRat->new($x),
|
|
y=>Math::BigRat->new($y),
|
|
orig => [$x, $y],
|
|
}
|
|
}
|
|
|
|
sub asFloatVector {
|
|
my $self = shift;
|
|
return $self unless ref $self->{x};
|
|
return bless {
|
|
x=>$self->{x}->numify(),
|
|
y=>$self->{y}->numify(),
|
|
orig=>$self->{orig},
|
|
}
|
|
}
|
|
|
|
sub angle {
|
|
my $self = shift;
|
|
$self = $self->asFloatVector();
|
|
return atan2($self->{y}, $self->{x})
|
|
}
|
|
|
|
# add point to a vector
|
|
sub add {
|
|
my ($self, $other) = @_;
|
|
return Manticore::Geometry::Point->new($other->{x}+$self->{x}, $other->{y}+$self->{y});
|
|
}
|
|
|
|
# returns the square of the size of a vector
|
|
sub sizeSq {
|
|
my $self = shift;
|
|
return ($self->{x}*$self->{x}+$self->{y}*$self->{y})
|
|
}
|
|
# returns the size of a vector
|
|
sub size {
|
|
my $self = shift;
|
|
return sqrt($self->{x}*$self->{x}+$self->{y}*$self->{y})
|
|
}
|
|
|
|
# scalarproduct with another vector
|
|
sub scalar {
|
|
my ($self, $other) = @_;
|
|
return $self->{x}*$other->{x} + $self->{y}*$other->{y};
|
|
}
|
|
|
|
# new vector, rotated by 90°
|
|
sub rightRotate {
|
|
my $self = shift;
|
|
return bless {
|
|
x => -$self->{y},
|
|
y => $self->{x},
|
|
}
|
|
}
|
|
|
|
1;
|