Respectable Monks,
Given 2 points for co-ordinates x and y , I need a function to check if a line is straight ( with some variance), I have the following code which works fine if there is a slope in the line, but if the slope is 0 then I am not sure how to check if the new points are a straight line, here is the code I have , What am I missing here
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;
}
} ;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.