easy_geometry 0.1.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.
@@ -0,0 +1,224 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe EasyGeometry::D2::Triangle do
4
+ let(:l1) { EasyGeometry::D2::Line.new([0, 0], [1, 1]) }
5
+ let(:t1) { described_class.new([0, 0], [4, 4], [0, 4]) }
6
+ let(:t2) { described_class.new([0, 0], [0, 1], [1, 1]) }
7
+ let(:t3) { described_class.new([0, 0], [1/2r, Math.sqrt(3)/2], [1, 0]) }
8
+ let(:t4) { described_class.new([0, 0], [5, 0], [5/2r, Math.sqrt(75/4r)]) }
9
+
10
+ describe '.new' do
11
+ it 'should raise type error if incorrect parameters' do
12
+ expect { described_class.new("1", 0) }.to raise_error(TypeError)
13
+ expect { described_class.new(nil, 0) }.to raise_error(TypeError)
14
+ expect { described_class.new([], 0) }.to raise_error(TypeError)
15
+ expect { described_class.new(l1) }.to raise_error(TypeError)
16
+ end
17
+
18
+ it 'should raise argument error if incorrect number of vertices' do
19
+ expect { described_class.new([0, 0], [0, 1]) }.to raise_error(ArgumentError)
20
+ expect { described_class.new([0, 0], [0, 1], [0, 1]) }.to raise_error(ArgumentError)
21
+ expect { described_class.new([0, 0], [0, 1], [1, 1], [2, 2]) }.to raise_error(ArgumentError)
22
+ end
23
+
24
+ it 'should remove consecutive duplicates' do
25
+ expect(described_class.new([0, 0], [0, 1], [0, 1], [0, 1], [0, 1], [1, 1], [1, 1]) == t2).to be true
26
+ end
27
+ end
28
+
29
+ describe '#is_similar?' do
30
+ it 'should return false if not triangle' do
31
+ expect(t1.is_similar?(l1)).to be false
32
+ expect(t1.is_similar?(5)).to be false
33
+ expect(t1.is_similar?("qwe")).to be false
34
+ end
35
+
36
+ it 'should return false if not similar' do
37
+ expect(t1.is_similar?(described_class.new([0, 0], [15, 9], [15, 1]))).to be false
38
+ expect(t1.is_similar?(described_class.new([100, 20], [37, 16], [15, 1]))).to be false
39
+ expect(t1.is_similar?(described_class.new([0, 0], [4, 4], [0.0000001, 4]))).to be false
40
+ end
41
+
42
+ it 'should return true if similar' do
43
+ expect(t1.is_similar?(t2)).to be true
44
+ expect(t1.is_similar?(t1)).to be true
45
+ expect(t2.is_similar?(t2)).to be true
46
+ expect(t2.is_similar?(described_class.new([0, 0], [0, 100], [100, 100]))).to be true
47
+ expect(t2.is_similar?(described_class.new([1, 1], [1, 2], [2, 2]))).to be true
48
+ end
49
+ end
50
+
51
+ describe '#is_equilateral?' do
52
+ it 'should return false if not equilateral' do
53
+ expect(t1.is_equilateral?).to be false
54
+ expect(t2.is_equilateral?).to be false
55
+ end
56
+
57
+ it 'should return true if equilateral' do
58
+ expect(t3.is_equilateral?).to be true
59
+ expect(t4.is_equilateral?).to be true
60
+ end
61
+ end
62
+
63
+ describe '#is_isosceles?' do
64
+ it 'should return false if not isosceles' do
65
+ expect(described_class.new([0, 0], [4, 0], [15, 113]).is_equilateral?).to be false
66
+ end
67
+
68
+ it 'should return true if isosceles' do
69
+ expect(t1.is_isosceles?).to be true
70
+ expect(t2.is_isosceles?).to be true
71
+ expect(t3.is_isosceles?).to be true
72
+ end
73
+ end
74
+
75
+ describe '#is_scalene?' do
76
+ it 'should return false if not scalene' do
77
+ expect(t1.is_scalene?).to be false
78
+ expect(t2.is_scalene?).to be false
79
+ expect(t3.is_scalene?).to be false
80
+ end
81
+
82
+ it 'should return true if scalene' do
83
+ expect(described_class.new([0, 0], [4, 0], [15, 113]).is_scalene?).to be true
84
+ end
85
+ end
86
+
87
+ describe '#is_right?' do
88
+ it 'should return false if not right-angled' do
89
+ expect(t3.is_right?).to be false
90
+ expect(t4.is_right?).to be false
91
+ expect(described_class.new([0, 0], [4, 0], [15, 113]).is_right?).to be false
92
+ end
93
+
94
+ it 'should return true if right-angled' do
95
+ expect(t1.is_right?).to be true
96
+ expect(t2.is_right?).to be true
97
+ end
98
+ end
99
+
100
+ describe '#altitudes' do
101
+ let(:p1) { EasyGeometry::D2::Point.new(0, 0) }
102
+ let(:p2) { EasyGeometry::D2::Point.new(5, 0) }
103
+ let(:p3) { EasyGeometry::D2::Point.new(0, 5) }
104
+ let(:triangle) { described_class.new(p1, p2, p3) }
105
+
106
+ it 'should return altitudes' do
107
+ altitudes = triangle.altitudes
108
+ expect(altitudes[p1]).to eq(EasyGeometry::D2::Segment.new(p1, EasyGeometry::D2::Point.new(Rational(5, 2), Rational(5, 2))))
109
+ expect(altitudes[p2]).to eq(triangle.sides[0])
110
+ expect(altitudes[p3]).to eq(triangle.sides[2])
111
+ end
112
+ end
113
+
114
+ describe '#orthocenter' do
115
+ let(:p1) { EasyGeometry::D2::Point.new(0, 0) }
116
+ let(:p2) { EasyGeometry::D2::Point.new(5, 0) }
117
+ let(:p3) { EasyGeometry::D2::Point.new(0, 5) }
118
+ let(:triangle) { described_class.new(p1, p2, p3) }
119
+
120
+ it 'should return orthocenter' do
121
+ expect(triangle.orthocenter).to eq(p1)
122
+ end
123
+ end
124
+
125
+ describe '#circumcenter' do
126
+ it 'should return circumcenter' do
127
+ expect(described_class.new([0, 0], [1, 0], [0, 1]).circumcenter).to eq(EasyGeometry::D2::Point.new(0.5, 0.5))
128
+ end
129
+ end
130
+
131
+ describe '#circumradius' do
132
+ it 'should return circumradius' do
133
+ expect(described_class.new([0, 0], [2, 0], [0, 2]).circumradius).to eq(Math.sqrt(2))
134
+ end
135
+ end
136
+
137
+ describe '#bisectors' do
138
+ let(:p1) { EasyGeometry::D2::Point.new(0, 0) }
139
+ let(:p2) { EasyGeometry::D2::Point.new(5, 0) }
140
+ let(:p3) { EasyGeometry::D2::Point.new(0, 5) }
141
+ let(:triangle1) { described_class.new(p1, p2, p3) }
142
+
143
+ let(:p4) { EasyGeometry::D2::Point.new(0, 0) }
144
+ let(:p5) { EasyGeometry::D2::Point.new(1, 0) }
145
+ let(:p6) { EasyGeometry::D2::Point.new(0, 1) }
146
+ let(:triangle2) { described_class.new(p4, p5, p6) }
147
+
148
+ it 'should return bisectors' do
149
+ bisectors1 = triangle1.bisectors
150
+ bisectors2 = triangle2.bisectors
151
+
152
+ expect(bisectors1[p1]).to eq(EasyGeometry::D2::Segment.new(p1, EasyGeometry::D2::Point.new(Rational(5, 2), Rational(5, 2))))
153
+ expect(bisectors2[p5]).to eq(EasyGeometry::D2::Segment.new(p5, EasyGeometry::D2::Point.new(0, Math.sqrt(2) - 1)))
154
+ end
155
+ end
156
+
157
+ describe '#incenter' do
158
+ let(:p1) { EasyGeometry::D2::Point.new(0, 0) }
159
+ let(:p2) { EasyGeometry::D2::Point.new(1, 0) }
160
+ let(:p3) { EasyGeometry::D2::Point.new(0, 1) }
161
+ let(:triangle) { described_class.new(p1, p2, p3) }
162
+
163
+ it 'should return incenter' do
164
+ expect(triangle.incenter).to eq(EasyGeometry::D2::Point.new(1 - Math.sqrt(2)/2, 1 - Math.sqrt(2)/2))
165
+ end
166
+ end
167
+
168
+ describe '#inradius' do
169
+ it 'should return inradius' do
170
+ expect(described_class.new([0, 0], [4, 0], [0, 3]).inradius).to eq(1)
171
+ expect(described_class.new([0, 0], [5, 0], [0, 5]).inradius).to eq(5 - 5 * Math.sqrt(2)/2)
172
+ end
173
+ end
174
+
175
+ describe '#exradii' do
176
+ let(:triangle1) { described_class.new([0, 0], [5, 0], [0, 5]) }
177
+ let(:triangle2) { described_class.new([0, 0], [6, 0], [0, 2]) }
178
+
179
+ it 'should return exradii' do
180
+ expect((triangle1.exradii[triangle1.sides[2]]).round(14)).to eq((5 * Math.sqrt(2)/2).round(14))
181
+ expect((triangle2.exradii[triangle2.sides[2]]).round(14)).to eq((-2 + Math.sqrt(10)).round(14))
182
+ end
183
+ end
184
+
185
+ describe '#medians' do
186
+ let(:p1) { EasyGeometry::D2::Point.new(0, 0) }
187
+ let(:p2) { EasyGeometry::D2::Point.new(1, 0) }
188
+ let(:p3) { EasyGeometry::D2::Point.new(0, 1) }
189
+ let(:triangle) { described_class.new(p1, p2, p3) }
190
+
191
+ it 'should return medians' do
192
+ expect((triangle.medians[p1])).to eq(EasyGeometry::D2::Segment.new(p1, EasyGeometry::D2::Point.new(0.5, 0.5)))
193
+ end
194
+ end
195
+
196
+ describe '#medial' do
197
+ let(:p1) { EasyGeometry::D2::Point.new(0, 0) }
198
+ let(:p2) { EasyGeometry::D2::Point.new(1, 0) }
199
+ let(:p3) { EasyGeometry::D2::Point.new(0, 1) }
200
+ let(:triangle) { described_class.new(p1, p2, p3) }
201
+
202
+ it 'should return medial' do
203
+ expect((triangle.medial)).to eq(described_class.new(
204
+ EasyGeometry::D2::Point.new(0.5, 0),
205
+ EasyGeometry::D2::Point.new(0.5, 0.5),
206
+ EasyGeometry::D2::Point.new(0, 0.5),
207
+ ))
208
+ end
209
+ end
210
+
211
+ describe '#eulerline' do
212
+ let(:p1) { EasyGeometry::D2::Point.new(0, 0) }
213
+ let(:p2) { EasyGeometry::D2::Point.new(1, 0) }
214
+ let(:p3) { EasyGeometry::D2::Point.new(0, 1) }
215
+ let(:triangle) { described_class.new(p1, p2, p3) }
216
+
217
+ it 'should return eulerline' do
218
+ expect((triangle.eulerline)).to eq(EasyGeometry::D2::Line.new(
219
+ EasyGeometry::D2::Point.new(0, 0),
220
+ EasyGeometry::D2::Point.new(0.5, 0.5),
221
+ ))
222
+ end
223
+ end
224
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe EasyGeometry::D2::Vector do
4
+ let(:p1) { described_class.new(0, 0) }
5
+ let(:p2) { described_class.new(1, 1) }
6
+ let(:p3) { described_class.new(3, 0) }
7
+ let(:p4) { described_class.new(2, 2) }
8
+ let(:l1) { EasyGeometry::D2::Line.new(p1, p2) }
9
+
10
+ describe '.new' do
11
+ it 'should raise error if incorrect parameters' do
12
+ expect { described_class.new("1", 0) }.to raise_error(TypeError)
13
+ expect { described_class.new(nil, 0) }.to raise_error(TypeError)
14
+ expect { described_class.new({}, 0) }.to raise_error(TypeError)
15
+ end
16
+
17
+ it 'should convert coords to big decimal' do
18
+ expect(p2.x).to be_kind_of(Rational)
19
+ expect(p2.y).to be_kind_of(Rational)
20
+ end
21
+ end
22
+
23
+ describe '#==' do
24
+ it 'should return true' do
25
+ expect(p1 == p1).to be true
26
+ expect(p2 == p2).to be true
27
+ expect(p3 == p3).to be true
28
+ expect(p4 == p4).to be true
29
+ end
30
+
31
+ it 'should return false' do
32
+ expect(p1 == p2).to be false
33
+ expect(p2 == p3).to be false
34
+ expect(p3 == p4).to be false
35
+ expect(p1 == 2).to be false
36
+ expect(p1 == '').to be false
37
+ expect(p1 == {}).to be false
38
+ end
39
+ end
40
+
41
+ describe '#-' do
42
+ it 'should raise error if incorrect parameters' do
43
+ expect { p1 - 1 }.to raise_error(TypeError)
44
+ expect { p2 - '' }.to raise_error(TypeError)
45
+ expect { p2 - l1 }.to raise_error(TypeError)
46
+ end
47
+
48
+ it 'should be equal' do
49
+ expect(p1 - p2).to eq(described_class.new(-1, -1))
50
+ expect(p3 - p2).to eq(described_class.new(2, -1))
51
+ end
52
+ end
53
+
54
+ describe '#orthogonal_direction' do
55
+ it 'should return orthogonal vector' do
56
+ expect(p1.orthogonal_direction).to eq(described_class.new(1, 0))
57
+ expect(p2.orthogonal_direction).to eq(described_class.new(-1, 1))
58
+ end
59
+ end
60
+
61
+ describe '#cross_product' do
62
+ it 'should raise error if incorrect parameters' do
63
+ expect { p1.cross_product(1) }.to raise_error(TypeError)
64
+ expect { p1.cross_product('') }.to raise_error(TypeError)
65
+ expect { p1.cross_product(nil) }.to raise_error(TypeError)
66
+ expect { p1.cross_product(l1) }.to raise_error(TypeError)
67
+ end
68
+
69
+ it 'should be equal' do
70
+ expect(p1.cross_product(p2)).to eq(0)
71
+ expect(p2.cross_product(p3)).to eq(-3)
72
+ expect(p3.cross_product(p2)).to eq(3)
73
+ end
74
+ end
75
+
76
+ describe '#dot' do
77
+ it 'should raise error if incorrect parameters' do
78
+ expect { p1.dot(1) }.to raise_error(TypeError)
79
+ expect { p1.dot('') }.to raise_error(TypeError)
80
+ expect { p1.dot(nil) }.to raise_error(TypeError)
81
+ expect { p1.dot(l1) }.to raise_error(TypeError)
82
+ end
83
+
84
+ it 'should be equal' do
85
+ expect(p1.dot(p2)).to eq(0)
86
+ expect(p2.dot(p3)).to eq(3)
87
+ expect(p3.dot(p2)).to eq(3)
88
+ expect(p4.dot(p2)).to eq(4)
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,101 @@
1
+ require "easy_geometry"
2
+ # This file was generated by the `rspec --init` command. Conventionally, all
3
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
5
+ # this file to always be loaded, without a need to explicitly require it in any
6
+ # files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need
14
+ # it.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # rspec-expectations config goes here. You can use an alternate
19
+ # assertion/expectation library such as wrong or the stdlib/minitest
20
+ # assertions if you prefer.
21
+ config.expect_with :rspec do |expectations|
22
+ # This option will default to `true` in RSpec 4. It makes the `description`
23
+ # and `failure_message` of custom matchers include text for helper methods
24
+ # defined using `chain`, e.g.:
25
+ # be_bigger_than(2).and_smaller_than(4).description
26
+ # # => "be bigger than 2 and smaller than 4"
27
+ # ...rather than:
28
+ # # => "be bigger than 2"
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ # rspec-mocks config goes here. You can use an alternate test double
33
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
34
+ config.mock_with :rspec do |mocks|
35
+ # Prevents you from mocking or stubbing a method that does not exist on
36
+ # a real object. This is generally recommended, and will default to
37
+ # `true` in RSpec 4.
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+
41
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
42
+ # have no way to turn it off -- the option exists only for backwards
43
+ # compatibility in RSpec 3). It causes shared context metadata to be
44
+ # inherited by the metadata hash of host groups and examples, rather than
45
+ # triggering implicit auto-inclusion in groups with matching metadata.
46
+ config.shared_context_metadata_behavior = :apply_to_host_groups
47
+
48
+ # The settings below are suggested to provide a good initial experience
49
+ # with RSpec, but feel free to customize to your heart's content.
50
+ =begin
51
+ # This allows you to limit a spec run to individual examples or groups
52
+ # you care about by tagging them with `:focus` metadata. When nothing
53
+ # is tagged with `:focus`, all examples get run. RSpec also provides
54
+ # aliases for `it`, `describe`, and `context` that include `:focus`
55
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
56
+ config.filter_run_when_matching :focus
57
+
58
+ # Allows RSpec to persist some state between runs in order to support
59
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
60
+ # you configure your source control system to ignore this file.
61
+ config.example_status_persistence_file_path = "spec/examples.txt"
62
+
63
+ # Limits the available syntax to the non-monkey patched syntax that is
64
+ # recommended. For more details, see:
65
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
66
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
67
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
68
+ config.disable_monkey_patching!
69
+
70
+ # This setting enables warnings. It's recommended, but in some cases may
71
+ # be too noisy due to issues in dependencies.
72
+ config.warnings = true
73
+
74
+ # Many RSpec users commonly either run the entire suite or an individual
75
+ # file, and it's useful to allow more verbose output when running an
76
+ # individual spec file.
77
+ if config.files_to_run.one?
78
+ # Use the documentation formatter for detailed output,
79
+ # unless a formatter has already been configured
80
+ # (e.g. via a command-line flag).
81
+ config.default_formatter = "doc"
82
+ end
83
+
84
+ # Print the 10 slowest examples and example groups at the
85
+ # end of the spec run, to help surface which specs are running
86
+ # particularly slow.
87
+ config.profile_examples = 10
88
+
89
+ # Run specs in random order to surface order dependencies. If you find an
90
+ # order dependency and want to debug it, you can fix the order by providing
91
+ # the seed, which is printed after each run.
92
+ # --seed 1234
93
+ config.order = :random
94
+
95
+ # Seed global randomization in this process using the `--seed` CLI option.
96
+ # Setting this allows you to use `--seed` to deterministically reproduce
97
+ # test failures related to randomization by passing the same `--seed` value
98
+ # as the one that triggered the failure.
99
+ Kernel.srand config.seed
100
+ =end
101
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_geometry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Henry Metlov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Geometric primitives and algorithms for Ruby
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".rspec"
34
+ - ".ruby-version"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - README
38
+ - easy_geometry.gemspec
39
+ - lib/.ruby-version
40
+ - lib/easy_geometry.rb
41
+ - lib/easy_geometry/d2/line.rb
42
+ - lib/easy_geometry/d2/linear_entity.rb
43
+ - lib/easy_geometry/d2/point.rb
44
+ - lib/easy_geometry/d2/polygon.rb
45
+ - lib/easy_geometry/d2/ray.rb
46
+ - lib/easy_geometry/d2/segment.rb
47
+ - lib/easy_geometry/d2/triangle.rb
48
+ - lib/easy_geometry/d2/vector.rb
49
+ - spec/d2/line_spec.rb
50
+ - spec/d2/point_spec.rb
51
+ - spec/d2/polygon_spec.rb
52
+ - spec/d2/ray_spec.rb
53
+ - spec/d2/segment_spec.rb
54
+ - spec/d2/triangle_spec.rb
55
+ - spec/d2/vector_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage: https://github.com/Metloff/easy_geometry
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.0.3
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Geometric primitives and algorithms for Ruby
80
+ test_files:
81
+ - spec/d2/line_spec.rb
82
+ - spec/d2/point_spec.rb
83
+ - spec/d2/polygon_spec.rb
84
+ - spec/d2/ray_spec.rb
85
+ - spec/d2/segment_spec.rb
86
+ - spec/d2/triangle_spec.rb
87
+ - spec/d2/vector_spec.rb
88
+ - spec/spec_helper.rb