Class: WindingPolygon::Vector
- Inherits:
-
Struct
- Object
- Struct
- WindingPolygon::Vector
- Defined in:
- lib/winding-polygon/vector.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #*(scalar) ⇒ Object
- #+(vector) ⇒ Object
- #-(vector) ⇒ Object
- #==(vector) ⇒ Object
- #coerce(scalar) ⇒ Object
- #collinear_with?(vector) ⇒ Boolean
-
#cross_product(vector) ⇒ Object
z-coordinate of cross product (also known as vector product or outer product) It is positive if other vector should be turned counter-clockwise in order to superpose them.
-
#modulus ⇒ Object
Modulus of vector.
-
#scalar_product(vector) ⇒ Object
Scalar product, also known as inner product or dot product.
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x
2 3 4 |
# File 'lib/winding-polygon/vector.rb', line 2 def x @x end |
#y ⇒ Object
Returns the value of attribute y
2 3 4 |
# File 'lib/winding-polygon/vector.rb', line 2 def y @y end |
Instance Method Details
#*(scalar) ⇒ Object
38 39 40 |
# File 'lib/winding-polygon/vector.rb', line 38 def *(scalar) Vector.new(x * scalar, y * scalar) end |
#+(vector) ⇒ Object
30 31 32 |
# File 'lib/winding-polygon/vector.rb', line 30 def +(vector) Vector.new(x + vector.x, y + vector.y) end |
#-(vector) ⇒ Object
34 35 36 |
# File 'lib/winding-polygon/vector.rb', line 34 def -(vector) self + (-1) * vector end |
#==(vector) ⇒ Object
3 4 5 |
# File 'lib/winding-polygon/vector.rb', line 3 def ==(vector) x === vector.x && y === vector.y end |
#coerce(scalar) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/winding-polygon/vector.rb', line 42 def coerce(scalar) if scalar.is_a?(Numeric) [self, scalar] else raise ArgumentError, "Vector: cannot coerce #{scalar.inspect}" end end |
#collinear_with?(vector) ⇒ Boolean
26 27 28 |
# File 'lib/winding-polygon/vector.rb', line 26 def collinear_with?(vector) cross_product(vector) === 0 end |
#cross_product(vector) ⇒ Object
z-coordinate of cross product (also known as vector product or outer product) It is positive if other vector should be turned counter-clockwise in order to superpose them. It is negetive if other vector should be turned clockwise in order to superpose them. It is zero when vectors are collinear. Remark: x- and y- coordinates of plane vectors cross product are always zero
17 18 19 |
# File 'lib/winding-polygon/vector.rb', line 17 def cross_product(vector) x * vector.y - y * vector.x end |
#modulus ⇒ Object
Modulus of vector. Also known as length, size or norm
8 9 10 |
# File 'lib/winding-polygon/vector.rb', line 8 def modulus Math.hypot(x ,y) end |
#scalar_product(vector) ⇒ Object
Scalar product, also known as inner product or dot product
22 23 24 |
# File 'lib/winding-polygon/vector.rb', line 22 def scalar_product(vector) x * vector.x + y * vector.y end |