Class: Vector

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable, Enumerable
Defined in:
lib/nswtopo/geometry/vector.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Vector

Returns a new instance of Vector.



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

def initialize(x, y)
  @x, @y = x, y
  freeze
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



11
12
13
# File 'lib/nswtopo/geometry/vector.rb', line 11

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



11
12
13
# File 'lib/nswtopo/geometry/vector.rb', line 11

def y
  @y
end

Class Method Details

.[](x, y) ⇒ Object



7
8
9
# File 'lib/nswtopo/geometry/vector.rb', line 7

def self.[](x, y)
  new x, y
end

Instance Method Details

#*(scalar) ⇒ Object



54
55
56
# File 'lib/nswtopo/geometry/vector.rb', line 54

def *(scalar)
  Vector[@x * scalar, @y * scalar]
end

#+(other) ⇒ Object



46
47
48
# File 'lib/nswtopo/geometry/vector.rb', line 46

def +(other)
  Vector[@x + other.x, @y + other.y]
end

#+@Object



62
63
64
# File 'lib/nswtopo/geometry/vector.rb', line 62

def +@
  self
end

#-(other) ⇒ Object



50
51
52
# File 'lib/nswtopo/geometry/vector.rb', line 50

def -(other)
  Vector[@x - other.x, @y - other.y]
end

#-@Object



66
67
68
# File 'lib/nswtopo/geometry/vector.rb', line 66

def -@
  self * -1
end

#/(scalar) ⇒ Object



58
59
60
# File 'lib/nswtopo/geometry/vector.rb', line 58

def /(scalar)
  Vector[@x / scalar, @y / scalar]
end

#angleObject



82
83
84
# File 'lib/nswtopo/geometry/vector.rb', line 82

def angle
  Math::atan2 @y, @x
end

#cross(other) ⇒ Object



78
79
80
# File 'lib/nswtopo/geometry/vector.rb', line 78

def cross(other)
  perp.dot other
end

#dot(other) ⇒ Object



70
71
72
# File 'lib/nswtopo/geometry/vector.rb', line 70

def dot(other)
  @x * other.x + @y * other.y
end

#inspectObject



24
25
26
# File 'lib/nswtopo/geometry/vector.rb', line 24

def inspect
  "{%s, %s}" % [@x, @y]
end

#normObject



86
87
88
# File 'lib/nswtopo/geometry/vector.rb', line 86

def norm
  Math::sqrt(dot self)
end

#normalisedObject



90
91
92
# File 'lib/nswtopo/geometry/vector.rb', line 90

def normalised
  self / norm
end

#perpObject



74
75
76
# File 'lib/nswtopo/geometry/vector.rb', line 74

def perp
  Vector[-@y, @x]
end

#proj(other) ⇒ Object



94
95
96
# File 'lib/nswtopo/geometry/vector.rb', line 94

def proj(other)
  dot(other) / other.norm
end

#rotate_by(angle) ⇒ Object



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

def rotate_by(angle)
  cos = Math::cos(angle)
  sin = Math::sin(angle)
  Vector[@x * cos - @y * sin, @x * sin + @y * cos]
end

#rotate_by_degrees(angle) ⇒ Object



42
43
44
# File 'lib/nswtopo/geometry/vector.rb', line 42

def rotate_by_degrees(angle)
  rotate_by(angle * Math::PI / 180.0)
end

#to_aryObject



13
14
15
# File 'lib/nswtopo/geometry/vector.rb', line 13

def to_ary
  [@x, @y]
end

#to_dObject



28
29
30
# File 'lib/nswtopo/geometry/vector.rb', line 28

def to_d
  Vector[@x.to_d, @y.to_d]
end

#to_fObject



32
33
34
# File 'lib/nswtopo/geometry/vector.rb', line 32

def to_f
  Vector[@x.to_f, @y.to_f]
end