Math-Geometry-IntersectionArea
view release on metacpan or search on metacpan
lib/Math/Geometry/IntersectionArea.pm view on Meta::CPAN
# / \
# | x---|-x
#
# The two points where the segment intersects the circle are found
# solving the following cuadratic equation:
#
# norm(a + alfa * ab) = d
#
#
# The coeficientes c2, c1, c0 are deduced from the formula
# above such that:
#
# c2 * alfa**2 + 2 * c1 * alfa + c0 = 0
#
# And then the clasical formula for quadratic equation solved is
# used:
#
# alfa0 = 1/c2 + (-$c1 + sqrt($c1*$c1 - 4 * $c0 * $c2))
# alfa1 = 1/c2 + (-$c1 - sqrt($c1*$c1 - 4 * $c0 * $c2))
my $ab = $b - $a;
my $c2 = $ab->norm2 or return 0; # a and b are the same point
my $c1 = $a * $ab;
my $c0 = $a->norm2 - $r2;
my $discriminant = $c1 * $c1 - $c0 * $c2;
( run in 0.337 second using v1.01-cache-2.11-cpan-26ccb49234f )