Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Check if line is straight

by LanX (Saint)
on May 16, 2011 at 01:26 UTC ( [id://905008]=note: print w/replies, xml ) Need Help??


in reply to Re: Check if line is straight
in thread Check if line is straight

proof of concept second approach, normalization of orthogonal not necessary for dot product to become 0.

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
    Sorry for not being clear, I assumed that looking at the code will make it obvious which is clearly not the case, The problem i am trying to solve is that it is basically a join the dots application, There are fixed points say point 1 and point 2, when the mouse is moved from point 1 to point 2 i want to make sure the path is correct so I take the start x,y (point1) and end x, y(point2) and take the current position of the mouse (x,y) and make sure that the path is correct. my code works fine if there is a slope but if the points are vertical or horizontal (slope 0) then I was not sure how to make sure that the direction and path is correct( a slight deviation is ok). Hope this clears up the question

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-16 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found