Class: Geometry::Line

Inherits:
Struct
  • Object
show all
Defined in:
lib/geometry/line.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#point1Object

Returns the value of attribute point1


2
3
4
# File 'lib/geometry/line.rb', line 2

def point1
  @point1
end

#point2Object

Returns the value of attribute point2


2
3
4
# File 'lib/geometry/line.rb', line 2

def point2
  @point2
end

Class Method Details

.new_by_arrays(point1_coordinates, point2_coordinates) ⇒ Object


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

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

Instance Method Details

#angle_to(other) ⇒ Object


74
75
76
77
78
79
# File 'lib/geometry/line.rb', line 74

def angle_to(other)
  # return absolute difference between angles to horizontal of self and other
  sa = Math::atan(slope)
  oa = Math::atan(other.slope)
  (sa-oa).abs
end

#distance_to(point) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/geometry/line.rb', line 81

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


48
49
50
# File 'lib/geometry/line.rb', line 48

def horizontal?
  slope == 0
end

#intersect_x(other) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/geometry/line.rb', line 52

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

  return nil if horizontal? and other.horizontal?

  return x_intercept if vertical?
  return other.x_intercept if other.vertical?

  d_intercept = other.y_intercept - y_intercept
  d_slope = slope - other.slope

  # if d_intercept and d_slope are both 0, the result is NaN, which indicates
  # the lines are identical
  d_intercept / d_slope
end

#lengthObject


93
94
95
# File 'lib/geometry/line.rb', line 93

def length
  point1.distance_to(point2)
end

#parallel_to?(other) ⇒ Boolean


33
34
35
36
37
38
# File 'lib/geometry/line.rb', line 33

def parallel_to?(other)
  # Special handling for when one slope is inf and the other is -inf:
  return true if slope.infinite? and other.slope.infinite?

  slope == other.slope
end

#slopeObject


8
9
10
11
12
13
14
15
# File 'lib/geometry/line.rb', line 8

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


40
41
42
43
44
45
46
# File 'lib/geometry/line.rb', line 40

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

#x_interceptObject


25
26
27
28
29
30
31
# File 'lib/geometry/line.rb', line 25

def x_intercept
  return nil if horizontal?

  # compute change in x between point1 and the origin
  dx = point1.y / slope
  point1.x - dx
end

#y_interceptObject


17
18
19
20
21
22
23
# File 'lib/geometry/line.rb', line 17

def y_intercept
  return nil if vertical?

  # compute change in y between point1 and the origin
  dy = point1.x * slope
  point1.y - dy
end