manticore/Manticore/Geometry/LineSegment.pm
2023-12-31 12:11:24 +01:00

29 lines
588 B
Perl

package Manticore::Geometry::LineSegment;
use strict;
use warnings;
use Manticore::Geometry::Point;
sub new {
my ($pkg, $from, $to) = @_;
return bless {from => $from, to => $to}
}
# relative position of a point to a line segment AB
# that is two values, pd, such that
# p is 0 at A, p is 1 at B, p<0 pre A, p>1 post B
# d is distance to the line
sub relative {
my ($self, $pt) = @_;
my $ab = $self->{to}->diff($self->{from});
my $abr = $ab->rightRotate();
my $ap = $pt->diff($self->{from});
return [
$ab->scalar($ap)/$ab->sizeSq(),
$abr->scalar($ap),
];
}
1;