Class: Mittsu::Vector2
- Inherits:
-
Object
- Object
- Mittsu::Vector2
- Defined in:
- lib/mittsu/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
- #==(v) ⇒ Object
- #[](index) ⇒ Object
- #[]=(index, value) ⇒ Object
- #add(v) ⇒ Object
- #add_scalar(s) ⇒ Object
- #add_vectors(a, b) ⇒ Object
- #ceil ⇒ Object
- #clamp(min, max) ⇒ Object
- #clamp_scalar(min, max) ⇒ Object
- #clone ⇒ Object
- #copy(v) ⇒ Object
- #distance_to(v) ⇒ Object
- #distance_to_squared(v) ⇒ Object
- #divide(v) ⇒ Object
- #divide_scalar(s) ⇒ Object
- #dot(v) ⇒ Object
- #floor ⇒ Object
- #from_array(array, offset = 0) ⇒ Object
- #from_attribute(attribute, index, offset = 0) ⇒ Object
-
#initialize(x = 0, y = 0) ⇒ Vector2
constructor
A new instance of Vector2.
- #length ⇒ Object
- #length_sq ⇒ Object
- #lerp(v, alpha) ⇒ Object
- #lerp_vectors(v1, v2, alpha) ⇒ Object
- #max(v) ⇒ Object
- #min(v) ⇒ Object
- #multiply(v) ⇒ Object
- #multiply_scalar(s) ⇒ Object
- #negate ⇒ Object
- #normalize ⇒ Object
- #round ⇒ Object
- #round_to_zero ⇒ Object
- #set(x, y) ⇒ Object
- #set_length(l) ⇒ Object
- #sub(v) ⇒ Object
- #sub_scalar(s) ⇒ Object
- #sub_vectors(a, b) ⇒ Object
- #to_a ⇒ Object
- #to_array(array = [], offset = 0) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(x = 0, y = 0) ⇒ Vector2
Returns a new instance of Vector2.
7 8 9 10 |
# File 'lib/mittsu/math/vector2.rb', line 7 def initialize(x = 0, y = 0) @x = x.to_f @y = y.to_f end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
5 6 7 |
# File 'lib/mittsu/math/vector2.rb', line 5 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
5 6 7 |
# File 'lib/mittsu/math/vector2.rb', line 5 def y @y end |
Instance Method Details
#==(v) ⇒ Object
202 203 204 |
# File 'lib/mittsu/math/vector2.rb', line 202 def ==(v) v.x == @x && v.y == @y end |
#[](index) ⇒ Object
32 33 34 35 36 |
# File 'lib/mittsu/math/vector2.rb', line 32 def [](index) return @x if index == 0 return @y if index == 1 raise IndexError end |
#[]=(index, value) ⇒ Object
26 27 28 29 30 |
# File 'lib/mittsu/math/vector2.rb', line 26 def []=(index, value) return @x = value.to_f if index == 0 return @y = value.to_f if index == 1 raise IndexError end |
#add(v) ⇒ Object
44 45 46 47 48 |
# File 'lib/mittsu/math/vector2.rb', line 44 def add(v) @x += v.x @y += v.y self end |
#add_scalar(s) ⇒ Object
50 51 52 53 54 |
# File 'lib/mittsu/math/vector2.rb', line 50 def add_scalar(s) @x += s @y += s self end |
#add_vectors(a, b) ⇒ Object
56 57 58 59 60 |
# File 'lib/mittsu/math/vector2.rb', line 56 def add_vectors(a, b) @x = a.x + b.x @y = a.y + b.y self end |
#ceil ⇒ Object
135 136 137 138 139 |
# File 'lib/mittsu/math/vector2.rb', line 135 def ceil @x = @x.ceil.to_f @y = @y.ceil.to_f self end |
#clamp(min, max) ⇒ Object
116 117 118 119 120 |
# File 'lib/mittsu/math/vector2.rb', line 116 def clamp(min, max) @x = Math.clamp(@x, min.x, max.x) @y = Math.clamp(@y, min.y, max.y) self end |
#clamp_scalar(min, max) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/mittsu/math/vector2.rb', line 122 def clamp_scalar(min, max) min, max = min.to_f, max.to_f @x = Math.clamp(@x, min, max) @y = Math.clamp(@y, min, max) self end |
#clone ⇒ Object
230 231 232 |
# File 'lib/mittsu/math/vector2.rb', line 230 def clone Vector2.new(@x, @y) end |
#copy(v) ⇒ Object
38 39 40 41 42 |
# File 'lib/mittsu/math/vector2.rb', line 38 def copy(v) @x = v.x @y = v.y self end |
#distance_to(v) ⇒ Object
175 176 177 |
# File 'lib/mittsu/math/vector2.rb', line 175 def distance_to(v) Math.sqrt(distance_to_squared(v)) end |
#distance_to_squared(v) ⇒ Object
179 180 181 182 |
# File 'lib/mittsu/math/vector2.rb', line 179 def distance_to_squared(v) dx, dy = @x - v.x, @y - v.y dx * dx + dy * dy end |
#divide(v) ⇒ Object
92 93 94 95 96 |
# File 'lib/mittsu/math/vector2.rb', line 92 def divide(v) @x /= v.x @y /= v.y self end |
#divide_scalar(s) ⇒ Object
98 99 100 101 102 |
# File 'lib/mittsu/math/vector2.rb', line 98 def divide_scalar(s) @x /= s @y /= s self end |
#dot(v) ⇒ Object
159 160 161 |
# File 'lib/mittsu/math/vector2.rb', line 159 def dot(v) @x * v.x + @y * v.y end |
#floor ⇒ Object
129 130 131 132 133 |
# File 'lib/mittsu/math/vector2.rb', line 129 def floor @x = @x.floor.to_f @y = @y.floor.to_f self end |
#from_array(array, offset = 0) ⇒ Object
206 207 208 209 210 |
# File 'lib/mittsu/math/vector2.rb', line 206 def from_array(array, offset = 0) @x = array[offset] @y = array[offset + 1] self end |
#from_attribute(attribute, index, offset = 0) ⇒ Object
223 224 225 226 227 228 |
# File 'lib/mittsu/math/vector2.rb', line 223 def from_attribute(attribute, index, offset = 0) index = index * attribute.item_size + offset @x = attribute.array[index] @y = attribute.array[index + 1] self end |
#length ⇒ Object
163 164 165 |
# File 'lib/mittsu/math/vector2.rb', line 163 def length Math.sqrt(length_sq) end |
#length_sq ⇒ Object
167 168 169 |
# File 'lib/mittsu/math/vector2.rb', line 167 def length_sq self.dot(self) end |
#lerp(v, alpha) ⇒ Object
192 193 194 195 196 |
# File 'lib/mittsu/math/vector2.rb', line 192 def lerp(v, alpha) @x += (v.x - @x) * alpha @y += (v.y - @y) * alpha self end |
#lerp_vectors(v1, v2, alpha) ⇒ Object
198 199 200 |
# File 'lib/mittsu/math/vector2.rb', line 198 def lerp_vectors(v1, v2, alpha) self.sub_vectors(v2, v1).multiply_scalar(alpha).add(v1); end |
#max(v) ⇒ Object
110 111 112 113 114 |
# File 'lib/mittsu/math/vector2.rb', line 110 def max(v) @x = [@x, v.x].max @y = [@y, v.y].max self end |
#min(v) ⇒ Object
104 105 106 107 108 |
# File 'lib/mittsu/math/vector2.rb', line 104 def min(v) @x = [@x, v.x].min @y = [@y, v.y].min self end |
#multiply(v) ⇒ Object
80 81 82 83 84 |
# File 'lib/mittsu/math/vector2.rb', line 80 def multiply(v) @x *= v.x @y *= v.y self end |
#multiply_scalar(s) ⇒ Object
86 87 88 89 90 |
# File 'lib/mittsu/math/vector2.rb', line 86 def multiply_scalar(s) @x *= s @y *= s self end |
#negate ⇒ Object
153 154 155 156 157 |
# File 'lib/mittsu/math/vector2.rb', line 153 def negate @x = -@x @y = -@y self end |
#normalize ⇒ Object
171 172 173 |
# File 'lib/mittsu/math/vector2.rb', line 171 def normalize self.divide_scalar(self.length) end |
#round ⇒ Object
141 142 143 144 145 |
# File 'lib/mittsu/math/vector2.rb', line 141 def round @x = @x.round.to_f @y = @y.round.to_f self end |
#round_to_zero ⇒ Object
147 148 149 150 151 |
# File 'lib/mittsu/math/vector2.rb', line 147 def round_to_zero @x = ( @x < 0 ) ? @x.ceil.to_f : @x.floor.to_f @y = ( @y < 0 ) ? @y.ceil.to_f : @y.floor.to_f self end |
#set(x, y) ⇒ Object
20 21 22 23 24 |
# File 'lib/mittsu/math/vector2.rb', line 20 def set(x, y) @x = x.to_f @y = y.to_f self end |
#set_length(l) ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/mittsu/math/vector2.rb', line 184 def set_length(l) old_length = self.length if old_length != 0 && l != old_length self.multiply_scalar(l / old_length) end self end |
#sub(v) ⇒ Object
62 63 64 65 66 |
# File 'lib/mittsu/math/vector2.rb', line 62 def sub(v) @x -= v.x @y -= v.y self end |
#sub_scalar(s) ⇒ Object
68 69 70 71 72 |
# File 'lib/mittsu/math/vector2.rb', line 68 def sub_scalar(s) @x -= s @y -= s self end |
#sub_vectors(a, b) ⇒ Object
74 75 76 77 78 |
# File 'lib/mittsu/math/vector2.rb', line 74 def sub_vectors(a, b) @x = a.x - b.x @y = a.y - b.y self end |
#to_a ⇒ Object
219 220 221 |
# File 'lib/mittsu/math/vector2.rb', line 219 def to_a self.to_array end |
#to_array(array = [], offset = 0) ⇒ Object
212 213 214 215 216 217 |
# File 'lib/mittsu/math/vector2.rb', line 212 def to_array(array = [], offset = 0) array.tap { |a| a[offset] = @x; a[offset + 1] = @y; } end |
#to_s ⇒ Object
234 235 236 |
# File 'lib/mittsu/math/vector2.rb', line 234 def to_s "[#{x}, #{y}]" end |