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)



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

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



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.

Examples:

Vector*5 #=> Vector

Parameters:

  • scalar (Numeric)


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

Examples:

Parameters:

  • vector (Vector, Array)

    Vector or Array with Numeric on index 0 and 1



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

Examples:

Parameters:

  • vector (Vector, Array)

    Vector or Array with Numeric on index 0 and 1



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.

Examples:

Vector/5 #=> Vector

Parameters:

  • scalar (Numeric)

Raises:

  • (ZeroDivisionError)

    When scalar == 0



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.

Parameters:

  • index (Number, Symbol)

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

Returns:

  • (Object)

    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

Parameters:

  • index (Number, Symbol)

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

  • 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

Yields:

  • (x)


21
22
23
24
# File 'lib/vector.rb', line 21

def each
  yield x
  yield y
end

#lengthNumeric

Returns length of vector

Examples:

Vector.length #=> 7.07106781

Returns:

  • (Numeric)

    Length of the vector



95
96
97
# File 'lib/vector.rb', line 95

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

#normalizeObject

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

Returns:



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_sObject



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

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