The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Math::Intersection::StraightLine - Calculate intersection point for two lines

SYNOPSIS

  use Math::Intersection::StraightLine;
  use Data::Dumper;
  my $finder = Math::Intersection::StraightLine->new();

  # one intersection point
  my $vector_a = [[20,60],[-40,0]];
  my $vector_b = [[50,80],[0,50]];
  my $result = $finder->vectors($vector_a,$vector_b);
  print Dumper($result);

  # no intersection point
  my $point_a = [[20,60],[30,10]];
  my $point_b = [[50,80],[50,75]];
  $result = $finder->point_limited($point_a,$point_b);
  print Dumper($result);

DESCRIPTION

This module calculates the intersection point of two straight lines (if one exists). It returns 0, if no intersection point exists. If the lines have an intersection point, the coordinates of the point are the returnvalue. If the given lines have infinite intersection points, -1 is returned. Math::Intersection::StraightLine can handle four types of input:

functions

Often straight lines are given in functions of that sort: y = 9x + 3

vectors

the vector assignment of the line

  (10)     +     lambda(30)
  (20)                 (50)
  

points

The straight lines are described with two vectors to points on the line

  X1 = (10)             X2 = (40)
       (20)                  (70)
       

point_limited

If the module should test, if an intersection point of two parts exists

  X1 = (10)             X2 = (40)
       (20)                  (70)
       

The following example should clarify the difference between points and point_limited:

  $line_a = [[20,60],[30,10]];
  $line_b = [[50,80],[50,75]];
  $result = $finder->points($line_a,$line_b);

  $line_a_part = [[20,60],[30,10]];
  $line_b_part = [[50,80],[50,75]];
  $result = $finder->point_limited($line_a_part,$line_b_part);
  

The first example returns the intersection point 50/-90, the second returns 0 because $line_a_part is just a part of $line_a and has no intersection point with the part of line b.

In the first example, the lines are changed to the vectors of the lines.

EXAMPLES

  $vector_a = [[20,60],[30,10]];
  $vector_b = [[50,80],[60,30]];
  $result = $finder->point_limited($vector_a,$vector_b);
  ok($result == 0,'parallel lines(diagonal)');

  $vector_a = [[20,60],[20,10]];
  $vector_b = [[60,80],[20,10]];
  $result = $finder->vectors($vector_a,$vector_b);
  ok($result == -1,'overlapping vectors');

  $vector_a = [[20,60],[30,10]];
  $vector_b = [[50,80],[50,75]];
  $result = $finder->points($vector_a,$vector_b);
  ok($result->[0] == 50 && $result->[1] == -90,'Lines with one intersection point');

  # test y=9x+5 and y=-3x-2
  my $function_one = [9,5];
  my $function_two = [-3,-2];
  $result = $finder->functions($function_one,$function_two);

MISC

Note! The coordinates for the intersection point can be imprecise!

  # test y=9x+5 and y=-3x-2
  my $function_one = [9,5];
  my $function_two = [-3,-2];
  $result = $finder->functions($function_one,$function_two);
  

returns

  $VAR1 = [
          '-0.583333333333333', # this is imprecise
          '-0.25'
          ];

OTHER METHODS

new

returns a new object of Math::Intersection::StraightLine

AUTHOR

Renee Baecker, <module@renee-baecker.de>

COPYRIGHT AND LICENSE

Copyright (C) 2005 by Renee Baecker

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.