Location : Home > Languages > Perl > Package
Title : Math::Intersection::StraightLine
Toolbox Logo

名称

 Math::Intersection::StraightLine - 2直線の交点を求める


概要

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

# 交点がある場合
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);

# ない場合
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);

説明

 本モジュールは所与の2直線の交点を(存在すれば)求める。交点がなければ 0 を返す。好転があればその交点の座標を返す。もし無限の交点を持つなら -1 を返す。Math::Intersection::StraightLine は以下の4タイプの入力を扱う。

functions

 直線はしばしば y = 9x + 3 のような1次関数で与えられる。

vectors

 直線をベクトルで表す。

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

points

 直線は1直線上の点上の2つのベクトルで記述できる。

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

point_limited

 2つの部分の交点が存在するならテストすべきである。

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

 以下の例は points と 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);

 最初の例は交点として 50/-90 を返すが、2番目の例は 0 を返す。これは $line_a_part は $line_a の一部であり、直線 b の部分としては交点がないからである。
 最初の例では直線はベクトルに変換されている。


使用例

$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');

# y = 9x + 5 と y = -3x - 2 でテスト
my $function_one = [9,5];
my $function_two = [-3,-2];
$result = $finder->functions($function_one,$function_two);

その他

 交点の座標は不正確であるかも知れない。

# y = 9x + 5 と y = -3x - 2 でテスト
my $function_one = [9,5];
my $function_two = [-3,-2];
$result = $finder->functions($function_one,$function_two);

は以下のような結果を返す。

$VAR1 = [
        '-0.583333333333333', # この値は不正確である。
        '-0.25'
        ];

著者

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


著作権とライセンス

 Copyright (C) 2005 by Renee Baecker

  本ライブラリはフリーソフトウェアであり、 Perl 本体と同等の条件で修正/再配布することができる。Perl は バージョン 5.8.5 またはユーザの選択でPerl 5 のそれ以降のバージョンで利用可能である。

Toolbox Logo
Updated : 2006/09/20