Class: GeometricVector
- Inherits:
-
Struct
- Object
- Struct
- GeometricVector
- Defined in:
- lib/flash_math/modules/geometry/geometric_vector.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #*(scalar) ⇒ Object
- #+(vector) ⇒ Object
- #-(vector) ⇒ Object
- #==(vector) ⇒ Object
- #coerce(scalar) ⇒ Object
- #collinear_with?(vector) ⇒ Boolean
- #cross_product(vector) ⇒ Object
- #modulus ⇒ Object
- #scalar_product(vector) ⇒ Object
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x
1 2 3 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 1 def x @x end |
#y ⇒ Object
Returns the value of attribute y
1 2 3 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 1 def y @y end |
Class Method Details
.new_by_array(array) ⇒ Object
3 4 5 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 3 def self.new_by_array(array) self.new(array[0], array[1]) end |
Instance Method Details
#*(scalar) ⇒ Object
19 20 21 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 19 def *(scalar) GeometricVector.new(x * scalar, y * scalar) end |
#+(vector) ⇒ Object
11 12 13 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 11 def +(vector) GeometricVector.new(x + vector.x, y + vector.y) end |
#-(vector) ⇒ Object
15 16 17 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 15 def -(vector) self + (-1) * vector end |
#==(vector) ⇒ Object
7 8 9 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 7 def ==(vector) x === vector.x && y === vector.y end |
#coerce(scalar) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 23 def coerce(scalar) if scalar.is_a?(Numeric) [self, scalar] else raise ArgumentError, "GeometricVector: cannot coerce #{scalar.inspect}" end end |
#collinear_with?(vector) ⇒ Boolean
39 40 41 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 39 def collinear_with?(vector) cross_product(vector) === 0 end |
#cross_product(vector) ⇒ Object
31 32 33 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 31 def cross_product(vector) x * vector.y - y * vector.x end |
#modulus ⇒ Object
43 44 45 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 43 def modulus Math.hypot(x ,y) end |
#scalar_product(vector) ⇒ Object
35 36 37 |
# File 'lib/flash_math/modules/geometry/geometric_vector.rb', line 35 def scalar_product(vector) x * vector.x + y * vector.y end |