Class: Vector2
- Inherits:
-
Object
- Object
- Vector2
- Defined in:
- src/ruby/models/vector2.rb,
mrb_doc/models/vector2.rb
Overview
A class used to hold an x and y value, will be translated when viewed through a Camera2D object.
Constant Summary collapse
- ZERO =
A Vector2 setup at 0, 0.
Vector2.new(x: 0, y: 0)
Instance Attribute Summary collapse
- #x ⇒ Float (also: #width)
- #y ⇒ Float (also: #height)
Class Method Summary collapse
-
.[](x, y) ⇒ Vector2
A short form way to create new Vector2 objects.
-
.mock_return(x: 0, y: 0) ⇒ String
A method used to generate the mock data for Raylib.
Instance Method Summary collapse
-
#*(other) ⇒ Vector2
(also: #scale)
Scale the Vector2 by the scalar.
- #+(other) ⇒ Vector2
- #-(other) ⇒ Vector2 (also: #difference)
-
#/(other) ⇒ Vector2
Divide the Vector2 by the value.
-
#==(other) ⇒ Boolean
The equality operator is used for checking if two Vector2 objects share the same position.
-
#draw ⇒ nil
Sets the corresponding pixel to the passed in Colour.
-
#initialize(x:, y:) ⇒ Vector2
constructor
Creates a new instance of Vector2.
-
#inspect ⇒ String
Returns a string representation of the Vector2 that's useful for debugging.
-
#length ⇒ Numeric
Calculates the length of the Vector2.
- #overlaps?(other) ⇒ Boolean
-
#to_h ⇒ Hash
Return the object represented by a Hash.
Constructor Details
Instance Attribute Details
#x ⇒ Float Also known as: width
3 4 5 |
# File 'mrb_doc/models/vector2.rb', line 3 def x @x end |
#y ⇒ Float Also known as: height
3 4 5 |
# File 'mrb_doc/models/vector2.rb', line 3 def y @y end |
Class Method Details
.[](x, y) ⇒ Vector2
A short form way to create new Vector2 objects.
28 29 30 |
# File 'src/ruby/models/vector2.rb', line 28 def self.[](x, y) new(x: x, y: y) end |
.mock_return(x: 0, y: 0) ⇒ String
A method used to generate the mock data for Raylib.
259 260 261 |
# File 'src/ruby/models/vector2.rb', line 259 def self.mock_return(x: 0, y: 0) [x, y].map(&:to_s).join(" ") end |
Instance Method Details
#*(other) ⇒ Vector2 Also known as: scale
Scale the Vector2 by the scalar.
138 139 140 141 142 143 |
# File 'src/ruby/models/vector2.rb', line 138 def *(other) Vector2.new( x: x * other, y: y * other ) end |
#+(other) ⇒ Vector2
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'src/ruby/models/vector2.rb', line 68 def +(other) case other when Vector2 Vector2.new( x: x + other.x, y: y + other.y ) when Numeric Vector2.new( x: x + other, y: y + other ) else raise ArgumentError, "can only add Numeric and Vector2" end end |
#-(other) ⇒ Vector2 Also known as: difference
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'src/ruby/models/vector2.rb', line 106 def -(other) case other when Vector2 Vector2.new( x: x - other.x, y: y - other.y ) when Numeric Vector2.new( x: x - other, y: y - other ) else raise ArgumentError, "can only subtract Numeric and Vector2" end end |
#/(other) ⇒ Vector2
Divide the Vector2 by the value.
158 159 160 161 162 163 |
# File 'src/ruby/models/vector2.rb', line 158 def /(other) Vector2.new( x: x / other, y: y / other ) end |
#==(other) ⇒ Boolean
The equality operator is used for checking if two Vector2 objects share the same position.
42 43 44 45 46 47 |
# File 'src/ruby/models/vector2.rb', line 42 def ==(other) return super unless other.is_a?(Vector2) x == other.x && y == other.y end |
#draw ⇒ nil
Sets the corresponding pixel to the passed in Colour.
25 26 27 28 29 |
# File 'mrb_doc/models/vector2.rb', line 25 def draw # mrb_Vector2_draw # src/mruby_integration/models/vector2.cpp nil end |
#inspect ⇒ String
Returns a string representation of the Vector2 that's useful for debugging.
244 245 246 |
# File 'src/ruby/models/vector2.rb', line 244 def inspect "#<Vector2:0x#{object_id.to_s(16)} x:#{x} y:#{y}>" end |
#length ⇒ Numeric
Calculates the length of the Vector2.
173 174 175 |
# File 'src/ruby/models/vector2.rb', line 173 def length Math.sqrt(x**2 + y**2) end |
#overlaps?(other) ⇒ Boolean
228 229 230 231 232 |
# File 'src/ruby/models/vector2.rb', line 228 def overlaps?(other) raise ArgumentError, "Must respond to #overlaps?" unless other.respond_to?(:overlaps?) other.overlaps?(self) end |
#to_h ⇒ Hash
Return the object represented by a Hash.
190 191 192 193 194 195 |
# File 'src/ruby/models/vector2.rb', line 190 def to_h { x: x, y: y } end |