Class: Chingu::Traits::Vectors::Vector

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Vector

Returns a new instance of Vector.

Parameters:

  • x (Numeric)

    (0)

  • y (Numeric)

    (0)



68
69
70
71
# File 'lib/vector.rb', line 68

def initialize(x, y)
  self[0] = x
  self[1] = y
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

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

Overloads:

  • .self.[](x, y) ⇒ Object

    Create a new Vector with x and y values

    Parameters:

    • x (Numeric)
    • y (Numeric)
  • .self.[](array) ⇒ Object

    Convert the array into a new Vector

    Parameters:

    • array (Array<Numeric>)

      Needs Numerics at index 0 and 1

  • .self.[](vector) ⇒ Object

    Copies a vector

    Parameters:

    • vector (Vector)

      The Vector to be copied



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.

Examples:

Vector*5 #=> Vector

Parameters:

  • scalar (Numeric)


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

Examples:

Parameters:

  • vector (Vector, Array)

    Vector or Array with Numeric on index 0 and 1



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

Examples:

Parameters:

  • vector (Vector, Array)

    Vector or Array with Numeric on index 0 and 1



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.

Examples:

Vector/5 #=> Vector

Parameters:

  • scalar (Numeric)

Raises:

  • (ZeroDivisionError)

    When scalar == 0



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.

Parameters:

  • index (Number, Symbol)

    Accessing the dimensions of the vector with 1, 2, :x or :y

Returns:

  • (Object)

    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

Parameters:

  • index (Number, Symbol)

    Accessing the dimensions of the vector with 1, 2, :x or :y

  • 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

Yields:

  • (x)


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

def each
  yield x
  yield y
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


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

def eql?(other)
  self.x == other.x &&
      self.y == other.y
end

#hashObject



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

def hash
  x.hash ^ y.hash
end

#lengthNumeric

Returns length of vector

Examples:

Vector.length #=> 7.07106781

Returns:

  • (Numeric)

    Length of the vector



111
112
113
# File 'lib/vector.rb', line 111

def length()
  Math.sqrt(x**2 + y**2)
end

#normalizeObject

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

Returns:



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_sObject



102
103
104
# File 'lib/vector.rb', line 102

def to_s
  "Vector[#{x},#{y}]"
end