euclidean 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +8 -0
- data/Guardfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +90 -0
- data/Rakefile +1 -0
- data/euclidean.gemspec +24 -0
- data/lib/euclidean.rb +12 -0
- data/lib/euclidean/circle.rb +118 -0
- data/lib/euclidean/cluster_factory.rb +15 -0
- data/lib/euclidean/edge.rb +143 -0
- data/lib/euclidean/point.rb +160 -0
- data/lib/euclidean/point_zero.rb +104 -0
- data/lib/euclidean/rectangle.rb +378 -0
- data/lib/euclidean/size.rb +78 -0
- data/lib/euclidean/vector.rb +34 -0
- data/lib/euclidean/version.rb +3 -0
- data/spec/euclidean/circle_spec.rb +115 -0
- data/spec/euclidean/edge_spec.rb +129 -0
- data/spec/euclidean/point_spec.rb +264 -0
- data/spec/euclidean/rectangle_spec.rb +155 -0
- data/spec/euclidean/size_spec.rb +98 -0
- data/spec/euclidean/vector_spec.rb +41 -0
- data/spec/spec_helper.rb +15 -0
- metadata +118 -0
@@ -0,0 +1,155 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'euclidean/rectangle'
|
3
|
+
|
4
|
+
def Rectangle(*args)
|
5
|
+
Euclidean::Rectangle.new(*args)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe Euclidean::Rectangle do
|
9
|
+
Rectangle = Euclidean::Rectangle
|
10
|
+
Point = Euclidean::Point
|
11
|
+
Size = Euclidean::Size
|
12
|
+
|
13
|
+
it "must accept two corners as Arrays" do
|
14
|
+
rectangle = Rectangle.new [1,2], [2,3]
|
15
|
+
expect(rectangle).to be_a_kind_of Euclidean::Rectangle
|
16
|
+
expect(rectangle.height).to eq 1
|
17
|
+
expect(rectangle.width).to eq 1
|
18
|
+
expect(rectangle.origin).to eq Point[1,2]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "must accept two named corners as Arrays" do
|
22
|
+
rectangle = Rectangle.new from:[1,2], to:[2,3]
|
23
|
+
expect(rectangle).to be_a_kind_of Euclidean::Rectangle
|
24
|
+
expect(rectangle.height).to eq 1
|
25
|
+
expect(rectangle.width).to eq 1
|
26
|
+
expect(rectangle.origin).to eq Point[1,2]
|
27
|
+
end
|
28
|
+
|
29
|
+
it "must accept named center point and size arguments" do
|
30
|
+
rectangle = Rectangle.new center:[1,2], size:[3,4]
|
31
|
+
expect(rectangle).to be_a_kind_of Euclidean::Rectangle
|
32
|
+
expect(rectangle.height).to eq 4
|
33
|
+
expect(rectangle.width).to eq 3
|
34
|
+
expect(rectangle.center).to eq Point[1,2]
|
35
|
+
end
|
36
|
+
|
37
|
+
it "must reject a named center argument with no size argument" do
|
38
|
+
expect{ Rectangle.new center:[1,2] }.to raise_error ArgumentError
|
39
|
+
end
|
40
|
+
|
41
|
+
it "must accept named origin point and size arguments" do
|
42
|
+
rectangle = Rectangle.new origin:[1,2], size:[3,4]
|
43
|
+
expect(rectangle).to be_a_kind_of Euclidean::Rectangle
|
44
|
+
expect(rectangle.height).to eq 4
|
45
|
+
expect(rectangle.width).to eq 3
|
46
|
+
expect(rectangle.origin).to eq Point[1,2]
|
47
|
+
end
|
48
|
+
|
49
|
+
it "must reject a named origin argument with no size argument" do
|
50
|
+
expect{ Rectangle.new origin:[1,2] }.to raise_error ArgumentError
|
51
|
+
end
|
52
|
+
|
53
|
+
it "must accept a sole named size argument that is an Array" do
|
54
|
+
rectangle = Rectangle.new size:[1,2]
|
55
|
+
expect(rectangle).to be_a_kind_of Euclidean::Rectangle
|
56
|
+
expect(rectangle.origin).to eq Point[0,0]
|
57
|
+
expect(rectangle.height).to eq 2
|
58
|
+
expect(rectangle.width).to eq 1
|
59
|
+
end
|
60
|
+
|
61
|
+
it "must accept a sole named size argument that is a Size" do
|
62
|
+
rectangle = Rectangle.new size:Size[1,2]
|
63
|
+
expect(rectangle).to be_a_kind_of Euclidean::Rectangle
|
64
|
+
expect(rectangle.origin).to eq Point[0,0]
|
65
|
+
expect(rectangle.height).to eq 2
|
66
|
+
expect(rectangle.width).to eq 1
|
67
|
+
end
|
68
|
+
|
69
|
+
it "must accept named width and height arguments" do
|
70
|
+
rectangle = Rectangle.new width:1, height:3
|
71
|
+
expect(rectangle).to be_a_kind_of Euclidean::Rectangle
|
72
|
+
expect(rectangle.height).to eq 3
|
73
|
+
expect(rectangle.width).to eq 1
|
74
|
+
end
|
75
|
+
|
76
|
+
it "must reject width or height by themselves" do
|
77
|
+
expect{ Rectangle.new height:1 }.to raise_error ArgumentError
|
78
|
+
expect{ Rectangle.new width:1 }.to raise_error ArgumentError
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "Comparison" do
|
82
|
+
it "must compare equal" do
|
83
|
+
rectangle = Rectangle [1,2], [3,4]
|
84
|
+
expect(rectangle).to eq Rectangle([1,2], [3, 4])
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "Inset" do
|
89
|
+
subject { Rectangle.new [0,0], [10,10] }
|
90
|
+
|
91
|
+
it "must inset equally" do
|
92
|
+
expect( subject.inset(1) ).to eq Rectangle.new [1,1], [9,9]
|
93
|
+
end
|
94
|
+
|
95
|
+
it "must inset vertically and horizontally" do
|
96
|
+
expect( subject.inset(1,2) ).to eq Rectangle.new [1,2], [9,8]
|
97
|
+
expect( subject.inset(x:1, y:2) ).to eq Rectangle.new [1,2], [9,8]
|
98
|
+
end
|
99
|
+
|
100
|
+
it "must inset from individual sides" do
|
101
|
+
expect( subject.inset(1,2,3,4) ).to eq Rectangle.new [2,3], [6,9]
|
102
|
+
expect( subject.inset(top:1, left:2, bottom:3, right:4) ).to eq Rectangle.new [2,3], [6,9]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "Properties" do
|
107
|
+
subject { Rectangle.new [1,2], [3,4] }
|
108
|
+
let(:rectangle) { Rectangle [1,2], [3,4] }
|
109
|
+
|
110
|
+
it "have a center point property" do
|
111
|
+
expect(rectangle.center).to eq Point[2,3]
|
112
|
+
end
|
113
|
+
|
114
|
+
it "have a width property" do
|
115
|
+
expect(rectangle.width).to eq 2
|
116
|
+
end
|
117
|
+
|
118
|
+
it "have a height property" do
|
119
|
+
expect(rectangle.height).to eq 2
|
120
|
+
end
|
121
|
+
|
122
|
+
it "have an origin property" do
|
123
|
+
expect(rectangle.origin).to eq Point[1,2]
|
124
|
+
end
|
125
|
+
|
126
|
+
it "have an edges property that returns 4 edges" do
|
127
|
+
edges = rectangle.edges
|
128
|
+
expect(edges.size).to eq 4
|
129
|
+
edges.each { |edge| expect(edge).to be_a_kind_of Euclidean::Edge }
|
130
|
+
end
|
131
|
+
|
132
|
+
it "have a points property that returns 4 points" do
|
133
|
+
points = rectangle.points
|
134
|
+
expect(points.size).to eq 4
|
135
|
+
points.each { |point| expect(point).to be_a_kind_of Euclidean::Point }
|
136
|
+
end
|
137
|
+
|
138
|
+
it "must have a bounds property that returns a Rectangle" do
|
139
|
+
expect(subject.bounds).to eq Rectangle.new([1,2], [3,4])
|
140
|
+
end
|
141
|
+
|
142
|
+
it "must have a minmax property that returns the corners of the bounding rectangle" do
|
143
|
+
expect(subject.minmax).to eq [Point[1,2], Point[3,4]]
|
144
|
+
end
|
145
|
+
|
146
|
+
it "must have a max property that returns the upper right corner of the bounding rectangle" do
|
147
|
+
expect(subject.max).to eq Point[3,4]
|
148
|
+
end
|
149
|
+
|
150
|
+
it "must have a min property that returns the lower left corner of the bounding rectangle" do
|
151
|
+
expect(subject.min).to eq Point[1,2]
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'euclidean/size'
|
3
|
+
|
4
|
+
describe Euclidean::Size do
|
5
|
+
it "creates a Size object using list syntax" do
|
6
|
+
size = Euclidean::Size[2,1]
|
7
|
+
expect(size.size).to eq 2
|
8
|
+
expect(size.x).to eq 2
|
9
|
+
expect(size.y).to eq 1
|
10
|
+
end
|
11
|
+
|
12
|
+
it "creates a Size object from an array" do
|
13
|
+
size = Euclidean::Size[[3,4]]
|
14
|
+
expect(size.size).to eq 2
|
15
|
+
expect(size.x).to eq 3
|
16
|
+
expect(size.y).to eq 4
|
17
|
+
end
|
18
|
+
|
19
|
+
it "creates a Size object from individual parameters" do
|
20
|
+
size = Euclidean::Size[3,4]
|
21
|
+
expect(size.size).to eq 2
|
22
|
+
expect(size.x).to eq 3
|
23
|
+
expect(size.y).to eq 4
|
24
|
+
end
|
25
|
+
|
26
|
+
it "creates a Size object from a Size" do
|
27
|
+
size = Euclidean::Size[Euclidean::Size[3,4]]
|
28
|
+
expect(size.size).to eq 2
|
29
|
+
expect(size.x).to eq 3
|
30
|
+
expect(size.y).to eq 4
|
31
|
+
end
|
32
|
+
|
33
|
+
it "creates a Size object from a Vector" do
|
34
|
+
size = Euclidean::Size[Vector[3,4]]
|
35
|
+
expect(size.size).to eq 2
|
36
|
+
expect(size.x).to eq 3
|
37
|
+
expect(size.y).to eq 4
|
38
|
+
end
|
39
|
+
|
40
|
+
it "allows indexed element access" do
|
41
|
+
size = Euclidean::Size[5,6]
|
42
|
+
expect(size.size).to eq 2
|
43
|
+
expect(size[0]).to eq 5
|
44
|
+
expect(size[1]).to eq 6
|
45
|
+
end
|
46
|
+
|
47
|
+
it "allows named element access" do
|
48
|
+
size = Euclidean::Size[5,6,7]
|
49
|
+
expect(size.size).to eq 3
|
50
|
+
expect(size.x).to eq 5
|
51
|
+
expect(size.y).to eq 6
|
52
|
+
expect(size.z).to eq 7
|
53
|
+
end
|
54
|
+
|
55
|
+
it "has a width accessor" do
|
56
|
+
size = Euclidean::Size[5,6,7]
|
57
|
+
expect(size.width).to eq 5
|
58
|
+
end
|
59
|
+
|
60
|
+
it "has a height accessor" do
|
61
|
+
size = Euclidean::Size[5,6,7]
|
62
|
+
expect(size.height).to eq 6
|
63
|
+
end
|
64
|
+
|
65
|
+
it "has a depth accessor" do
|
66
|
+
size = Euclidean::Size[5,6,7]
|
67
|
+
expect(size.depth).to eq 7
|
68
|
+
end
|
69
|
+
|
70
|
+
it "compares equal" do
|
71
|
+
size1 = Euclidean::Size[1,2]
|
72
|
+
size2 = Euclidean::Size[1,2]
|
73
|
+
size3 = Euclidean::Size[3,4]
|
74
|
+
expect(size1==size2).to be true
|
75
|
+
expect(size2==size3).to be false
|
76
|
+
end
|
77
|
+
|
78
|
+
it "compares equal to an array with equal elements" do
|
79
|
+
size1 = Euclidean::Size[1,2]
|
80
|
+
expect(size1).to eq [1,2]
|
81
|
+
end
|
82
|
+
|
83
|
+
it "does not compare equal to an array with unequal elements" do
|
84
|
+
size1 = Euclidean::Size[1,2]
|
85
|
+
size1.should_not eq [3,2]
|
86
|
+
end
|
87
|
+
|
88
|
+
it "implements inspect" do
|
89
|
+
size = Euclidean::Size[8,9]
|
90
|
+
expect(size.inspect).to eq('Size[8, 9]')
|
91
|
+
end
|
92
|
+
|
93
|
+
it "implements to_s" do
|
94
|
+
size = Euclidean::Size[10,11]
|
95
|
+
expect(size.to_s).to eq('Size[10, 11]')
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'euclidean/vector'
|
3
|
+
|
4
|
+
describe Vector do
|
5
|
+
context "Monkeypatch" do
|
6
|
+
let(:left) { Vector[1,2] }
|
7
|
+
let(:right) { Vector[3,4] }
|
8
|
+
|
9
|
+
it "must have +@" do
|
10
|
+
expect(+left).to eq Vector[1,2]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "must have unary negation" do
|
14
|
+
expect(-left).to eq Vector[-1,-2]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "must cross product" do
|
18
|
+
expect(left.cross(right)).to eq -2
|
19
|
+
expect(Vector[1,2,3].cross(Vector[3,4,5])).to eq Vector[-2, 4, -2]
|
20
|
+
expect((Vector[1,2,3] ** Vector[3,4,5])).to eq Vector[-2, 4, -2]
|
21
|
+
end
|
22
|
+
|
23
|
+
it "must have a constant representing the X axis" do
|
24
|
+
expect(Vector::X).to eq Vector[1,0,0]
|
25
|
+
end
|
26
|
+
|
27
|
+
it "must have a constant representing the Y axis" do
|
28
|
+
expect(Vector::Y).to eq Vector[0,1,0]
|
29
|
+
end
|
30
|
+
|
31
|
+
it "must have a constant representing the Z axis" do
|
32
|
+
expect(Vector::Z).to eq Vector[0,0,1]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "must not create global axis constants" do
|
36
|
+
expect{ X }.to raise_error NameError
|
37
|
+
expect{ Y }.to raise_error NameError
|
38
|
+
expect{ Z }.to raise_error NameError
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'euclidean'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
config.mock_with :rspec
|
5
|
+
|
6
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
7
|
+
config.run_all_when_everything_filtered = true
|
8
|
+
config.filter_run :focus
|
9
|
+
|
10
|
+
# Run specs in random order to surface order dependencies. If you find an
|
11
|
+
# order dependency and want to debug it, you can fix the order by providing
|
12
|
+
# the seed, which is printed after each run.
|
13
|
+
# --seed 1234
|
14
|
+
config.order = 'random'
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: euclidean
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Ward
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Basic Geometric primitives and algoritms for Ruby
|
56
|
+
email:
|
57
|
+
- jeremy.ward@digital-ocd.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- Guardfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- euclidean.gemspec
|
70
|
+
- lib/euclidean.rb
|
71
|
+
- lib/euclidean/circle.rb
|
72
|
+
- lib/euclidean/cluster_factory.rb
|
73
|
+
- lib/euclidean/edge.rb
|
74
|
+
- lib/euclidean/point.rb
|
75
|
+
- lib/euclidean/point_zero.rb
|
76
|
+
- lib/euclidean/rectangle.rb
|
77
|
+
- lib/euclidean/size.rb
|
78
|
+
- lib/euclidean/vector.rb
|
79
|
+
- lib/euclidean/version.rb
|
80
|
+
- spec/euclidean/circle_spec.rb
|
81
|
+
- spec/euclidean/edge_spec.rb
|
82
|
+
- spec/euclidean/point_spec.rb
|
83
|
+
- spec/euclidean/rectangle_spec.rb
|
84
|
+
- spec/euclidean/size_spec.rb
|
85
|
+
- spec/euclidean/vector_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
homepage: https://github.com/jrmyward/euclidean
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.2.0
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Basic Geometric primitives and algoritms for Ruby
|
111
|
+
test_files:
|
112
|
+
- spec/euclidean/circle_spec.rb
|
113
|
+
- spec/euclidean/edge_spec.rb
|
114
|
+
- spec/euclidean/point_spec.rb
|
115
|
+
- spec/euclidean/rectangle_spec.rb
|
116
|
+
- spec/euclidean/size_spec.rb
|
117
|
+
- spec/euclidean/vector_spec.rb
|
118
|
+
- spec/spec_helper.rb
|