1 # Copyright (C) 2007-2012 Martin A. Hansen.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # http://www.gnu.org/copyleft/gpl.html
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
21 # This software is part of the Biopieces framework (www.biopieces.org).
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 # Class method to calculate the distance from at point to a line.
28 # The point and line are given as pairs of coordinates.
29 def self.dist_point2line(
30 px, # point x coordinate
31 py, # point y coordinate
32 x1, # line 1 x coordinate
33 y1, # line 1 y coordinate
34 x2, # line 2 x coordinate
35 y2 # line 2 y coordinate
38 a = (y2 - y1).to_f / (x2 - x1).to_f
41 (a * px + b - py).abs / Math.sqrt(a ** 2 + 1)
45 # Class method to calculate the distance between two points given
46 # as pairs of coordinates.
47 def self.dist_point2point(x1, y1, x2, y2)
48 Math.sqrt((x2.to_f - x1.to_f) ** 2 + (y2.to_f - y1.to_f) ** 2)