in reply to Re: Check if line is straight
in thread Check if line is straight
UPDATE: Nota bene: only additions no divisions, which makes the code fast and secure from division by 0 problems.
($p1,$p2,$p3)=([2,2],[3,4],[4,6]); sub vector { my ($x1,$y1) = @{$_[0]}; my ($x2,$y2) = @{$_[1]}; my @v=( $x2-$x1, $y2-$y1 ); my @o=( -$v[1] , $v[0] ); return [@v],[@o]; } sub dot { my ($x1,$y1) = @{$_[0]}; my ($x2,$y2) = @{$_[1]}; return $x1*$x2 + $y1*$y2; } my ($v12,$o12)= vector($p1,$p2); # the test my ($v)= vector($p1,$p3); print dot ($o12,$v); # 0 because p3 is collinear with p1 ,p2
Cheers Rolf
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Check if line is straight
by Anonymous Monk on May 16, 2011 at 19:53 UTC |