]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/math_aux.rb
fixed file bug
[biopieces.git] / code_ruby / lib / maasha / math_aux.rb
1 # Copyright (C) 2007-2012 Martin A. Hansen.
2
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.
7
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.
12
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.
16
17 # http://www.gnu.org/copyleft/gpl.html
18
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
20
21 # This software is part of the Biopieces framework (www.biopieces.org).
22
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24
25
26 module Math
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
36   )
37
38     a = (y2 - y1).to_f / (x2 - x1).to_f
39     b = y1 - a * x1
40
41     (a * px + b - py).abs / Math.sqrt(a ** 2 + 1)
42   end
43
44
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)
49   end
50 end