Class: SFML::Vector2
- Inherits:
-
Object
- Object
- SFML::Vector2
- 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
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #*(scalar) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(scalar) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
-
#coerce(other) ⇒ Object
Lets Ruby evaluate ‘2 * vec` as `vec * 2`.
- #cross(other) ⇒ Object
- #deconstruct ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #dot(other) ⇒ Object
- #hash ⇒ Object
-
#initialize(x = 0, y = 0) ⇒ Vector2
constructor
A new instance of Vector2.
- #length ⇒ Object
- #length_sq ⇒ Object
- #normalize ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
-
#to_native_f ⇒ Object
:nodoc:.
- #to_s ⇒ Object (also: #inspect)
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
#x ⇒ Object (readonly)
Returns the value of attribute x.
10 11 12 |
# File 'lib/sfml/system/vector2.rb', line 10 def x @x end |
#y ⇒ Object (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 |
.zero ⇒ Object
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) |
#/(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.
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) |
#deconstruct ⇒ Object
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) |
#hash ⇒ Object
36 |
# File 'lib/sfml/system/vector2.rb', line 36 def hash = [@x, @y].hash |
#length ⇒ Object
38 |
# File 'lib/sfml/system/vector2.rb', line 38 def length = Math.sqrt(length_sq) |
#length_sq ⇒ Object
39 |
# File 'lib/sfml/system/vector2.rb', line 39 def length_sq = (@x * @x) + (@y * @y) |
#normalize ⇒ Object
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_a ⇒ Object
50 |
# File 'lib/sfml/system/vector2.rb', line 50 def to_a = [@x, @y] |
#to_h ⇒ Object
51 |
# File 'lib/sfml/system/vector2.rb', line 51 def to_h = { x: @x, y: @y } |
#to_native_f ⇒ Object
: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_s ⇒ Object Also known as: inspect
55 |
# File 'lib/sfml/system/vector2.rb', line 55 def to_s = "Vector2(#{@x}, #{@y})" |