math2d 1.4 → 1.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/math2d/utils2d.rb +131 -1
- data/lib/math2d/vector2d.rb +15 -5
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7fe67e25a8057e6e11bd7cd602d37c67eb7cab78ed029c83928638144d1d590d
|
4
|
+
data.tar.gz: 2bf6bf8012cba83df542190d30980234e70b74fe1bad00f46e1694a48a9b42fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d445a9bf01b379884e8b59508960b10ba51a82866ab8402c6dc44b44370532ff64e15cad3492afe279bdcfa3788c732f68ca5b5214d5356c716692197755369
|
7
|
+
data.tar.gz: 0e88cea36fa423ab36138ab6140d4ca2e87d9987f95898c5fe95667c2826077099bc0bbf28c4a3d003eed69e561ca7b47e6d297485e08d1ec8c4578e97f1ea1a
|
data/lib/math2d/utils2d.rb
CHANGED
@@ -48,7 +48,7 @@ module Math2D
|
|
48
48
|
end
|
49
49
|
|
50
50
|
# Calculates a number between two numbers at a specific increment.
|
51
|
-
# The amt parameter is the amount to interpolate between the two
|
51
|
+
# The +amt+ parameter is the amount to interpolate between the two
|
52
52
|
# values where 0.0 equal to the first point, 0.1 is very near the
|
53
53
|
# first point, 0.5 is half-way in between, and 1.0 is equal to the
|
54
54
|
# second point.
|
@@ -160,6 +160,136 @@ module Math2D
|
|
160
160
|
alias greyscale grayscale
|
161
161
|
end
|
162
162
|
|
163
|
+
# Interpolates two RGB (r,g,b,a) colors +a+ and +b+ by an amount +amt+.
|
164
|
+
#
|
165
|
+
# @param [Array<Numeric>] a
|
166
|
+
# @param [Array<Numeric>] b
|
167
|
+
# @param [Numeric] amt
|
168
|
+
# @return [Array<Float>]
|
169
|
+
def self.lerp_rgb(a, b, amt)
|
170
|
+
[
|
171
|
+
lerp(a[0], b[0], amt),
|
172
|
+
lerp(a[1], b[1], amt),
|
173
|
+
lerp(a[2], b[2], amt),
|
174
|
+
lerp(a[3], b[3], amt)
|
175
|
+
]
|
176
|
+
end
|
177
|
+
|
178
|
+
# Interpolates two RGB (r,g,b,a) colors +a+ and +b+ in the HSV color space by an amount +amt+.
|
179
|
+
# This method is preferred over +lerp_rgb+ when working with intensity graphs (i.e. temperature, probability density
|
180
|
+
# etc.)
|
181
|
+
#
|
182
|
+
# @note Both colors are converted internally to HSV and then converted back to RGB 0-1 at the end.
|
183
|
+
#
|
184
|
+
# @param [Array<Numeric>] a
|
185
|
+
# @param [Array<Numeric>] b
|
186
|
+
# @param [Numeric] amt
|
187
|
+
# @return [Array<Float>]
|
188
|
+
def self.lerp_hue(a, b, amt)
|
189
|
+
a = rgb_to_hsv(a)
|
190
|
+
b = rgb_to_hsv(b)
|
191
|
+
d = (b[0] - a[0]).abs
|
192
|
+
if a[0] > b[0]
|
193
|
+
a, b = b, a
|
194
|
+
amt = 1 - amt
|
195
|
+
end
|
196
|
+
if d > 0.5
|
197
|
+
a[0] += 1
|
198
|
+
h = (a[0] + amt * (b[0] - a[0])) % 1
|
199
|
+
else
|
200
|
+
h = a[0] + amt * d
|
201
|
+
end
|
202
|
+
hsv_to_rgb(
|
203
|
+
[
|
204
|
+
h,
|
205
|
+
lerp(a[1], b[1], amt),
|
206
|
+
lerp(a[2], b[2], amt),
|
207
|
+
lerp(a[3], b[3], amt)
|
208
|
+
]
|
209
|
+
)
|
210
|
+
end
|
211
|
+
|
212
|
+
# Converts an RGB color array (r,g,b,a) +color+ to the HSV (h,s,v,a) color space.
|
213
|
+
#
|
214
|
+
# @note This method assumes that both the RGB and HSV color arrays are in Linear RGB/HSV (also called RBG/HSV 0-1),
|
215
|
+
# where each component ranges from 0.0 to 1.0.
|
216
|
+
#
|
217
|
+
# @param [Array<Numeric>]
|
218
|
+
# @return [Array<Float>]
|
219
|
+
def self.rgb_to_hsv(color)
|
220
|
+
r = color[0]
|
221
|
+
g = color[1]
|
222
|
+
b = color[2]
|
223
|
+
a = color[3]
|
224
|
+
max = [r, g, b].max
|
225
|
+
min = [r, g, b].min
|
226
|
+
v = max
|
227
|
+
d = max - min
|
228
|
+
s = max.zero? ? 0 : d / max
|
229
|
+
if max == min
|
230
|
+
h = 0
|
231
|
+
else
|
232
|
+
case max
|
233
|
+
when r
|
234
|
+
h = (g - b) / d + (g < b ? 6 : 0)
|
235
|
+
when g
|
236
|
+
h = (b - r) / d + 2
|
237
|
+
when b
|
238
|
+
h = (r - g) / d + 4
|
239
|
+
end
|
240
|
+
h /= 6
|
241
|
+
end
|
242
|
+
[h, s, v, a]
|
243
|
+
end
|
244
|
+
|
245
|
+
# Converts an HSV color array (h,s,v,a) +color+ to the RGB (r,g,b,a) color space.
|
246
|
+
#
|
247
|
+
# @note This method assumes that both the RGB and HSV color arrays are in Linear RGB/HSV (also called RBG/HSV 0-1),
|
248
|
+
# where each component ranges from 0.0 to 1.0.
|
249
|
+
#
|
250
|
+
# @param [Array<Numeric>]
|
251
|
+
# @return [Array<Float>]
|
252
|
+
def self.hsv_to_rgb(color)
|
253
|
+
h = color[0]
|
254
|
+
s = color[1]
|
255
|
+
v = color[2]
|
256
|
+
a = color[3]
|
257
|
+
i = (h * 6).floor
|
258
|
+
f = h * 6 - i
|
259
|
+
pp = v * (1 - s)
|
260
|
+
q = v * (1 - f * s)
|
261
|
+
t = v * (1 - (1 - f) * s)
|
262
|
+
|
263
|
+
case i % 6
|
264
|
+
when 0
|
265
|
+
r = v
|
266
|
+
g = t
|
267
|
+
b = pp
|
268
|
+
when 1
|
269
|
+
r = q
|
270
|
+
g = v
|
271
|
+
b = pp
|
272
|
+
when 2
|
273
|
+
r = pp
|
274
|
+
g = v
|
275
|
+
b = t
|
276
|
+
when 3
|
277
|
+
r = pp
|
278
|
+
g = q
|
279
|
+
b = v
|
280
|
+
when 4
|
281
|
+
r = t
|
282
|
+
g = pp
|
283
|
+
b = v
|
284
|
+
when 5
|
285
|
+
r = v
|
286
|
+
g = pp
|
287
|
+
b = q
|
288
|
+
end
|
289
|
+
|
290
|
+
[r, g, b, a]
|
291
|
+
end
|
292
|
+
|
163
293
|
private
|
164
294
|
|
165
295
|
# @private
|
data/lib/math2d/vector2d.rb
CHANGED
@@ -269,22 +269,30 @@ module Math2D
|
|
269
269
|
def set_magnitude(new_mag)
|
270
270
|
mag = magnitude
|
271
271
|
mag = Float::INFINITY if mag.zero?
|
272
|
-
|
272
|
+
times!(new_mag / mag)
|
273
273
|
end
|
274
274
|
|
275
275
|
alias magnitude! set_magnitude
|
276
276
|
|
277
|
-
# Normalizes +self+ (set the magnitude to 1).
|
277
|
+
# Normalizes +self+ (set the magnitude to 1) and returns a new vector.
|
278
278
|
# +unit+ is an alias for this method.
|
279
279
|
#
|
280
|
-
# @return [Vector2D]
|
280
|
+
# @return [Vector2D] new vector
|
281
281
|
def normalize
|
282
|
+
clone.set_magnitude(1)
|
283
|
+
end
|
284
|
+
|
285
|
+
alias unit normalize
|
286
|
+
|
287
|
+
# Normalizes +self+ (set the magnitude to 1) *in place*.
|
288
|
+
# +unit!+ is an alias for this method.
|
289
|
+
#
|
290
|
+
# @return [Vector2D] modified self
|
291
|
+
def normalize!
|
282
292
|
set_magnitude(1)
|
283
293
|
end
|
284
294
|
|
285
|
-
alias normalize! normalize
|
286
295
|
alias unit! normalize!
|
287
|
-
alias unit normalize!
|
288
296
|
|
289
297
|
# Returns true if the magnitude of +self+ is equal to 1, false otherwise.
|
290
298
|
# +unit?+ is an alias for this method.
|
@@ -354,6 +362,7 @@ module Math2D
|
|
354
362
|
cos_ang = Math.cos(angle)
|
355
363
|
@x = @x * cos_ang - @y * sin_ang
|
356
364
|
@y = @x * sin_ang + @y * cos_ang
|
365
|
+
self
|
357
366
|
end
|
358
367
|
|
359
368
|
# Clockwise rotates +self+ +angle+ radians around a +pivot+ point and returns it as a new Vector2D.
|
@@ -379,6 +388,7 @@ module Math2D
|
|
379
388
|
cos_ang = Math.cos(angle)
|
380
389
|
@x = pivot_x + (dx * cos_ang) - (dy * sin_ang)
|
381
390
|
@y = pivot_y + (dx * sin_ang) + (dy * cos_ang)
|
391
|
+
self
|
382
392
|
end
|
383
393
|
|
384
394
|
# Linear interpolate +self+ and +other+ with an amount +amt+.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: math2d
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ualace Henrique
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
80
|
- !ruby/object:Gem::Version
|
81
81
|
version: '0'
|
82
82
|
requirements: []
|
83
|
-
rubygems_version: 3.3.
|
83
|
+
rubygems_version: 3.3.23
|
84
84
|
signing_key:
|
85
85
|
specification_version: 4
|
86
86
|
summary: Math2D
|