Class: Three::Vector2

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/three/math/vector2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0) ⇒ Vector2

Returns a new instance of Vector2.



9
10
11
12
13
# File 'lib/three/math/vector2.rb', line 9

def initialize(x = 0, y = 0)
  @x = x
  @y = y
  @on_change_callback = proc {}
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



7
8
9
# File 'lib/three/math/vector2.rb', line 7

def x
  @x
end

#yObject

Returns the value of attribute y.



7
8
9
# File 'lib/three/math/vector2.rb', line 7

def y
  @y
end

Instance Method Details

#==(other) ⇒ Object



59
60
61
# File 'lib/three/math/vector2.rb', line 59

def ==(other)
  other.is_a?(self.class) && equals?(other)
end

#cloneObject



36
37
38
# File 'lib/three/math/vector2.rb', line 36

def clone
  self.class.new(@x, @y)
end

#copy(vector) ⇒ Object



40
41
42
# File 'lib/three/math/vector2.rb', line 40

def copy(vector)
  set(vector.x, vector.y)
end

#deconstructObject



74
75
76
# File 'lib/three/math/vector2.rb', line 74

def deconstruct
  to_a
end

#each {|@x| ... } ⇒ Object

Yields:

  • (@x)


63
64
65
66
67
68
# File 'lib/three/math/vector2.rb', line 63

def each
  return enum_for(:each) unless block_given?

  yield @x
  yield @y
end

#equals?(vector, epsilon: 0.0) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/three/math/vector2.rb', line 54

def equals?(vector, epsilon: 0.0)
  (@x - vector.x).abs <= epsilon &&
    (@y - vector.y).abs <= epsilon
end

#from_array(array, offset = 0) ⇒ Object



44
45
46
# File 'lib/three/math/vector2.rb', line 44

def from_array(array, offset = 0)
  set(array[offset], array[offset + 1])
end

#inspectObject



85
86
87
# File 'lib/three/math/vector2.rb', line 85

def inspect
  "#<#{self.class} x=#{@x.inspect} y=#{@y.inspect}>"
end

#on_change(&callback) ⇒ Object Also known as: _on_change



78
79
80
81
# File 'lib/three/math/vector2.rb', line 78

def on_change(&callback)
  @on_change_callback = callback || proc {}
  self
end

#set(x, y) ⇒ Object



25
26
27
28
29
30
# File 'lib/three/math/vector2.rb', line 25

def set(x, y)
  @x = x
  @y = y
  changed!
  self
end

#set_scalar(scalar) ⇒ Object



32
33
34
# File 'lib/three/math/vector2.rb', line 32

def set_scalar(scalar)
  set(scalar, scalar)
end

#to_aObject



70
71
72
# File 'lib/three/math/vector2.rb', line 70

def to_a
  [@x, @y]
end

#to_array(array = [], offset = 0) ⇒ Object



48
49
50
51
52
# File 'lib/three/math/vector2.rb', line 48

def to_array(array = [], offset = 0)
  array[offset] = @x
  array[offset + 1] = @y
  array
end