manticore/Manticore/Geometry/Point.pm
2023-12-30 13:38:26 +01:00

49 lines
844 B
Perl

package Manticore::Geometry::Point;
use strict;
use warnings;
use Math::BigRat lib => 'GMP';
sub new {
my ($pkg, $x, $y) = @_;
return bless {
orig=>[$x,$y],
x=>Math::BigRat->new($x),
y=>Math::BigRat->new($y),
}
}
sub x {
my $self = shift;
return $self->{x}
}
sub y {
my $self = shift;
return $self->{y}
}
# parse a string and return as vector
sub fromText {
my ($pkg,$text)=@_;
if($text=~m#^([0-9\.]+),([0-9\.]+)$#) {
my ($x, $y) = ($1, $2);
return bless {
orig=>$text,
x=>Math::BigRat->new($x),
y=>Math::BigRat->new($y),
}
} else {
return { error=> "Parse error: Not a vector" }
}
}
# difference between two points; returns a vector
sub diff {
my ($self, $other) = @_;
return Manticore::Geometry::Vector->new($other->{x}-$self->{x}, $other->{y}-$self->{y});
}
1;