vector2d 2.3.0 → 3.0.0

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.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build.yml +1 -1
  3. data/.gitignore +3 -1
  4. data/.release-please-manifest.json +1 -1
  5. data/.rubocop.yml +1 -1
  6. data/.yardopts +11 -0
  7. data/CHANGELOG.md +216 -0
  8. data/Gemfile +5 -1
  9. data/Gemfile.lock +35 -5
  10. data/README.md +325 -12
  11. data/Rakefile +18 -0
  12. data/benchmark/vector_comparison.rb +129 -0
  13. data/lib/vector2d/angles.rb +315 -0
  14. data/lib/vector2d/arithmetic.rb +97 -0
  15. data/lib/vector2d/comparison.rb +188 -0
  16. data/lib/vector2d/componentwise.rb +239 -0
  17. data/lib/vector2d/constructors.rb +137 -0
  18. data/lib/vector2d/conversions.rb +139 -0
  19. data/lib/vector2d/coordinates.rb +22 -0
  20. data/lib/vector2d/deprecation.rb +16 -0
  21. data/lib/vector2d/dimensions.rb +297 -0
  22. data/lib/vector2d/interpolation.rb +191 -0
  23. data/lib/vector2d/lengths.rb +284 -0
  24. data/lib/vector2d/matrix_interop.rb +93 -0
  25. data/lib/vector2d/parsing.rb +142 -0
  26. data/lib/vector2d/projection.rb +208 -0
  27. data/lib/vector2d/version.rb +2 -1
  28. data/lib/vector2d.rb +172 -60
  29. data/spec/lib/vector2d/angles_spec.rb +441 -0
  30. data/spec/lib/vector2d/{calculations_spec.rb → arithmetic_spec.rb} +48 -58
  31. data/spec/lib/vector2d/comparison_spec.rb +358 -0
  32. data/spec/lib/vector2d/componentwise_spec.rb +300 -0
  33. data/spec/lib/vector2d/constructors_spec.rb +299 -0
  34. data/spec/lib/vector2d/conversions_spec.rb +234 -0
  35. data/spec/lib/vector2d/dimensions_spec.rb +820 -0
  36. data/spec/lib/vector2d/interpolation_spec.rb +322 -0
  37. data/spec/lib/vector2d/lengths_spec.rb +457 -0
  38. data/spec/lib/vector2d/matrix_interop_spec.rb +92 -0
  39. data/spec/lib/vector2d/parsing_spec.rb +329 -0
  40. data/spec/lib/vector2d/projection_spec.rb +306 -0
  41. data/spec/lib/vector2d_documentation_spec.rb +38 -0
  42. data/spec/lib/vector2d_immutability_spec.rb +263 -0
  43. data/spec/lib/vector2d_spec.rb +90 -50
  44. data/spec/lib/vector2d_subclassing_spec.rb +210 -0
  45. data/spec/lib/vector2d_tags_spec.rb +43 -0
  46. data/spec/spec_helper.rb +2 -0
  47. data/spec/support/doc_examples/comments.rb +65 -0
  48. data/spec/support/doc_examples/markdown.rb +50 -0
  49. data/spec/support/doc_examples.rb +94 -0
  50. data/spec/support/doc_tags.rb +151 -0
  51. data/spec/support/shared_examples/class_preserving_method.rb +10 -0
  52. data/spec/support/shared_examples/deprecated_method.rb +25 -0
  53. data/spec/support/shared_examples/parsed_vector.rb +11 -0
  54. data/vector2d.gemspec +2 -1
  55. metadata +42 -13
  56. data/.travis.yml +0 -10
  57. data/lib/vector2d/calculations.rb +0 -141
  58. data/lib/vector2d/coercions.rb +0 -64
  59. data/lib/vector2d/fitting.rb +0 -60
  60. data/lib/vector2d/properties.rb +0 -46
  61. data/lib/vector2d/transformations.rb +0 -98
  62. data/spec/lib/vector2d/coercions_spec.rb +0 -59
  63. data/spec/lib/vector2d/fitting_spec.rb +0 -70
  64. data/spec/lib/vector2d/properties_spec.rb +0 -47
  65. data/spec/lib/vector2d/transformations_spec.rb +0 -139
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f3b7e7c1ae4279b24950e17dab944782608209e7dbdc061a2382d840a34ff59
4
- data.tar.gz: 767737d5b485094560139148118512c9f7bd5e777f804b1d724f552212ccb485
3
+ metadata.gz: 02eaa1a646eca603d58bc80b84ad7c1d3f4973df2a9837caa722a6e706b8ad4b
4
+ data.tar.gz: 29b5dbc22b83bc53946c9da06caf119fa07d5e2c8cf39d940e7f751ec4426065
5
5
  SHA512:
6
- metadata.gz: a29a8e2e16357b5b0fea79db807c604502a6c097a835071776d15369f82ec2650b31722f2e7d9f25b38b4c929a3356f9fe5ad05236b825303bccfad724f524eb
7
- data.tar.gz: 56c976aa96b172db1a72fea707bd8ac5c7d103c8a25e970483e4d1298e0a4675965c381c5420e018edeaf9255d70e61bd41fa663e6f508ad163b385f29df0e68
6
+ metadata.gz: 206db7be3e3cd57909d8200f09504dc8c539ec1a6c1f4fa6e1015cd21496577445004c3cb319547d1bbdab3dd8bf25fb6fa94428bb564432c41d86a32016f234
7
+ data.tar.gz: 94213c539359dd0f7ccac221a5a3361838d84612115d20c7bdc91788a579cccbe397b8bc8d6147ab06420f72fbaa566f18f4e700654633b152833029ac9e73a9
@@ -29,7 +29,7 @@ jobs:
29
29
  runs-on: ubuntu-latest
30
30
  strategy:
31
31
  matrix:
32
- ruby: ["3.3", "3.4", "4.0"]
32
+ ruby: ["3.4", "4.0"]
33
33
  steps:
34
34
  - uses: actions/checkout@v6.1.0
35
35
  - uses: ruby/setup-ruby@v1.321.0
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
+ .yardoc
1
2
  coverage
2
- rdoc
3
+ doc
3
4
  pkg
5
+ rdoc
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "2.3.0"
2
+ ".": "3.0.0"
3
3
  }
data/.rubocop.yml CHANGED
@@ -4,7 +4,7 @@ plugins:
4
4
 
5
5
  AllCops:
6
6
  NewCops: enable
7
- TargetRubyVersion: 3.2
7
+ TargetRubyVersion: 3.4
8
8
 
9
9
  Style/StringLiterals:
10
10
  EnforcedStyle: double_quotes
data/.yardopts ADDED
@@ -0,0 +1,11 @@
1
+ --markup rdoc
2
+ --readme README.md
3
+ --title "Vector2d"
4
+ --output-dir doc
5
+ lib/vector2d.rb
6
+ lib/vector2d/**/*.rb
7
+ -
8
+ CHANGELOG.md
9
+ CONTRIBUTING.md
10
+ CODE_OF_CONDUCT.md
11
+ LICENSE
data/CHANGELOG.md CHANGED
@@ -1,5 +1,221 @@
1
1
  # Changelog
2
2
 
3
+ ## [3.0.0](https://github.com/elektronaut/vector2d/compare/vector2d/v2.3.0...vector2d/v3.0.0) (2026-09-20)
4
+
5
+ The public API grows from 43 methods to 115. Vectors are now genuinely
6
+ immutable and safe to share between ractors, they can be pattern matched,
7
+ and they convert to and from the standard library `Matrix` and `Vector`.
8
+
9
+ Nothing was removed. The seven deprecated methods still work, and stay
10
+ silent unless deprecation warnings are enabled.
11
+
12
+ Most of the breaking changes correct behavior that was undefined or plainly
13
+ wrong in 2.x: input that used to produce an unusable vector now raises where
14
+ the bad input is, rather than somewhere further along. Four changes can
15
+ affect code that works today, and they come first.
16
+
17
+ ### Breaking changes
18
+
19
+ #### Ruby 3.4 is now the minimum, up from 3.2
20
+
21
+ `MatrixInterop#to_matrix`, `#to_vector` and `#transform` are not
22
+ ractor-safe before 3.4. Ruby 3.2 raises `Ractor::IsolationError` from the
23
+ `require`, and 3.3 raises out of `::Vector[]` even with `matrix` already
24
+ loaded. The floor moves so the matrix paths are safe across the whole
25
+ supported range, and the gemspec stops claiming versions CI never ran.
26
+
27
+ #### Vectors are frozen
28
+
29
+ Instances are frozen on construction, which is what makes them shareable
30
+ between ractors when their coordinates are. `#dup` and `clone(freeze: false)`
31
+ return frozen objects, matching `Data`, `Rational` and `Complex`.
32
+
33
+ A subclass that adds instance variables must assign them before calling
34
+ `super`, as the object is frozen by the time `super` returns.
35
+
36
+ #### Strings keep their coordinate type
37
+
38
+ `Vector2d.parse("50x70")` returns `Vector2d(50,70)` with `Integer`
39
+ coordinates. It previously returned floats, which made strings the one
40
+ input type that did not preserve what it was given.
41
+
42
+ Parsing is stricter about malformed input and more permissive about valid
43
+ input. `"1.2.3x4"` was accepted and silently truncated, and now raises.
44
+ Negative coordinates, an uppercase `X` separator and comma-separated pairs
45
+ are now accepted.
46
+
47
+ #### Coordinates must be `Numeric`
48
+
49
+ Coordinates were never validated, so any unrecognized argument fell through
50
+ to the constructor. `Vector2d.parse(nil)` returned `Vector2d(nil,nil)` and
51
+ raised `NoMethodError` later at an unrelated call site.
52
+
53
+ Every parse path now checks both coordinates. `Integer`, `Float`, `Rational`,
54
+ `BigDecimal` and custom `Numeric` subclasses are unaffected. Objects that
55
+ are numeric by duck typing alone are no longer accepted.
56
+
57
+ #### Tightened validation
58
+
59
+ These raise where 2.x returned something unusable. Code that was working is
60
+ unlikely to notice.
61
+
62
+ - `Vector2d(2, 0).aspect_ratio` returned `Infinity` and the zero vector
63
+ returned `NaN`. Both now raise `ArgumentError`.
64
+ - `Complex` passed the coordinate check, and the resulting vector raised
65
+ `RangeError` out of `Math` from `#aspect_ratio` and `.from_angle`. It is
66
+ now rejected: `ArgumentError` from `.parse`, `.new` and `.from_angle`,
67
+ `TypeError` from the operators.
68
+ - `Vector2d.parse(5, nil)` read the explicit `nil` as an omitted argument
69
+ and returned `Vector2d(5,5)`. An explicit `nil` second argument now
70
+ raises `ArgumentError`.
71
+ - `Vector2d(2, 3).clamp_length(-1.0)` returned a reversed unit vector
72
+ rather than anything shorter. A negative maximum now raises
73
+ `ArgumentError`, following `Comparable#clamp`. This applies to
74
+ `#truncate` as well. Pass `max.clamp(0..)` to saturate instead.
75
+ `#resize` is unchanged, where a negative length is a signed magnitude
76
+ that reverses the vector.
77
+ - `#fit` and `#fit_either` returned early on the zero vector, before the
78
+ argument was coerced, so `Vector2d(0, 0).fit("garbage")` returned the
79
+ zero vector. Both now raise `ArgumentError`, as every other receiver
80
+ already did.
81
+
82
+ #### Subclasses
83
+
84
+ Two places where a subclass could be dropped on the way through:
85
+
86
+ - `#coerce` built the left-hand operand with `.parse`, so `2 * subvector`
87
+ returned a `Vector2d` where `subvector * 2` returned the subclass. It now
88
+ builds through `#build`, so coercion is symmetric. `#coerce_vector` is
89
+ unchanged, so methods that only read an operand's coordinates still parse
90
+ into the base class.
91
+ - `.parse` returned any `Vector2d` argument untouched, so `Sub.parse(vector)`
92
+ handed back the plain vector. An argument is returned as it is only when
93
+ it is already an instance of the class parsing it; anything else is
94
+ rebuilt through `.build`.
95
+
96
+ ### Deprecations
97
+
98
+ All of these still work. They warn under `Warning[:deprecated] = true` or
99
+ `ruby -w`, and each one names its replacement.
100
+
101
+ | Deprecated | Replacement |
102
+ | ------------------- | ------------------------------- |
103
+ | `#truncate` | `#limit_length` |
104
+ | `#squared_length` | `#length_squared` |
105
+ | `#squared_distance` | `#distance_squared` |
106
+ | `#fit_either` | `#cover` |
107
+ | `#constrain_both` | `#fit` |
108
+ | `#constrain_one` | `#cover` |
109
+ | `#contain` | `other.fit(self, upscale: false)` |
110
+
111
+ `#truncate` is the one worth looking at. It took a maximum length, where
112
+ `Numeric#truncate` takes a digit count, and the name went to the wrong
113
+ method. `#limit_length` replaces it.
114
+
115
+ ### Features
116
+
117
+ #### Construction
118
+
119
+ `.build` and `#build` are the hooks every returning method routes through,
120
+ so a subclass overrides construction in one place.
121
+
122
+ - `Vector2d.from_angle(angle, length = 1.0)` and `#to_polar`
123
+ - `Vector2d.from_degrees(angle, length = 1.0)`
124
+ - `Vector2d.random(length = 1.0, random: Random)`
125
+ - `Vector2d.zero`, `.one`, `.up`, `.down`, `.left` and `.right`
126
+
127
+ #### Angles and rotation
128
+
129
+ - `#angle_to` returns a signed angle in `-PI..PI`, positive when the other
130
+ vector is counterclockwise. `#angle_between` stays unsigned in `0..PI`.
131
+ - `Vector2d.degrees` and `.radians` convert between the two
132
+ - `#angle_in_degrees` and `#rotate_degrees`
133
+ - `#rotate_around(center, angle)`
134
+ - `#perpendicular_cw`, alongside the existing counterclockwise
135
+ `#perpendicular`
136
+
137
+ #### Length and distance
138
+
139
+ - `#length_squared` and `#distance_squared`, which skip the square root
140
+ - `#manhattan_distance` and `#chebyshev_distance`
141
+ - `#limit_length(max)` scales down only if longer
142
+ - `#clamp_length(min, max = nil)` takes a range or a pair, mirroring
143
+ `Comparable#clamp`
144
+ - `#direction_to`
145
+ - `#magnitude` and `#norm` as aliases of `#length`
146
+
147
+ #### Projection and reflection
148
+
149
+ - `#project`, `#reject` and `#scalar_projection`
150
+ - `#reflect(normal)` and `#refract(normal, refractive_index)`
151
+ - `#dot` and `#inner_product` as aliases of `#dot_product`
152
+
153
+ #### Interpolation
154
+
155
+ - `#lerp`, `#inverse_lerp` and `#slerp`
156
+ - `#midpoint`
157
+ - `#move_toward(target, distance)`
158
+
159
+ #### Comparison
160
+
161
+ - `#eql?` and `#hash`, so vectors work as hash keys
162
+ - `#approx_equal?(other, tolerance = nil)` and `#approx_zero?`
163
+ - `#zero?`, `#finite?` and `#nan?`
164
+ - `#parallel?`, `#perpendicular?`, `#opposite?` and `#independent?`
165
+
166
+ #### Component-wise operations
167
+
168
+ - Unary `+@` and `-@`
169
+ - `#abs`, `#sign` and `#snap(step)`
170
+ - `#with_x` and `#with_y`
171
+ - `#min` and `#max`
172
+ - `#floor` and `#ceil` take a digit count
173
+
174
+ #### Dimensions and fitting
175
+
176
+ - `#cover(other, upscale: true)` scales to the smallest size that fills the
177
+ box, the counterpart to `#fit`
178
+ - `#fit(other, upscale: false)` never scales up
179
+ - `#fits?` and `#covers?`
180
+ - `#area`, `#square?`, `#portrait?` and `#landscape?`
181
+
182
+ #### Standard library compatibility
183
+
184
+ - `#to_matrix`, `#to_vector` and `#transform(matrix)`
185
+ - `#deconstruct` and `#deconstruct_keys` for pattern matching
186
+
187
+ ### Bug fixes
188
+
189
+ - `#angle_between` is computed with `atan2` rather than
190
+ `Math.acos(dot_product)`, which raised `Math::DomainError` whenever float
191
+ normalization pushed the dot product past ±1.0. That happened for roughly
192
+ 15% of parallel and antiparallel pairs in a random probe. The zero vector
193
+ now gives `0.0` instead of `NaN`. The range is unchanged.
194
+ - The angle converters always return floats, so a `BigDecimal` angle no
195
+ longer leaks through.
196
+ - `#perpendicular` and `#rotate` preserve the receiver's class.
197
+ - `#inspect` renders the receiver's class.
198
+ - `#normalize` and `#resize` return the zero vector unchanged, where they
199
+ previously returned `NaN` coordinates.
200
+ - `#reflect` guards against a zero normal, returning the vector unchanged.
201
+ - `#project` returns a float zero when projecting onto the zero vector.
202
+ - `#lerp` requires a real number as the amount.
203
+ - `#fit` and `#fit_either` handle negative vectors and zero coordinates by
204
+ magnitude.
205
+ - `#==` returns false for non-vectors instead of raising.
206
+ - Scalar arguments are validated in the transformations.
207
+ - Internal files are loaded with `require_relative`.
208
+ - The gemspec has a `changelog_uri`.
209
+
210
+ ### Performance improvements
211
+
212
+ Roughly 1.6x faster than the standard library `Vector` on the common
213
+ operations.
214
+
215
+ - Operands that are already usable skip the parser
216
+ - Coercion skips the parser for vectors
217
+ - The common coordinate types are checked first
218
+
3
219
  ## [2.3.0](https://github.com/elektronaut/vector2d/compare/vector2d-v2.2.4...vector2d/v2.3.0) (2026-08-30)
4
220
 
5
221
 
data/Gemfile CHANGED
@@ -5,11 +5,15 @@ source "https://rubygems.org"
5
5
  gemspec
6
6
 
7
7
  group :development, :test do
8
+ gem "bigdecimal"
9
+ gem "irb"
10
+ gem "matrix"
8
11
  gem "rake"
9
12
  gem "rspec", "~> 3.8"
10
13
  gem "rspec-its", "~> 2.0"
11
14
  gem "rubocop", require: false
12
15
  gem "rubocop-rake", require: false
13
16
  gem "rubocop-rspec", require: false
14
- gem "simplecov", "~> 1.1.1"
17
+ gem "simplecov", "~> 1.2.0"
18
+ gem "yard", require: false
15
19
  end
data/Gemfile.lock CHANGED
@@ -1,25 +1,49 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- vector2d (2.3.0)
4
+ vector2d (3.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.3)
10
+ bigdecimal (4.1.3)
10
11
  diff-lcs (1.6.2)
12
+ erb (6.0.7)
13
+ io-console (0.9.4)
14
+ irb (1.18.0)
15
+ pp (>= 0.6.0)
16
+ prism (>= 1.3.0)
17
+ rdoc (>= 4.0.0)
18
+ reline (>= 0.4.2)
11
19
  json (2.21.2)
12
20
  language_server-protocol (3.17.0.6)
13
21
  lint_roller (1.1.0)
22
+ logger (1.7.0)
23
+ matrix (0.4.3)
14
24
  parallel (1.28.0)
15
25
  parser (3.3.12.0)
16
26
  ast (~> 2.4.1)
17
27
  racc
28
+ pp (0.6.4)
29
+ prettyprint
30
+ prettyprint (0.2.0)
18
31
  prism (1.9.0)
19
32
  racc (1.8.1)
20
33
  rainbow (3.1.1)
21
34
  rake (13.4.2)
35
+ rbs (4.2.0)
36
+ logger
37
+ prism (>= 1.6.0)
38
+ tsort
39
+ rdoc (8.0.0)
40
+ erb
41
+ prism (>= 1.6.0)
42
+ rbs (>= 4.0.0)
43
+ tsort
22
44
  regexp_parser (2.12.0)
45
+ reline (0.7.0)
46
+ io-console (~> 0.5)
23
47
  rspec (3.13.2)
24
48
  rspec-core (~> 3.13.0)
25
49
  rspec-expectations (~> 3.13.0)
@@ -36,8 +60,8 @@ GEM
36
60
  diff-lcs (>= 1.2.0, < 2.0)
37
61
  rspec-support (~> 3.13.0)
38
62
  rspec-support (3.13.7)
39
- rubocop (1.89.0)
40
- json (~> 2.3)
63
+ rubocop (1.90.0)
64
+ json (>= 2.3)
41
65
  language_server-protocol (~> 3.17.0.2)
42
66
  lint_roller (~> 1.1.0)
43
67
  parallel (>= 1.10)
@@ -58,24 +82,30 @@ GEM
58
82
  regexp_parser (>= 2.0)
59
83
  rubocop (~> 1.86, >= 1.86.2)
60
84
  ruby-progressbar (1.13.0)
61
- simplecov (1.1.1)
85
+ simplecov (1.2.0)
86
+ tsort (0.2.0)
62
87
  unicode-display_width (3.2.0)
63
88
  unicode-emoji (~> 4.1)
64
89
  unicode-emoji (4.2.0)
90
+ yard (0.9.45)
65
91
 
66
92
  PLATFORMS
67
93
  arm64-darwin-23
68
94
  ruby
69
95
 
70
96
  DEPENDENCIES
97
+ bigdecimal
98
+ irb
99
+ matrix
71
100
  rake
72
101
  rspec (~> 3.8)
73
102
  rspec-its (~> 2.0)
74
103
  rubocop
75
104
  rubocop-rake
76
105
  rubocop-rspec
77
- simplecov (~> 1.1.1)
106
+ simplecov (~> 1.2.0)
78
107
  vector2d!
108
+ yard
79
109
 
80
110
  BUNDLED WITH
81
111
  4.0.4