Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

perl module or code for calculating the shortest distance between a line and a point (vector linear algebra)

by fraizerangus (Sexton)
on Dec 30, 2009 at 10:05 UTC ( [id://814899]=perlquestion: print w/replies, xml ) Need Help??

fraizerangus has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everyone I know this is a slightly more mathematical based question but I thought it was worth just asking here just in case, I'm trying to calculate the shortest distance of every atom in a PDB file to an axis in the protein, I'm using the following algorithm but it does'nt appear to work? is there something I'm doing wrong or has anyone come across this problem and got code for it?
$A = x(point) - x(line point 1); $B = y(point) - y(line point 1); $C = x(line point 2) - x(line point 1); $D = y(line point 2) - y(line point 1); $dot = (($A * $C) + ($B * $D)); $len_sq = ((C * C) + (D * D)); $param = (dot / len_sq); if($param < 0) { $xx = x(line point 1); $yy = y(line point 2); } else if($param > 1) { $xx = $x(line point 2); $yy = $y(line point 2); } else { $xx = x(line point 1) + $param * $C; $yy = y(line point 1) + $param * $D; } $dist = sqrt((($x(point) - $xx) * ($x(point) - $xx)) + (($y(point) - $ +yy) * ($y(point) - $yy)) + (($z(point) - $zz) * ($z(point) - $zz)));
  • Comment on perl module or code for calculating the shortest distance between a line and a point (vector linear algebra)
  • Download Code

Replies are listed 'Best First'.
Re: perl module or code for calculating the shortest distance between a line and a point (vector linear algebra)
by zwon (Abbot) on Dec 30, 2009 at 10:14 UTC
Re: perl module or code for calculating the shortest distance between a line and a point (vector linear algebra)
by GrandFather (Saint) on Dec 30, 2009 at 10:33 UTC

    zwon gave my first look reply. However I'm curious about your else if construct. Is that a typo (which implies you didn't copy and paste actual code), or haven't you bothered to tell us about the actual problem you are having?

    Maybe if zwon's answer isn't the solution you are looking for you should reply with some code that demonstrates the error you are getting and (for some very small sample data set) give an expected answer. Oh, and always use strictures (use strict; use warnings;).


    True laziness is hard work
      Is that a typo (which implies you didn't copy and paste actual code)

      Looks very much like pseudo code to me...  In other words, I suppose the OP is more interested in help with the algorithm, than with the concrete Perl code shown.

Re: perl module or code for calculating the shortest distance between a line and a point (vector linear algebra)
by marto (Cardinal) on Dec 30, 2009 at 10:22 UTC

    PDB file, sounds like something I'd expect BioPerl to deal with, perhaps Bio::Coordinate::Graph? That said, I'm no biologist so I could be totally wrong.

    Martin

Re: perl module or code for calculating the shortest distance between a line and a point (vector linear algebra)
by randyk (Parson) on Dec 30, 2009 at 17:00 UTC
    A general algorithm for calculating the closest distance between a line and a point is described, for example, at http://math.ucsd.edu/~wgarner/math4c/derivations/distance/distptline.htm. Here's a script that illustrates this:
    use strict; use warnings; # line passes through (x1, y1) and (x2, y2) my ($x1, $y1) = (0, 2); my ($x2, $y2) = (-2, 2); # put it into the form y = mx + b my $m = ($y2 - $y1) / ($x2 - $x1); my $b = ($y1 * $x2 - $y2 * $x1) / ($x2 - $x1); # target point is (x0, y0) my ($x0, $y0) = (8, 0); # shortest distance from line to target point my $d = abs( $y0 - $m * $x0 - $b) / sqrt($m * $m + 1); print "The closest distance is $d\n";
      hi randyk,

      yes, that's true, for the plane.

      in his code he seems to use $z(point) which suggests these lines and points are in 3D space. the shortest distance from a point to a line is a perpendicular on the line passing through that point. see here the development of a formula for the problem in question.

      Your code can't handle the case where $x2 == $x1.
Re: perl module or code for calculating the shortest distance between a line and a point (vector linear algebra)
by salva (Canon) on Dec 30, 2009 at 20:34 UTC
    Use some vector manipulation package as for instance Math::Vector::Real (that I have just uploaded to CPAN!):
    use Math::Vector::Real; my $l0 = V(?, ?); my $l1 = V(?, ?); my $p = V(?, ?); my $u = $l1 - $l0; $u /= abs($u); my $n = $p - $l0; $n -= ($n * $u) * $u; printf "distance is %d\n", abs($n);
Re: perl module or code for calculating the shortest distance between a line and a point (vector linear algebra)
by Limbic~Region (Chancellor) on Dec 31, 2009 at 21:03 UTC
    fraizerangus,
    It sounds like you have a lot of "lines" and a single reference "point" that you want to calculate distances on. You have been provided a number of good options.

    I was a bit curious as to a PDB file so I googled it and found this and this which suggest that you are provided with an X,Y and Z of the atom in relationship to the cell's origin. This is just a point, not a line. Where are your "lines" coming from?

    I was wondering how many computations needed to be made, what accuracy was required and how important runtime was. For instance, would using Inline::C be in order? Would it be possible to take advantage of a loose margin of error to normalize the inputs to use a cache lookup (either precomputed or memoized) for speedier results. None of this is actually important - it just seemed like a potentially interesting problem so respond if you have time.

    Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://814899]
Approved by zwon
Front-paged by matze77
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found