Class: Geometry::Vector

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#xObject

Returns the value of attribute x

Returns:

  • (Object)

    the current value of x


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

def x
  @x
end

#yObject

Returns the value of attribute y

Returns:

  • (Object)

    the current value of y


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

def y
  @y
end

Instance Method Details

#*(scalar) ⇒ Object


38
39
40
# File 'lib/geometry/vector.rb', line 38

def *(scalar)      
  Vector.new(x * scalar, y * scalar)
end

#+(vector) ⇒ Object


30
31
32
# File 'lib/geometry/vector.rb', line 30

def +(vector)
  Vector.new(x + vector.x, y + vector.y)
end

#-(vector) ⇒ Object


34
35
36
# File 'lib/geometry/vector.rb', line 34

def -(vector)
  self + (-1) * vector
end

#==(vector) ⇒ Object


3
4
5
# File 'lib/geometry/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/geometry/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

Returns:

  • (Boolean)

26
27
28
# File 'lib/geometry/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/geometry/vector.rb', line 17

def cross_product(vector)
  x * vector.y - y * vector.x
end

#modulusObject

Modulus of vector. Also known as length, size or norm


8
9
10
# File 'lib/geometry/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/geometry/vector.rb', line 22

def scalar_product(vector)
  x * vector.x + y * vector.y
end