Class: GeometricLine
- Inherits:
-
Struct
- Object
- Struct
- GeometricLine
- Defined in:
- lib/flash_math/modules/geometry/geometric_line.rb
Instance Attribute Summary collapse
-
#point1 ⇒ Object
Returns the value of attribute point1.
-
#point2 ⇒ Object
Returns the value of attribute point2.
Class Method Summary collapse
Instance Method Summary collapse
- #angle_to(another_line) ⇒ Object
- #distance_to(point) ⇒ Object
- #horizontal? ⇒ Boolean
- #intersect_x(another_line) ⇒ Object
- #parallel_to?(another_line) ⇒ Boolean
- #slope ⇒ Object
- #vertical? ⇒ Boolean
- #x_intercept ⇒ Object
- #y_intercept ⇒ Object
Instance Attribute Details
#point1 ⇒ Object
Returns the value of attribute point1
1 2 3 |
# File 'lib/flash_math/modules/geometry/geometric_line.rb', line 1 def point1 @point1 end |
#point2 ⇒ Object
Returns the value of attribute 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
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
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 |
#slope ⇒ Object
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
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_intercept ⇒ Object
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_intercept ⇒ Object
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 |