Class: Chingu::Traits::Vectors::Vector
- Inherits:
-
Object
- Object
- Chingu::Traits::Vectors::Vector
- Includes:
- Comparable, Enumerable, NamedParameters
- Defined in:
- lib/vector.rb
Overview
A two dimensional vector
Constant Summary collapse
- ZERO =
Vector.new(x: 0, y: 0)
- UP =
Vector.new(x: 0, y: -1)
- DOWN =
Vector.new(x: 0, y: 1)
- LEFT =
Vector.new(x: -1, y: 0)
- RIGHT =
Vector.new(x: 1, y: 0)
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
Returns a new Vector that is
scalartimes longer thanself. -
#+(vector) ⇒ Object
Adds
vectorto self, not manipulating. -
#-(vector) ⇒ Object
Substracts
vectorto self, not manipulating. - #-@ ⇒ Object
-
#/(scalar) ⇒ Object
Returns a new Vector that is
scalartimes shorter thanself. - #<=>(other) ⇒ Object
-
#[](index) ⇒ Object
The object at the specified index.
- #[]=(index, object) ⇒ Object
- #each {|x| ... } ⇒ Object
-
#initialize(x, y) ⇒ Vector
constructor
A new instance of Vector.
-
#length ⇒ Numeric
Returns length of vector.
-
#normalize ⇒ Object
Returns a new vector with the same direction but with length 1.
-
#normalize! ⇒ Vector
Shortens / lengthnes the vector to length 1.
- #to_s ⇒ Object
Constructor Details
#initialize(x, y) ⇒ Vector
Returns a new instance of Vector.
52 53 54 55 |
# File 'lib/vector.rb', line 52 def initialize(x, y) self[0] = x self[1] = y end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
11 12 13 |
# File 'lib/vector.rb', line 11 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
11 12 13 |
# File 'lib/vector.rb', line 11 def y @y end |
Class Method Details
.self.[](x, y) ⇒ Object .self.[](array) ⇒ Object .self.[](vector) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/vector.rb', line 67 def self.[](x, y=0) if (x.is_a?(Numeric) && y.is_a?(Numeric)) new(x: x, y: y) elsif (x.is_a? Array) new(x: x[0], y: x[1]) elsif (x.is_a? Vector) new(x: x.x, y: x.y) end end |
Instance Method Details
#*(scalar) ⇒ Object
Returns a new Vector that is scalar times longer than self. Direction stays the same.
122 123 124 |
# File 'lib/vector.rb', line 122 def * (scalar) Vector[x*scalar, y*scalar] end |
#+(vector) ⇒ Object
Adds vector to self, not manipulating
104 105 106 |
# File 'lib/vector.rb', line 104 def + (vector) Vector[x+vector[0], y+vector[1]] end |
#-(vector) ⇒ Object
Substracts vector to self, not manipulating
113 114 115 |
# File 'lib/vector.rb', line 113 def - (vector) self + (vector * -1) end |
#-@ ⇒ Object
140 141 142 |
# File 'lib/vector.rb', line 140 def -@ self * -1 end |
#/(scalar) ⇒ Object
Returns a new Vector that is scalar times shorter than self. Direction stays the same.
132 133 134 135 136 137 138 |
# File 'lib/vector.rb', line 132 def / (scalar) if scalar == 0 raise ZeroDivisionError("Division of vectors with zero not allowed") else self * (1.0 / scalar) end end |
#<=>(other) ⇒ Object
15 16 17 |
# File 'lib/vector.rb', line 15 def <=>(other) length <=> other.length end |
#[](index) ⇒ Object
Returns The object at the specified index.
29 30 31 32 |
# File 'lib/vector.rb', line 29 def [](index) (index == 0 || index == :x) ? x : (index == 1 || index == :y) ? y : (raise IndexError.new("Vectors have only two dimensions")) end |
#[]=(index, object) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/vector.rb', line 36 def []=(index, object) if (index == 0 || index == :x) self.x = object object elsif (index == 1 || index == :y) self.y = object object else raise IndexError.new("Vectors have only two dimensions") end end |
#each {|x| ... } ⇒ Object
21 22 23 24 |
# File 'lib/vector.rb', line 21 def each yield x yield y end |
#length ⇒ Numeric
Returns length of vector
95 96 97 |
# File 'lib/vector.rb', line 95 def length() Math.sqrt(x**2 + y**2) end |
#normalize ⇒ Object
Returns a new vector with the same direction but with length 1.
147 148 149 150 |
# File 'lib/vector.rb', line 147 def normalize return self if length == 0 self / length end |
#normalize! ⇒ Vector
Shortens / lengthnes the vector to length 1
156 157 158 159 160 161 |
# File 'lib/vector.rb', line 156 def normalize! normalized = normalize self.x = normalized.x self.y = normalized.y self end |
#to_s ⇒ Object
86 87 88 |
# File 'lib/vector.rb', line 86 def to_s "Vector[#{x},#{y}]" end |