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
scalar
times longer thanself
. -
#+(vector) ⇒ Object
Adds
vector
to self, not manipulating. -
#-(vector) ⇒ Object
Substracts
vector
to self, not manipulating. - #-@ ⇒ Object
-
#/(scalar) ⇒ Object
Returns a new Vector that is
scalar
times shorter thanself
. - #<=>(other) ⇒ Object
-
#[](index) ⇒ Object
The object at the specified index.
- #[]=(index, object) ⇒ Object
- #each {|x| ... } ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ 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.
68 69 70 71 |
# File 'lib/vector.rb', line 68 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
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vector.rb', line 83 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.
138 139 140 |
# File 'lib/vector.rb', line 138 def * (scalar) Vector[x*scalar, y*scalar] end |
#+(vector) ⇒ Object
Adds vector
to self, not manipulating
120 121 122 |
# File 'lib/vector.rb', line 120 def + (vector) Vector[x+vector[0], y+vector[1]] end |
#-(vector) ⇒ Object
Substracts vector
to self, not manipulating
129 130 131 |
# File 'lib/vector.rb', line 129 def - (vector) self + (vector * -1) end |
#-@ ⇒ Object
156 157 158 |
# File 'lib/vector.rb', line 156 def -@ self * -1 end |
#/(scalar) ⇒ Object
Returns a new Vector that is scalar
times shorter than self
. Direction stays the same.
148 149 150 151 152 153 154 |
# File 'lib/vector.rb', line 148 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 18 19 20 21 22 |
# File 'lib/vector.rb', line 15 def <=>(other) x_comp = self.x <=> other.x if x_comp == 0 self.y <=> other.y else x_comp end end |
#[](index) ⇒ Object
Returns The object at the specified index.
45 46 47 48 |
# File 'lib/vector.rb', line 45 def [](index) (index == 0 || index == :x) ? x : (index == 1 || index == :y) ? y : (raise IndexError.new("Vectors have only two dimensions")) end |
#[]=(index, object) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/vector.rb', line 52 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
37 38 39 40 |
# File 'lib/vector.rb', line 37 def each yield x yield y end |
#eql?(other) ⇒ Boolean Also known as: ==
24 25 26 27 |
# File 'lib/vector.rb', line 24 def eql?(other) self.x == other.x && self.y == other.y end |
#hash ⇒ Object
31 32 33 |
# File 'lib/vector.rb', line 31 def hash x.hash ^ y.hash end |
#length ⇒ Numeric
Returns length of vector
111 112 113 |
# File 'lib/vector.rb', line 111 def length() Math.sqrt(x**2 + y**2) end |
#normalize ⇒ Object
Returns a new vector with the same direction but with length 1.
163 164 165 166 |
# File 'lib/vector.rb', line 163 def normalize return self if length == 0 self / length end |
#normalize! ⇒ Vector
Shortens / lengthnes the vector to length 1
172 173 174 175 176 177 |
# File 'lib/vector.rb', line 172 def normalize! normalized = normalize self.x = normalized.x self.y = normalized.y self end |
#to_s ⇒ Object
102 103 104 |
# File 'lib/vector.rb', line 102 def to_s "Vector[#{x},#{y}]" end |