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 +4 -4
- data/lib/coords/cartesian2d.rb +26 -7
- data/lib/coords/cartesian3d.rb +0 -12
- data/lib/coords/polar.rb +1 -1
- data/lib/coords/version.rb +1 -1
- data/spec/lib/coords/cartesian2d_spec.rb +52 -9
- data/spec/lib/coords/polar_spec.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ead9696c33e5de4100aa80223a3b9760456dff6c
|
4
|
+
data.tar.gz: f85926df8801c3f961fa05368cd08cafadc238d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bce87b3931da04e5cfa9318c5d9e78c66f17f187bc26493597a41c89a2606e53cd93d40d7a546d2c38f2d568f7bcb7f46876ed54f31362eda24639c049dba159
|
7
|
+
data.tar.gz: 179c206b03680ad6082b5ab4808958d57a4b90de3273a94b4bac6dd86090335821c2c18c5b75a49ff67cc08b2e85ce7718b318e44c0ce7a575bca2c8f67970c7
|
data/lib/coords/cartesian2d.rb
CHANGED
@@ -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
|
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
|
-
|
51
|
-
|
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 = ((
|
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
|
data/lib/coords/cartesian3d.rb
CHANGED
@@ -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
data/lib/coords/version.rb
CHANGED
@@ -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.
|
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
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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.
|
42
|
-
expect(cart.y).to eq(0.
|
41
|
+
expect(cart.x).to eq(-0.416146836547142)
|
42
|
+
expect(cart.y).to eq(0.909297426825682)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|