vector2d 2.1.0 → 2.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e4a4ed3c9fc3c47783fa81e5959d89b1cbadc94a
4
- data.tar.gz: 93c385328a4335f272844e31ba882814746c1f92
3
+ metadata.gz: 12566131225e489c9e3f3404fbe3a223d51adf44
4
+ data.tar.gz: 45476593bbdbab2330fe991f1aa96aea00b9d3bd
5
5
  SHA512:
6
- metadata.gz: 7279a2e6b24a90359ef10068d8e54b1887699af0218b51ea977665a6c7f919e6f4597e106ba137cd9e75bc4d289983ca33c5f28da8a88a61b3a31188fa5d65cd
7
- data.tar.gz: 71ece7a02af784784c5d5f757744d6fea21de21dc513507c5ba92ccf0b8f93632c122bdb9d3e6ac58c055fa72a2b5989a82c34468be8e3576f172e9033c22f91
6
+ metadata.gz: 3ded7745491872050fc9c92a98d11b6757478f5f7ab13e5c91031921854007adcd3d417d606fd8bf91d9ff6267296a2d363d792af4b77b4c6bf1e9095dc2a7ef
7
+ data.tar.gz: da6376921f75c0ccbe9ee65048b465f039379f18aead466620a9d36ffaa8258d83c1b96b8cf38c2b26e630804e762ad3782f3b9ac0c2ca9e1daeb592a511393e
@@ -1,25 +1,32 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- vector2d (2.1.0)
4
+ vector2d (2.2.0)
5
+ contracts
5
6
 
6
7
  GEM
7
8
  remote: http://rubygems.org/
8
9
  specs:
9
10
  codeclimate-test-reporter (0.3.0)
10
11
  simplecov (>= 0.7.1, < 1.0.0)
12
+ contracts (0.7)
11
13
  diff-lcs (1.2.5)
12
14
  docile (1.1.4)
13
15
  multi_json (1.10.1)
14
16
  rake (10.3.2)
15
- rspec (2.14.1)
16
- rspec-core (~> 2.14.0)
17
- rspec-expectations (~> 2.14.0)
18
- rspec-mocks (~> 2.14.0)
19
- rspec-core (2.14.8)
20
- rspec-expectations (2.14.5)
21
- diff-lcs (>= 1.1.3, < 2.0)
22
- rspec-mocks (2.14.6)
17
+ rspec (3.2.0)
18
+ rspec-core (~> 3.2.0)
19
+ rspec-expectations (~> 3.2.0)
20
+ rspec-mocks (~> 3.2.0)
21
+ rspec-core (3.2.1)
22
+ rspec-support (~> 3.2.0)
23
+ rspec-expectations (3.2.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.2.0)
26
+ rspec-mocks (3.2.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.2.0)
29
+ rspec-support (3.2.2)
23
30
  simplecov (0.8.2)
24
31
  docile (~> 1.1.0)
25
32
  multi_json
@@ -32,5 +39,5 @@ PLATFORMS
32
39
  DEPENDENCIES
33
40
  codeclimate-test-reporter
34
41
  rake (~> 10.3)
35
- rspec (~> 2.1)
42
+ rspec (~> 3.2)
36
43
  vector2d!
@@ -1,5 +1,19 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require "contracts"
4
+
5
+ class Vector2d
6
+ include Contracts
7
+ VectorLike = Or[
8
+ [Num, Num],
9
+ { x: Num, y: Num },
10
+ { 'x' => Num, 'y' => Num },
11
+ Num,
12
+ String,
13
+ Vector2d
14
+ ]
15
+ end
16
+
3
17
  require 'vector2d/calculations'
4
18
  require 'vector2d/coercions'
5
19
  require 'vector2d/fitting'
@@ -27,6 +41,7 @@ class Vector2d
27
41
  # Vector2d.parse({x: 150, y: 100})
28
42
  # Vector2d.parse({"x" => 150.0, "y" => 100.0})
29
43
  # Vector2d.parse(Vector2d(150, 100))
44
+ Contract VectorLike => Vector2d
30
45
  def parse(arg, second_arg=nil)
31
46
  if second_arg.nil?
32
47
  parse_single_arg(arg)
@@ -37,6 +52,7 @@ class Vector2d
37
52
 
38
53
  private
39
54
 
55
+ Contract VectorLike => Vector2d
40
56
  def parse_single_arg(arg)
41
57
  case arg
42
58
  when Vector2d
@@ -52,12 +68,14 @@ class Vector2d
52
68
  end
53
69
  end
54
70
 
71
+ Contract Hash => Vector2d
55
72
  def parse_hash(hash)
56
73
  hash[:x] ||= hash['x'] if hash.has_key?('x')
57
74
  hash[:y] ||= hash['y'] if hash.has_key?('y')
58
75
  self.new(hash[:x], hash[:y])
59
76
  end
60
77
 
78
+ Contract String => Vector2d
61
79
  def parse_str(str)
62
80
  if str =~ /^[\s]*[\d\.]*[\s]*x[\s]*[\d\.]*[\s]*$/
63
81
  self.new(*str.split("x").map(&:to_f))
@@ -78,6 +96,7 @@ class Vector2d
78
96
  # Vector2d(2, 3) == Vector2d(2, 3) # => true
79
97
  # Vector2d(2, 3) == Vector2d(1, 0) # => false
80
98
  #
99
+ Contract Vector2d => Bool
81
100
  def ==(comp)
82
101
  comp.x === x && comp.y === y
83
102
  end
@@ -89,4 +108,4 @@ end
89
108
  #
90
109
  def Vector2d(*args)
91
110
  Vector2d.parse(*args)
92
- end
111
+ end
@@ -2,13 +2,18 @@
2
2
 
3
3
  class Vector2d
4
4
  module Calculations
5
+ include Contracts
6
+
5
7
  module ClassMethods
8
+ include Contracts
9
+
6
10
  # Calculates cross product of two vectors.
7
11
  #
8
12
  # v1 = Vector2d(2, 1)
9
13
  # v2 = Vector2d(2, 3)
10
14
  # Vector2d.cross_product(v1, v2) # => 4
11
15
  #
16
+ Contract Vector2d, Vector2d => Num
12
17
  def cross_product(vector1, vector2)
13
18
  vector1.x * vector2.y - vector1.y * vector2.x
14
19
  end
@@ -19,6 +24,7 @@ class Vector2d
19
24
  # v2 = Vector2d(2, 3)
20
25
  # Vector2d.dot_product(v1, v2) # => 10
21
26
  #
27
+ Contract Vector2d, Vector2d => Num
22
28
  def dot_product(vector1, vector2)
23
29
  vector1.x * vector2.x + vector1.y * vector2.y
24
30
  end
@@ -29,6 +35,7 @@ class Vector2d
29
35
  # v2 = Vector2d(4, 5)
30
36
  # Vector2d.angle_between(v1, v2) # => 0.0867..
31
37
  #
38
+ Contract Vector2d, Vector2d => Num
32
39
  def angle_between(vector1, vector2)
33
40
  one = vector1.normalized? ? vector1 : vector1.normalize
34
41
  two = vector2.normalized? ? vector2 : vector2.normalize
@@ -41,6 +48,7 @@ class Vector2d
41
48
  # Vector2d(1, 2) * Vector2d(2, 3) # => Vector2d(2, 6)
42
49
  # Vector2d(1, 2) * 2 # => Vector2d(2, 4)
43
50
  #
51
+ Contract VectorLike => Vector2d
44
52
  def *(other)
45
53
  calculate_each(:*, other)
46
54
  end
@@ -50,6 +58,7 @@ class Vector2d
50
58
  # Vector2d(4, 2) / Vector2d(2, 1) # => Vector2d(2, 2)
51
59
  # Vector2d(4, 2) / 2 # => Vector2d(2, 1)
52
60
  #
61
+ Contract VectorLike => Vector2d
53
62
  def /(other)
54
63
  calculate_each(:/, other)
55
64
  end
@@ -59,6 +68,7 @@ class Vector2d
59
68
  # Vector2d(1, 2) + Vector2d(2, 3) # => Vector2d(3, 5)
60
69
  # Vector2d(1, 2) + 2 # => Vector2d(3, 4)
61
70
  #
71
+ Contract VectorLike => Vector2d
62
72
  def +(other)
63
73
  calculate_each(:+, other)
64
74
  end
@@ -68,6 +78,7 @@ class Vector2d
68
78
  # Vector2d(2, 3) - Vector2d(2, 1) # => Vector2d(0, 2)
69
79
  # Vector2d(4, 3) - 1 # => Vector2d(3, 2)
70
80
  #
81
+ Contract VectorLike => Vector2d
71
82
  def -(other)
72
83
  calculate_each(:-, other)
73
84
  end
@@ -78,6 +89,7 @@ class Vector2d
78
89
  # v2 = Vector2d(5, 6)
79
90
  # v1.distance(v2) # => 1.4142..
80
91
  #
92
+ Contract VectorLike => Num
81
93
  def distance(other)
82
94
  Math.sqrt(squared_distance(other))
83
95
  end
@@ -88,6 +100,7 @@ class Vector2d
88
100
  # v2 = Vector2d(5, 6)
89
101
  # v1.distance_squared(v2) # => 18
90
102
  #
103
+ Contract VectorLike => Num
91
104
  def squared_distance(other)
92
105
  v, _ = coerce(other)
93
106
  dx = v.x - x
@@ -101,6 +114,7 @@ class Vector2d
101
114
  # v2 = Vector2d(2, 3)
102
115
  # v1.dot_product(v2) # => 10
103
116
  #
117
+ Contract VectorLike => Num
104
118
  def dot_product(other)
105
119
  v, _ = coerce(other)
106
120
  self.class.dot_product(self, v)
@@ -112,6 +126,7 @@ class Vector2d
112
126
  # v2 = Vector2d(2, 3)
113
127
  # v1.cross_product(v2) # => 4
114
128
  #
129
+ Contract VectorLike => Num
115
130
  def cross_product(other)
116
131
  v, _ = coerce(other)
117
132
  self.class.cross_product(self, v)
@@ -123,6 +138,7 @@ class Vector2d
123
138
  # v2 = Vector2d(4, 5)
124
139
  # v1.angle_between(v2) # => 0.0867..
125
140
  #
141
+ Contract VectorLike => Num
126
142
  def angle_between(other)
127
143
  v, _ = coerce(other)
128
144
  self.class.angle_between(self, v)
@@ -138,4 +154,4 @@ class Vector2d
138
154
  )
139
155
  end
140
156
  end
141
- end
157
+ end
@@ -2,6 +2,9 @@
2
2
 
3
3
  class Vector2d
4
4
  module Coercions
5
+ include Contracts
6
+
7
+ Contract VectorLike => [Vector2d, Vector2d]
5
8
  def coerce(other)
6
9
  case other
7
10
  when Vector2d
@@ -17,6 +20,7 @@ class Vector2d
17
20
  #
18
21
  # Vector2d(2, 3).inspect # => "Vector2d(2,3)"
19
22
  #
23
+ Contract None => String
20
24
  def inspect
21
25
  "Vector2d(#{x},#{y})"
22
26
  end
@@ -25,6 +29,7 @@ class Vector2d
25
29
  #
26
30
  # Vector2d(2, 3).to_a # => [2,3]
27
31
  #
32
+ Contract None => [Num, Num]
28
33
  def to_a
29
34
  [x, y]
30
35
  end
@@ -33,6 +38,7 @@ class Vector2d
33
38
  #
34
39
  # Vector2d(2, 3).to_hash # => {x: 2, y: 3}
35
40
  #
41
+ Contract None => Hash
36
42
  def to_hash
37
43
  { x: x, y: y }
38
44
  end
@@ -41,6 +47,7 @@ class Vector2d
41
47
  #
42
48
  # Vector2d(2.0, 3.0).to_i_vector # => Vector2d(2,3)
43
49
  #
50
+ Contract None => Vector2d
44
51
  def to_i_vector
45
52
  self.class.new(x.to_i, y.to_i)
46
53
  end
@@ -49,6 +56,7 @@ class Vector2d
49
56
  #
50
57
  # Vector2d(2, 3).to_f_vector # => Vector2d(2.0,3.0)
51
58
  #
59
+ Contract None => Vector2d
52
60
  def to_f_vector
53
61
  self.class.new(x.to_f, y.to_f)
54
62
  end
@@ -57,8 +65,9 @@ class Vector2d
57
65
  #
58
66
  # Vector2d.new(150, 100).to_s # => "150x100"
59
67
  #
68
+ Contract None => String
60
69
  def to_s
61
70
  "#{x}x#{y}"
62
71
  end
63
72
  end
64
- end
73
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  class Vector2d
4
4
  module Fitting
5
+ include Contracts
5
6
 
6
7
  # Scales down the given vector unless it fits inside.
7
8
  #
@@ -10,6 +11,7 @@ class Vector2d
10
11
  # vector.contain(Vector2d(40, 20)) # => Vector2d(20,10)
11
12
  # vector.contain(Vector2d(20, 40)) # => Vector2d(10,20)
12
13
  #
14
+ Contract VectorLike => Vector2d
13
15
  def contain(other)
14
16
  v, _ = coerce(other)
15
17
  (v.x > x || v.y > y) ? other.fit(self) : other
@@ -24,6 +26,7 @@ class Vector2d
24
26
  #
25
27
  # Note: Either axis will be disregarded if zero or nil. This is a feature, not a bug.
26
28
  #
29
+ Contract VectorLike => Vector2d
27
30
  def fit(other)
28
31
  v, _ = coerce(other)
29
32
  scale = v.to_f_vector / self
@@ -37,6 +40,7 @@ class Vector2d
37
40
  # Vector2d(20, 10).fit_either(constraint) # => Vector2d(10,5)
38
41
  # Vector2d(10, 20).fit_either(constraint) # => Vector2d(5,10)
39
42
  #
43
+ Contract VectorLike => Vector2d
40
44
  def fit_either(other)
41
45
  v, _ = coerce(other)
42
46
  scale = v.to_f_vector / self
@@ -49,4 +53,4 @@ class Vector2d
49
53
  end
50
54
  alias_method :constrain_one, :fit_either
51
55
  end
52
- end
56
+ end
@@ -2,10 +2,13 @@
2
2
 
3
3
  class Vector2d
4
4
  module Properties
5
+ include Contracts
6
+
5
7
  # Angle of vector.
6
8
  #
7
9
  # Vector2d(2, 3).angle # => 0.9827..
8
10
  #
11
+ Contract None => Num
9
12
  def angle
10
13
  Math.atan2(y, x)
11
14
  end
@@ -14,6 +17,7 @@ class Vector2d
14
17
  #
15
18
  # Vector2d(2, 3).aspect_ratio # => 0.6667..
16
19
  #
20
+ Contract None => Num
17
21
  def aspect_ratio
18
22
  (x.to_f / y.to_f).abs
19
23
  end
@@ -22,6 +26,7 @@ class Vector2d
22
26
  #
23
27
  # Vector2d(2, 3).length # => 3.6055..
24
28
  #
29
+ Contract None => Num
25
30
  def length
26
31
  Math.sqrt(squared_length)
27
32
  end
@@ -30,6 +35,7 @@ class Vector2d
30
35
  #
31
36
  # Vector2d(2, 3).squared_length # => 13
32
37
  #
38
+ Contract None => Num
33
39
  def squared_length
34
40
  x * x + y * y
35
41
  end
@@ -39,8 +45,9 @@ class Vector2d
39
45
  # Vector2d(0, 1).normalized? # => true
40
46
  # Vector2d(2, 3).normalized? # => false
41
47
  #
48
+ Contract None => Bool
42
49
  def normalized?
43
50
  self.length.to_f == 1.0
44
51
  end
45
52
  end
46
- end
53
+ end
@@ -2,10 +2,13 @@
2
2
 
3
3
  class Vector2d
4
4
  module Transformations
5
+ include Contracts
6
+
5
7
  # Rounds vector to up nearest integer.
6
8
  #
7
9
  # Vector2d(2.4, 3.6).ceil # => Vector2d(3,4)
8
10
  #
11
+ Contract None => Vector2d
9
12
  def ceil
10
13
  self.class.new(x.ceil, y.ceil)
11
14
  end
@@ -14,6 +17,7 @@ class Vector2d
14
17
  #
15
18
  # Vector2d(2.4, 3.6).floor # => Vector2d(2,3)
16
19
  #
20
+ Contract None => Vector2d
17
21
  def floor
18
22
  self.class.new(x.floor, y.floor)
19
23
  end
@@ -24,6 +28,7 @@ class Vector2d
24
28
  # vector.normalize # => Vector2d(0.5547.., 0.8320..)
25
29
  # vector.normalize.length # => 1.0
26
30
  #
31
+ Contract None => Vector2d
27
32
  def normalize
28
33
  resize(1.0)
29
34
  end
@@ -32,6 +37,7 @@ class Vector2d
32
37
  #
33
38
  # Vector2d(2, 3).perpendicular # => Vector2d(-3,2)
34
39
  #
40
+ Contract None => Vector2d
35
41
  def perpendicular
36
42
  Vector2d.new(-y, x)
37
43
  end
@@ -40,6 +46,7 @@ class Vector2d
40
46
  #
41
47
  # Vector2d(2, 3).resize(1.0) # => Vector2d(0.5547.., 0.8320..)
42
48
  #
49
+ Contract Num => Vector2d
43
50
  def resize(new_length)
44
51
  self * (new_length / self.length)
45
52
  end
@@ -48,6 +55,7 @@ class Vector2d
48
55
  #
49
56
  # Vector2d(2, 3).reverse # => Vector2d(-2,-3)
50
57
  #
58
+ Contract None => Vector2d
51
59
  def reverse
52
60
  self.class.new(-x, -y)
53
61
  end
@@ -56,8 +64,12 @@ class Vector2d
56
64
  #
57
65
  # Vector2d(1, 0).rotate(Math:PI/2) => Vector2d(1,0)
58
66
  #
67
+ Contract Num => Vector2d
59
68
  def rotate(angle)
60
- Vector2d.new(x * Math.cos(angle) - y * Math.sin(angle), x * Math.sin(angle) + y * Math.cos(angle))
69
+ Vector2d.new(
70
+ x * Math.cos(angle) - y * Math.sin(angle),
71
+ x * Math.sin(angle) + y * Math.cos(angle)
72
+ )
61
73
  end
62
74
 
63
75
  # Rounds vector to nearest integer.
@@ -65,6 +77,7 @@ class Vector2d
65
77
  # Vector2d(2.4, 3.6).round # => Vector2d(2,4)
66
78
  # Vector2d(2.4444, 3.666).round(2) # => Vector2d(2.44, 3.67)
67
79
  #
80
+ Contract Or[None, Num] => Vector2d
68
81
  def round(digits=0)
69
82
  self.class.new(x.round(digits), y.round(digits))
70
83
  end
@@ -75,8 +88,9 @@ class Vector2d
75
88
  # vector.truncate(5.0) # => Vector2d(2.0, 3.0)
76
89
  # vector.truncate(1.0) # => Vector2d(0.5547.., 0.8320..)
77
90
  #
91
+ Contract Num => Vector2d
78
92
  def truncate(max)
79
93
  resize([max, self.length].min)
80
94
  end
81
95
  end
82
- end
96
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  class Vector2d
4
- VERSION = "2.1.0"
4
+ VERSION = "2.2.0"
5
5
  end
@@ -33,11 +33,11 @@ describe Vector2d::Properties do
33
33
  subject { vector.normalized? }
34
34
  context "when vector is normalized" do
35
35
  let(:vector) { Vector2d.new(2, 3).normalize }
36
- it { should be_true }
36
+ it { is_expected.to eq(true) }
37
37
  end
38
38
  context "when vector isn't normalized" do
39
39
  let(:vector) { Vector2d.new(2, 3) }
40
- it { should be_false }
40
+ it { is_expected.to eq(false) }
41
41
  end
42
42
  end
43
- end
43
+ end
@@ -70,17 +70,17 @@ describe Vector2d do
70
70
 
71
71
  context "with both arguments equal" do
72
72
  let(:comp) { Vector2d.new(2, 3) }
73
- it { should be_true }
73
+ it { is_expected.to eq(true) }
74
74
  end
75
75
 
76
76
  context "with x differing" do
77
77
  let(:comp) { Vector2d.new(3, 3) }
78
- it { should be_false }
78
+ it { is_expected.to eq(false) }
79
79
  end
80
80
 
81
81
  context "with y differing" do
82
82
  let(:comp) { Vector2d.new(2, 4) }
83
- it { should be_false }
83
+ it { is_expected.to eq(false) }
84
84
  end
85
85
  end
86
- end
86
+ end
@@ -1,3 +1,4 @@
1
+ # coding: utf-8
1
2
  # Generated by jeweler
2
3
  # DO NOT EDIT THIS FILE DIRECTLY
3
4
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
@@ -25,5 +26,6 @@ Gem::Specification.new do |s|
25
26
 
26
27
  # specify any dependencies here; for example:
27
28
  s.add_development_dependency "rake", "~> 10.3"
28
- s.add_development_dependency "rspec", "~> 2.1"
29
+ s.add_development_dependency "rspec", "~> 3.2"
30
+ s.add_dependency "contracts"
29
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vector2d
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Inge Jørgensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-07 00:00:00.000000000 Z
11
+ date: 2015-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -30,14 +30,28 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.1'
33
+ version: '3.2'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.1'
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: contracts
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: Vector2d allows for easy handling of two-dimensionals coordinates and
42
56
  vectors
43
57
  email: