in reply to line segment intersection

I found the necessary module on CPAN. It's called Imager::Graph::Util. Here's an example script:

#!/usr/bin/perl -w use strict; use Imager::Graph::Util; my ($ax, $ay, $bx, $by, $cx, $cy, $dx, $dy) = GetLines(); # you have t +o write that subroutine my @line1 = line_from_points($ax, $ay, $bx, $by); my @line2 = line_from_points($cx, $cy, $dx, $dy); # @p will contain the (x, y) point of intersection if it exists my @p = intersect_lines(@line1, @line2);

Replies are listed 'Best First'.
Re: Re: line segment intersection
by tall_man (Parson) on Apr 30, 2003 at 17:27 UTC
    I took a look at Imager::Graph::Util and the math does not look so good. It doesn't use parametric equations like tye has, so it will be more prone to overflow and roundoff errors. Also, it solves line intersections only, not segments.

    An alternative to simplify this type of equation is to use Math::VectorReal. There's a neat formula with vector cross products to solve this problem. It tests for being outside the segment as early as it can.