coords 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f93b4aa9416c7cae15b4674bd007ff7bf81f0ac
4
- data.tar.gz: cdd12a97636f9fd2ee8777a89cd33d5e22e27136
3
+ metadata.gz: ead9696c33e5de4100aa80223a3b9760456dff6c
4
+ data.tar.gz: f85926df8801c3f961fa05368cd08cafadc238d3
5
5
  SHA512:
6
- metadata.gz: 99a732eee293f74f8b0a6f0d8e53588f9c20112afd29e78e057cecd7b2e7b79a12799c5469fe6124464519b4ff450aeab2018d55d4f2d1eae4e762496236a898
7
- data.tar.gz: 5bc610ea300c2dc27d55f0d40f5a6cf82f76fc652d6193cb27915e67f4ca3098e2cda158d91aa04c9418f0aecc00f4c6f50551c8471e8454c2303e97d2a8b675
6
+ metadata.gz: bce87b3931da04e5cfa9318c5d9e78c66f17f187bc26493597a41c89a2606e53cd93d40d7a546d2c38f2d568f7bcb7f46876ed54f31362eda24639c049dba159
7
+ data.tar.gz: 179c206b03680ad6082b5ab4808958d57a4b90de3273a94b4bac6dd86090335821c2c18c5b75a49ff67cc08b2e85ce7718b318e44c0ce7a575bca2c8f67970c7
@@ -30,7 +30,7 @@ module Coords
30
30
  radius = Math.sqrt((x ** 2) + (y ** 2));
31
31
  theta = Math.atan2(y, x);
32
32
 
33
- Polar.new(radius, theta)
33
+ Polar.new(radius.round(15), theta.round(15))
34
34
  end
35
35
 
36
36
  def ==(point)
@@ -38,7 +38,9 @@ module Coords
38
38
  end
39
39
 
40
40
  def translate(x2, y2)
41
- Cartesian2d.new(x + x2, y + y2)
41
+ translated_point = Cartesian2d.new(x, y)
42
+ translated_point.translate!(x2, y2)
43
+ translated_point
42
44
  end
43
45
 
44
46
  def translate!(x2, y2)
@@ -47,15 +49,32 @@ module Coords
47
49
  end
48
50
 
49
51
  def rotate(theta)
50
- x_rotated = ((x * Math.cos(theta)) - (y * Math.sin(theta))).round(15)
51
- y_rotated = ((x * Math.sin(theta)) + (y * Math.cos(theta))).round(15)
52
-
53
- Cartesian2d.new(x_rotated, y_rotated)
52
+ rotated_point = Cartesian2d.new(x, y)
53
+ rotated_point.rotate!(theta)
54
+ rotated_point
54
55
  end
55
56
 
56
57
  def rotate!(theta)
58
+ tmp_x = x
57
59
  @x = ((x * Math.cos(theta)) - (y * Math.sin(theta))).round(15)
58
- @y = ((x * Math.sin(theta)) + (y * Math.cos(theta))).round(15)
60
+ @y = ((tmp_x * Math.sin(theta)) + (y * Math.cos(theta))).round(15)
61
+ end
62
+
63
+ def reflect(type = 'origin')
64
+ reflected_point = Cartesian2d.new(x, y)
65
+ reflected_point.reflect!(type)
66
+ reflected_point
67
+ end
68
+
69
+ def reflect!(type = 'origin')
70
+ if type == 'line'
71
+ tmp_x = x
72
+ @x = y
73
+ @y = tmp_x
74
+ else
75
+ @x = x * -1 if ['origin', 'y'].include?(type)
76
+ @y = y * -1 if ['origin', 'x'].include?(type)
77
+ end
59
78
  end
60
79
 
61
80
  end
@@ -39,17 +39,5 @@ module Coords
39
39
  @z += z2
40
40
  end
41
41
 
42
- # def rotate(theta)
43
- # x_rotated = (x * Math.cos(theta)) - (y * Math.sin(theta))
44
- # y_rotated = (x * Math.sin(theta)) + (y * Math.cos(theta))
45
-
46
- # Cartesian2d.new(x_rotated, y_rotated)
47
- # end
48
-
49
- # def rotate!(theta)
50
- # @x = (x * Math.cos(theta)) - (y * Math.sin(theta))
51
- # @y = (x * Math.sin(theta)) + (y * Math.cos(theta))
52
- # end
53
-
54
42
  end
55
43
  end
data/lib/coords/polar.rb CHANGED
@@ -35,7 +35,7 @@ module Coords
35
35
  x = radius * Math.cos(theta)
36
36
  y = radius * Math.sin(theta)
37
37
 
38
- Cartesian2d.new(x, y)
38
+ Cartesian2d.new(x.round(15), y.round(15))
39
39
  end
40
40
 
41
41
  def ==(point)
@@ -1,3 +1,3 @@
1
1
  module Coords
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -39,7 +39,7 @@ describe Coords::Cartesian2d do
39
39
  it 'returns point in polar coordinate system' do
40
40
  polar = subject.to_polar
41
41
  expect(polar.radius).to eq(2.23606797749979)
42
- expect(polar.theta).to eq(1.1071487177940904)
42
+ expect(polar.theta).to eq(1.10714871779409)
43
43
  end
44
44
  end
45
45
 
@@ -61,14 +61,14 @@ describe Coords::Cartesian2d do
61
61
  end
62
62
  end
63
63
 
64
- describe '#translate!' do
65
- it 'translates values of original point' do
66
- point2 = Coords::Cartesian2d.new(1, 2)
67
- point2.translate!(3, 4)
68
- expect(point2.x).to eq(4)
69
- expect(point2.y).to eq(6)
70
- end
71
- end
64
+ # describe '#translate!' do
65
+ # it 'translates values of original point' do
66
+ # point2 = Coords::Cartesian2d.new(1, 2)
67
+ # point2.translate!(3, 4)
68
+ # expect(point2.x).to eq(4)
69
+ # expect(point2.y).to eq(6)
70
+ # end
71
+ # end
72
72
 
73
73
  describe '#rotate' do
74
74
  it 'rotates values to new point' do
@@ -79,4 +79,47 @@ describe Coords::Cartesian2d do
79
79
  expect(point2.y).to eq(1)
80
80
  end
81
81
  end
82
+
83
+ # describe '#rotate!' do
84
+ # it 'rotates values of original point' do
85
+ # point2 = Coords::Cartesian2d.new(1, 2)
86
+ # point2.rotate!(Coords.radians(90))
87
+ # expect(point2.x).to eq(-2)
88
+ # expect(point2.y).to eq(1)
89
+ # end
90
+ # end
91
+
92
+ describe '#reflect' do
93
+ it 'reflects values to new point by origin' do
94
+ point2 = subject.reflect('origin')
95
+ expect(subject.x).to eq(1)
96
+ expect(subject.y).to eq(2)
97
+ expect(point2.x).to eq(-1)
98
+ expect(point2.y).to eq(-2)
99
+ end
100
+
101
+ it 'reflects values to new point by line' do
102
+ point2 = subject.reflect('line')
103
+ expect(subject.x).to eq(1)
104
+ expect(subject.y).to eq(2)
105
+ expect(point2.x).to eq(2)
106
+ expect(point2.y).to eq(1)
107
+ end
108
+
109
+ it 'reflects values to new point by x axis' do
110
+ point2 = subject.reflect('x')
111
+ expect(subject.x).to eq(1)
112
+ expect(subject.y).to eq(2)
113
+ expect(point2.x).to eq(1)
114
+ expect(point2.y).to eq(-2)
115
+ end
116
+
117
+ it 'reflects values to new point by y axis' do
118
+ point2 = subject.reflect('y')
119
+ expect(subject.x).to eq(1)
120
+ expect(subject.y).to eq(2)
121
+ expect(point2.x).to eq(-1)
122
+ expect(point2.y).to eq(2)
123
+ end
124
+ end
82
125
  end
@@ -38,8 +38,8 @@ describe Coords::Polar do
38
38
  describe '#to_cartesian' do
39
39
  it 'returns point in cartesian coordinate system' do
40
40
  cart = subject.to_cartesian
41
- expect(cart.x).to eq(-0.4161468365471424)
42
- expect(cart.y).to eq(0.9092974268256817)
41
+ expect(cart.x).to eq(-0.416146836547142)
42
+ expect(cart.y).to eq(0.909297426825682)
43
43
  end
44
44
  end
45
45
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coords
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Allan