Class: Bivector

Inherits:
Object
  • Object
show all
Defined in:
lib/bivector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xCoordinate, yCoordinate) ⇒ Bivector

Returns a new instance of Bivector.



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

def initialize(xCoordinate, yCoordinate)
  @x, @y = xCoordinate, yCoordinate
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#*(aNumber) ⇒ Object



28
29
30
31
32
# File 'lib/bivector.rb', line 28

def *(aNumber)
  if aNumber.is_a? Numeric
    Bivector.new(x * aNumber, y * aNumber)
  end
end

#+(another) ⇒ Object



16
17
18
19
20
# File 'lib/bivector.rb', line 16

def +(another)
  if another.is_a? Bivector
    Bivector.new(x + another.x, y + another.y)
  end
end

#-(another) ⇒ Object



22
23
24
25
26
# File 'lib/bivector.rb', line 22

def -(another)
  if another.is_a? Bivector
    Bivector.new(x - another.x, y - another.y)
  end
end

#==(another) ⇒ Object



12
13
14
# File 'lib/bivector.rb', line 12

def ==(another)
  another.is_a?(Bivector) && x == another.x && y == another.y
end

#angles_to(another, style = :rad) ⇒ Object



64
65
66
67
68
# File 'lib/bivector.rb', line 64

def angles_to(another, style = :rad)
  if another.is_a? Bivector
    BV::Angle.new(Math.acos(dot_product(another) / ( norm * another.norm))).values_in(style)
  end
end

#cross_product(another) ⇒ Object Also known as: outer_product



52
53
54
55
56
# File 'lib/bivector.rb', line 52

def cross_product(another)
  if another.is_a? Bivector
    Bivector.new()
  end
end

#dot_product(another) ⇒ Object Also known as: inner_product



45
46
47
48
49
# File 'lib/bivector.rb', line 45

def dot_product(another)
  if another.is_a? Bivector
    x * another.x + y * another.y
  end
end

#normObject Also known as: length



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

def norm
  (x**2 + y**2)**0.5
end

#parallels_to?(another) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/bivector.rb', line 70

def parallels_to?(another)
  if another.is_a? Bivector
    self.zero? || another.zero? || x * another.y - y * another.x == 0
  end
end

#perpendicular_to?(another) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/bivector.rb', line 76

def perpendicular_to?(another)
  if another.is_a? Bivector
    self.dot_product(another) == 0
  end
end

#polarObject



86
87
88
# File 'lib/bivector.rb', line 86

def polar
  "(#{norm}, #{theta})"
end

#theta(style = :rad) ⇒ Object



82
83
84
# File 'lib/bivector.rb', line 82

def theta(style = :rad)
  BV::Angle.new(Math.atan(y/x)).values_in(style)
end

#to_sObject



8
9
10
# File 'lib/bivector.rb', line 8

def to_s
  "(#{x}, #{y})"
end

#unit_vectorObject



59
60
61
62
# File 'lib/bivector.rb', line 59

def unit_vector
  return nil if self.zero?
  Bivector.new(x / norm, y / norm)
end

#zero?Boolean Also known as: zero_vector?, null_vector?

Returns:

  • (Boolean)


39
40
41
# File 'lib/bivector.rb', line 39

def zero?
  return (x == 0 && y ==0) ? true : false
end