Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Check if line is straight

by pajout (Curate)
on May 16, 2011 at 06:36 UTC ( #905021=note: print w/ replies, xml ) Need Help??


in reply to Check if line is straight

If you really want to check colinearity of three points, you can just multiply vectors:

sub isCorrectPath () { my ($x1, $y1, $x2, $y2, $x, $y) = @_; return !(($x2-$x1)*($y-$y1)-($x-$x1)*($y2-$y1)); }


Comment on Re: Check if line is straight
Download Code
Re^2: Check if line is straight
by Anonymous Monk on May 16, 2011 at 20:14 UTC
    This is pretty neat, saves a lot of code. What is the best way to add tolerance to this? any suggestions ? Thanks
      return abs(($x2-$x1)*($y-$y1)-($x-$x1)*($y2-$y1)) < $tolerance;

      Update added abs()

      It depends if you mean tolerance necessary for dealing floats - lidden suggested it already. But if you mean real tolerance, for instance "path is correct if the angle between two vectors is less than 1 degree", you should really compute that value.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://905021]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2015-07-07 01:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls