Maybe you want something like this:

#!/usr/bin/perl use strict; use warnings; my $match45 = isCorrectPath (0, 0, 10, 10); for my $pair ([0, 0], [-1, 0], [-4, 0], [5, 5], [1, 5]) { if ($match45->(@$pair)) { print "Match 45 for @$pair\n"; } else { print "Fail 45 for @$pair\n"; } } my $match0 = isCorrectPath (0, 0, 10, 0); for my $pair ([0, 0], [0, -1], [0, -4], [5, 5], [100, 2]) { if ($match0->(@$pair)) { print "Match 0 for @$pair\n"; } else { print "Fail 0 for @$pair\n"; } } my $match90 = isCorrectPath (0, 0, 0, 10); for my $pair ([0, 0], [-1, -0], [-4, 0], [5, 5], [2, 100]) { if ($match90->(@$pair)) { print "Match 90 for @$pair\n"; } else { print "Fail 90 for @$pair\n"; } } sub isCorrectPath { my ($x1, $y1, $x2, $y2) = @_; my $difx = ($x2 - $x1); my $slope = $difx ? ($y2 - $y1) / $difx : undef; return sub { my ($x, $y) = @_; my $xmin = ($x - $x1); return abs($x - $x1) < 3 # Vertical line: allow 3 units eit +her side if !defined $slope; return abs($y - $y1) < 3 # Horizontal line: allow 3 units e +ither side if !$slope; return abs(($slope * ($x - $x1)) - ($y - $y1)) < 3; }; }

Prints:

Match 45 for 0 0 Match 45 for -1 0 Fail 45 for -4 0 Match 45 for 5 5 Fail 45 for 1 5 Match 0 for 0 0 Match 0 for 0 -1 Fail 0 for 0 -4 Fail 0 for 5 5 Match 0 for 100 2 Match 90 for 0 0 Match 90 for -1 0 Fail 90 for -4 0 Fail 90 for 5 5 Match 90 for 2 100
True laziness is hard work

In reply to Re: Check if line is straight by GrandFather
in thread Check if line is straight by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.