Class: Mittsu::Vector2

Inherits:
Vector
  • Object
show all
Defined in:
lib/mittsu/math/vector2.rb

Constant Summary collapse

ELEMENTS =
{ x: 0, y: 1 }
DIMENSIONS =
ELEMENTS.count

Instance Attribute Summary

Attributes inherited from Vector

#elements

Instance Method Summary collapse

Methods inherited from Vector

#==, #[], #[]=, #add, #add_scalar, #add_vectors, #angle_to, #ceil, #clamp, #clamp_scalar, #clone, #copy, #distance_to, #divide, #divide_scalar, #each_dimension, #floor, #from_array, #length, #length_sq, #lerp, #lerp_vectors, #max, #min, #multiply, #multiply_scalar, #multiply_vectors, #negate, #normalize, #project_on_plane, #project_on_vector, #reflect, #round, #round_to_zero, #set_length, #sub, #sub_scalar, #sub_vectors, #to_array, #to_s

Constructor Details

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

Returns a new instance of Vector2.



9
10
11
# File 'lib/mittsu/math/vector2.rb', line 9

def initialize(x = 0, y = 0)
  super [x.to_f, y.to_f]
end

Instance Method Details

#distance_to_squared(v) ⇒ Object



27
28
29
30
# File 'lib/mittsu/math/vector2.rb', line 27

def distance_to_squared(v)
  dx, dy = x - v.x, y - v.y
  dx * dx + dy * dy
end

#dot(v) ⇒ Object



23
24
25
# File 'lib/mittsu/math/vector2.rb', line 23

def dot(v)
  x * v.x + y * v.y
end

#from_attribute(attribute, index, offset = 0) ⇒ Object



32
33
34
35
36
37
# File 'lib/mittsu/math/vector2.rb', line 32

def from_attribute(attribute, index, offset = 0)
  index = index * attribute.item_size + offset
  @elements[0] = attribute.array[index]
  @elements[1] = attribute.array[index + 1]
  self
end

#set(x, y) ⇒ Object



13
14
15
# File 'lib/mittsu/math/vector2.rb', line 13

def set(x, y)
  super [x.to_f, y.to_f]
end

#xObject



17
# File 'lib/mittsu/math/vector2.rb', line 17

def x; @elements[0]; end

#x=(value) ⇒ Object



20
# File 'lib/mittsu/math/vector2.rb', line 20

def x=(value); @elements[0] = value.to_f; end

#yObject



18
# File 'lib/mittsu/math/vector2.rb', line 18

def y; @elements[1]; end

#y=(value) ⇒ Object



21
# File 'lib/mittsu/math/vector2.rb', line 21

def y=(value); @elements[1] = value.to_f; end