math2d 1.2.1 → 1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fa9e7785755d0911beda7ece84a14f4b0234d9103ae593b556237be3403973e
4
- data.tar.gz: ba748b29f8499d5ef2504942b4ee7c7dc3c9bad9d6fb5393f9aa78fd6d303cab
3
+ metadata.gz: 7d41532de5491da28b9de631463ae49ab77b12bd17ecac7f7efacf28e508a5d9
4
+ data.tar.gz: 763fe0ac3840a0cb9b241514c8baac852a9388059428e89fce516116ab659b02
5
5
  SHA512:
6
- metadata.gz: c494f9101b180ec9d2072630d48bbb9e0b99b404dec093ccaf35b4856cd75a8a11d4eed8478449a51f5bc3a37a239d37ab6899c35c558b24ff439490a64b88ff
7
- data.tar.gz: 4f79cd577b4c2686e07fe8b36b30cf28f8b1e4aac13e48e51df3d9e0a64b80454ca10076dbe06734d36b511cacb044a0101a5e614cebb766bb5a7dce880df84d
6
+ metadata.gz: ea01a56166ba411a2d015d67fb45224f072dfc6a2d57e00683ebc40216aa3dfae88e9b7a4a7cc057beae2eed42dd6a97eb5fc7553ed51c4593564bdc92997d86
7
+ data.tar.gz: c911d1c8bdc54a010588c07294601b1e9e5549bd35c8a76bc8e461683bdb09e37a01bf5d2fc98867e89795b45c420bb42f426b18e9b4c94cf39151fd2947f824
@@ -13,12 +13,19 @@ module Math2D
13
13
  # Twice the mathematical constant PI, also called TAU.
14
14
  TWO_PI = TAU = Math::PI * 2
15
15
 
16
+ # Multiplication constant to convert a value in degrees to radians
17
+ # @note Can be used as a substitute to +Utils2D.to_deg+.
18
+ DEG2RAD = Math::PI / 180
19
+ # Multiplication constant to convert a value in degrees to radians
20
+ # @note Can be used as a substitute to +Utils2D.to_rad+.
21
+ RAD2DEG = 180 / Math::PI
22
+
16
23
  # Returns +angle+ radians in degrees.
17
24
  #
18
25
  # @param [Numeric] angle
19
26
  # @return [Float]
20
27
  def self.to_deg(angle)
21
- (180 * angle) / Math::PI
28
+ angle * DEG2RAD
22
29
  end
23
30
 
24
31
  # Returns +angle+ degrees in radians.
@@ -26,7 +33,7 @@ module Math2D
26
33
  # @param [Numeric] angle
27
34
  # @return [Float]
28
35
  def self.to_rad(angle)
29
- (Math::PI * angle) / 180
36
+ angle * RAD2DEG
30
37
  end
31
38
 
32
39
  # Returns the distance between two cartesian points.
@@ -48,11 +55,23 @@ module Math2D
48
55
  #
49
56
  # @param [Numeric] a
50
57
  # @param [Numeric] b
58
+ # @param [Numeric] amt
51
59
  # @return [Float]
52
60
  def self.lerp(a, b, amt)
53
61
  (b - a) * (3.0 - amt * 2.0) * amt * amt + a
54
62
  end
55
63
 
64
+ # Being the inverse of +#lerp+, it calculates the interpolation parameter t
65
+ # of the Lerp method given a range [+a+, +b+] and a interpolant value of +value+.
66
+ #
67
+ # @param [Numeric] a
68
+ # @param [Numeric] b
69
+ # @param [Numeric] value
70
+ # @return [Float]
71
+ def self.inverse_lerp(a, b, value)
72
+ (value - a) / (b - a).to_f
73
+ end
74
+
56
75
  # Re-maps a number from one range (+a1+..+a2+) to another (+b1+..+b2+).
57
76
  #
58
77
  # @param [Numeric] value
@@ -127,6 +127,10 @@ module Math2D
127
127
  Vector2D.new(@x / other, @y / other)
128
128
  end
129
129
 
130
+ def ==(other)
131
+ (@x == other.x) && (@y == other.y)
132
+ end
133
+
130
134
  # Calculates the dot product between +self+ and +other+, where:
131
135
  # A.B (A dot B) = (Ax * Bx) + (Ay * By)
132
136
  #
@@ -136,10 +140,12 @@ module Math2D
136
140
  (@x * other.x) + (@y * other.y)
137
141
  end
138
142
 
139
- # Calculates the cross product between +self+ and +other+, where:
140
- # AxB (A cross B) = (Ax * By) - (Bx * Ay)
141
- # HOWEVER, the cross product is NOT defined in a 2D space, so the operation
142
- # simply returns the magnitude of the resulting cross-product 3D vector.
143
+ # Calculates the "cross product" (see note) between +self+ and +other+, where:
144
+ # A^B (A wedge B) = (Ax * By) - (Bx * Ay)
145
+ #
146
+ # @note Strictly speaking, the cross product is not defined in a 2-dimensional space,
147
+ # instead what is being calculated here is called a `wedge product`, which is defined
148
+ # in any space of dimension greater than 1.
143
149
  #
144
150
  # @param [Vector2D] other
145
151
  # @return [Numeric]
@@ -147,6 +153,8 @@ module Math2D
147
153
  (@x * other.y) - (other.x * @y)
148
154
  end
149
155
 
156
+ alias wedge cross
157
+
150
158
  # Returns the magnitude squared of +self+.
151
159
  #
152
160
  # @return [Numeric]
@@ -161,6 +169,8 @@ module Math2D
161
169
  Math.sqrt(@x**2 + @y**2)
162
170
  end
163
171
 
172
+ alias length magnitude
173
+
164
174
  # Returns the Euclidean distance between +self+ and +other+.
165
175
  #
166
176
  # @param [Vector2D] other
@@ -273,6 +283,14 @@ module Math2D
273
283
  Math.acos((@x * other.x + @y * other.y) / (magnitude * other.magnitude))
274
284
  end
275
285
 
286
+ # Checks if +self+ is facing the opposite direction of +other+.
287
+ #
288
+ # @param [Vector2D] other
289
+ # @return [Boolean]
290
+ def opposite?(other)
291
+ dot(other) < 0
292
+ end
293
+
276
294
  # Clockwise rotates +self+ +angle+ radians and returns it as a new Vector2D.
277
295
  #
278
296
  # @param [Numeric] angle
@@ -305,6 +323,15 @@ module Math2D
305
323
  self + (other - self) * amt
306
324
  end
307
325
 
326
+ # Calculates the parameter t of the +#lerp+ method between +self+ and +other+ given an interpolant +value+.
327
+ #
328
+ # @param [Numeric, Vector2D] other
329
+ # @param [Vector2D] value
330
+ # @return [Vector2D]
331
+ def inverse_lerp(other, value)
332
+ (value - self) / (other - self)
333
+ end
334
+
308
335
  # Reflects +self+ and returns it as a new Vector2D.
309
336
  # +other+ is the normal of the plane where +self+ is reflected.
310
337
  #
@@ -318,6 +345,24 @@ module Math2D
318
345
  Vector2D.new(x, y)
319
346
  end
320
347
 
348
+ # Refracts +self+ and returns it as a new Vector2D.
349
+ # +other+ is the normal of the plane where +self+ is refracted.
350
+ #
351
+ # @see https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.1.20.pdf GLS Language Specification (page 66)
352
+ #
353
+ # @param [Vector2D] other
354
+ # @param [Numeric] refractive_index
355
+ # @return [Vector2D]
356
+ def refract(other, refractive_index)
357
+ dot_prod = other.dot(self)
358
+ k = 1.0 - refractive_index * refractive_index * (1.0 - dot_prod * dot_prod)
359
+ return Vector2D.zero if k.negative?
360
+
361
+ x = refractive_index * @x - (refractive_index * dot_prod * Math.sqrt(k)) * other.x
362
+ y = refractive_index * @y - (refractive_index * dot_prod * Math.sqrt(k)) * other.y
363
+ Vector2D.new(x, y)
364
+ end
365
+
321
366
  # Returns a new Vector2D with random components but magnitude equal to 1.
322
367
  #
323
368
  # @return [Vector2D]
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: 1.2.1
4
+ version: '1.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ualace Henrique
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-25 00:00:00.000000000 Z
11
+ date: 2022-02-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A collection of useful Mathematical and Vector tools in 2D space
14
14
  email:
@@ -38,7 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
40
  requirements: []
41
- rubygems_version: 3.2.16
41
+ rubygems_version: 3.3.3
42
42
  signing_key:
43
43
  specification_version: 4
44
44
  summary: Math2D