New +Line::intersection_infinite() method

This commit is contained in:
Alessandro Ranellucci 2015-01-16 16:25:39 +01:00
parent aa69ae11a8
commit e749f6040f
4 changed files with 33 additions and 1 deletions

View file

@ -89,6 +89,23 @@ Line::point_at(double distance) const
return p;
}
bool
Line::intersection_infinite(const Line &other, Point* point) const
{
Vector x = this->a.vector_to(other.a);
Vector d1 = this->vector();
Vector d2 = other.vector();
double cross = d1.x * d2.y - d1.y * d2.x;
if (std::fabs(cross) < EPSILON)
return false;
double t1 = (x.x * d2.y - x.y * d2.x)/cross;
point->x = this->a.x + d1.x * t1;
point->y = this->a.y + d1.y * t1;
return true;
}
bool
Line::coincides_with(const Line &line) const
{

View file

@ -29,6 +29,7 @@ class Line
Point* midpoint() const;
void point_at(double distance, Point* point) const;
Point point_at(double distance) const;
bool intersection_infinite(const Line &other, Point* point) const;
bool coincides_with(const Line &line) const;
double distance_to(const Point &point) const;
bool parallel_to(double angle) const;

View file

@ -4,7 +4,7 @@ use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 39;
use Test::More tests => 40;
use constant PI => 4 * atan2(1, 1);
use constant EPSILON => 1E-4;
@ -71,4 +71,11 @@ foreach my $base_angle (0, PI/4, PI/2, PI) {
}
}
{
my $a = Slic3r::Line->new([100, 0], [200, 0]);
my $b = Slic3r::Line->new([300, 300], [300, 100]);
my $r = $a->intersection_infinite($b);
is_deeply $r->pp, [300, 0], 'intersection_infinite';
}
__END__

View file

@ -30,6 +30,13 @@
%code{% RETVAL = THIS->parallel_to(*line); %};
Point* midpoint();
Clone<Point> point_at(double distance);
Clone<Point> intersection_infinite(Line* other)
%code{%
Point p;
bool res = THIS->intersection_infinite(*other, &p);
if (!res) CONFESS("Intersection failed");
RETVAL = p;
%};
Polyline* as_polyline()
%code{% RETVAL = new Polyline(*THIS); %};
Clone<Point> normal();