Class: SFML::Vector2

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/system/vector2.rb

Overview

2D vector with float components. Operator-friendly and pattern-matchable.

v = SFML::Vector2[3, 4]
v.length          #=> 5.0
v + Vector2[1, 1] #=> Vector2(4, 5)
v * 2             #=> Vector2(6, 8)
x, y = v          # destructures via to_a

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Vector2.



15
16
17
18
19
# File 'lib/sfml/system/vector2.rb', line 15

def initialize(x = 0, y = 0)
  @x = x
  @y = y
  freeze
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



10
11
12
# File 'lib/sfml/system/vector2.rb', line 10

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



10
11
12
# File 'lib/sfml/system/vector2.rb', line 10

def y
  @y
end

Class Method Details

.[](x, y) ⇒ Object



12
# File 'lib/sfml/system/vector2.rb', line 12

def self.[](x, y) = new(x, y)

.from_native(struct) ⇒ Object

:nodoc:



58
59
60
# File 'lib/sfml/system/vector2.rb', line 58

def self.from_native(struct) # :nodoc:
  new(struct[:x], struct[:y])
end

.zeroObject



13
# File 'lib/sfml/system/vector2.rb', line 13

def self.zero     = new(0, 0)

Instance Method Details

#*(scalar) ⇒ Object



23
# File 'lib/sfml/system/vector2.rb', line 23

def *(scalar) = Vector2.new(@x * scalar, @y * scalar)

#+(other) ⇒ Object



21
# File 'lib/sfml/system/vector2.rb', line 21

def +(other) = Vector2.new(@x + other.x, @y + other.y)

#-(other) ⇒ Object



22
# File 'lib/sfml/system/vector2.rb', line 22

def -(other) = Vector2.new(@x - other.x, @y - other.y)

#-@Object



25
# File 'lib/sfml/system/vector2.rb', line 25

def -@ = Vector2.new(-@x, -@y)

#/(scalar) ⇒ Object



24
# File 'lib/sfml/system/vector2.rb', line 24

def /(scalar) = Vector2.new(@x / scalar.to_f, @y / scalar.to_f)

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



34
# File 'lib/sfml/system/vector2.rb', line 34

def ==(other) = other.is_a?(Vector2) && @x == other.x && @y == other.y

#coerce(other) ⇒ Object

Lets Ruby evaluate ‘2 * vec` as `vec * 2`. Without this, Numeric#* would raise TypeError because it doesn’t know about Vector2.

Raises:

  • (TypeError)


29
30
31
32
# File 'lib/sfml/system/vector2.rb', line 29

def coerce(other)
  raise TypeError, "Vector2 cannot coerce #{other.class}" unless other.is_a?(Numeric)
  [self, other]
end

#cross(other) ⇒ Object



48
# File 'lib/sfml/system/vector2.rb', line 48

def cross(other) = (@x * other.y) - (@y * other.x)

#deconstructObject



52
# File 'lib/sfml/system/vector2.rb', line 52

def deconstruct = [@x, @y]

#deconstruct_keys(_keys) ⇒ Object



53
# File 'lib/sfml/system/vector2.rb', line 53

def deconstruct_keys(_keys) = { x: @x, y: @y }

#dot(other) ⇒ Object



47
# File 'lib/sfml/system/vector2.rb', line 47

def dot(other)   = (@x * other.x) + (@y * other.y)

#hashObject



36
# File 'lib/sfml/system/vector2.rb', line 36

def hash = [@x, @y].hash

#lengthObject



38
# File 'lib/sfml/system/vector2.rb', line 38

def length    = Math.sqrt(length_sq)

#length_sqObject



39
# File 'lib/sfml/system/vector2.rb', line 39

def length_sq = (@x * @x) + (@y * @y)

#normalizeObject



41
42
43
44
45
# File 'lib/sfml/system/vector2.rb', line 41

def normalize
  len = length
  return Vector2.zero if len.zero?
  self / len
end

#to_aObject



50
# File 'lib/sfml/system/vector2.rb', line 50

def to_a = [@x, @y]

#to_hObject



51
# File 'lib/sfml/system/vector2.rb', line 51

def to_h = { x: @x, y: @y }

#to_native_fObject

:nodoc:



62
63
64
# File 'lib/sfml/system/vector2.rb', line 62

def to_native_f # :nodoc:
  C::System::Vector2f.new.tap { |v| v[:x] = @x; v[:y] = @y }
end

#to_sObject Also known as: inspect



55
# File 'lib/sfml/system/vector2.rb', line 55

def to_s = "Vector2(#{@x}, #{@y})"