Class: GeometricLine

Inherits:
Struct
  • Object
show all
Defined in:
lib/flash_math/modules/geometry/geometric_line.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#point1Object

Returns the value of attribute point1

Returns:

  • (Object)

    the current value of point1



1
2
3
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 1

def point1
  @point1
end

#point2Object

Returns the value of attribute point2

Returns:

  • (Object)

    the current value of point2



1
2
3
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 1

def point2
  @point2
end

Class Method Details

.new_by_arrays(point1_coordinates, point2_coordinates) ⇒ Object



3
4
5
6
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 3

def self.new_by_arrays(point1_coordinates, point2_coordinates)
  self.new(GeometricPoint.new_by_array(point1_coordinates),
           GeometricPoint.new_by_array(point2_coordinates))
end

Instance Method Details

#angle_to(another_line) ⇒ Object



8
9
10
11
12
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 8

def angle_to(another_line)
  sa = Math::atan(slope)
  oa = Math::atan(another_line.slope)
  (sa-oa).abs
end

#distance_to(point) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 14

def distance_to(point)
  x0 = point.x
  y0 = point.y

  x1 = point1.x
  x2 = point2.x
  y1 = point1.y
  y2 = point2.y

  (((x2-x1)*(y1-y0))-((x1-x0)*(y2-y1))).abs/Math.sqrt((x2-x1)**2+(y2-y1)**2)
end

#horizontal?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 26

def horizontal?
  slope == 0
end

#intersect_x(another_line) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 30

def intersect_x(another_line)
  if vertical? and another_line.vertical?
    if x_intercept == another_line.x_intercept
      return x_intercept
    else
      return nil
    end
  end

  return nil if horizontal? and another_line.horizontal?

  return x_intercept if vertical?
  return another_line.x_intercept if another_line.vertical?

  d_intercept = another_line.y_intercept - y_intercept
  d_slope = slope - another_line.slope

  d_intercept / d_slope
end

#parallel_to?(another_line) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 50

def parallel_to?(another_line)
  return true if slope.infinite? and another_line.slope.infinite?

  slope == another_line.slope
end

#slopeObject



56
57
58
59
60
61
62
63
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 56

def slope
  dy = Float(point2.y - point1.y)
  dx = Float(point2.x - point1.x)

  return 0.0 if dy == 0

  dy / dx
end

#vertical?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 65

def vertical?
  if slope.infinite?
    return true
  else
    return false
  end
end

#x_interceptObject



73
74
75
76
77
78
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 73

def x_intercept
  return nil if horizontal?

  dx = point1.y / slope
  point1.x - dx
end

#y_interceptObject



80
81
82
83
84
85
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 80

def y_intercept
  return nil if vertical?

  dy = point1.x * slope
  point1.y - dy
end