sub isCorrectPath () { my ($x1, $y1, $x2, $y2) = @_ my $difx = ($x2 - $x1) ; my $slope = ($difx == 0) ? 0 : ($y2 - $y1) / $difx; return sub { my ($x, $y) = @_; my $xmin = ($x - $x1); if ( $slope == 0 ) {# if line is straight if ($difx == 0 ) {# how to make sure the path is correct and in correct direction return (($x - $x1) < 3 ) && ((($y - $y1) > 0) || (($y - $y1) > 0)); } else { return (($y - $y1) < 3 ) && (($x - $x1) > 0) || (($x1 - $x) > 0) ; } } else { return abs(($slope * ($x - $x1)) - ($y - $y1)) <= 30; } } ; }