Class: Three::Vector2
- Inherits:
-
Object
- Object
- Three::Vector2
- Includes:
- Enumerable
- Defined in:
- lib/three/math/vector2.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #clone ⇒ Object
- #copy(vector) ⇒ Object
- #deconstruct ⇒ Object
- #each {|@x| ... } ⇒ Object
- #equals?(vector, epsilon: 0.0) ⇒ Boolean
- #from_array(array, offset = 0) ⇒ Object
-
#initialize(x = 0, y = 0) ⇒ Vector2
constructor
A new instance of Vector2.
- #inspect ⇒ Object
- #on_change(&callback) ⇒ Object (also: #_on_change)
- #set(x, y) ⇒ Object
- #set_scalar(scalar) ⇒ Object
- #to_a ⇒ Object
- #to_array(array = [], offset = 0) ⇒ Object
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
#x ⇒ Object
Returns the value of attribute x.
7 8 9 |
# File 'lib/three/math/vector2.rb', line 7 def x @x end |
#y ⇒ Object
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 |
#clone ⇒ Object
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 |
#deconstruct ⇒ Object
74 75 76 |
# File 'lib/three/math/vector2.rb', line 74 def deconstruct to_a end |
#each {|@x| ... } ⇒ Object
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
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 |
#inspect ⇒ Object
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_a ⇒ Object
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 |