Module: BioDSL::Math

Defined in:
lib/BioDSL/math.rb

Overview

Adding methods to Math module.

Class Method Summary collapse

Class Method Details

.dist_point2line(px, py, x1, y1, x2, y2) ⇒ Object

Class method to calculate the distance from at point to a line. The point and line are given as pairs of coordinates.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/BioDSL/math.rb', line 34

def self.dist_point2line(
  px,  # point  x coordinate
  py,  # point  y coordinate
  x1,  # line 1 x coordinate
  y1,  # line 1 y coordinate
  x2,  # line 2 x coordinate
  y2   # line 2 y coordinate
)

  a = (y2 - y1).to_f / (x2 - x1).to_f
  b = y1 - a * x1

  (a * px + b - py).abs / ::Math.sqrt(a**2 + 1)
end

.dist_point2point(x1, y1, x2, y2) ⇒ Object

Class method to calculate the distance between two points given as pairs of coordinates.



51
52
53
# File 'lib/BioDSL/math.rb', line 51

def self.dist_point2point(x1, y1, x2, y2)
  ::Math.sqrt((x2.to_f - x1.to_f)**2 + (y2.to_f - y1.to_f)**2)
end