coords 0.0.8 → 0.0.9

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: 71f88964e9d3049a37804c1c9db1401aefb8f163
4
- data.tar.gz: 2bcbd911f75fa84b05482d8994ee1956bf53dca7
3
+ metadata.gz: 5650aafea9e3293dfabd0891b4b918454189af5d
4
+ data.tar.gz: 86475eb12664e5cc73ddeeab6b6ee528e03256c1
5
5
  SHA512:
6
- metadata.gz: 28d4fe183b98a0b10c3ffb8e44035beb0042235722e98fa1f3a9c922bc66a38553332c584fed6db370fe18da846777fa3e824207b2851f882bf9ad24e3272471
7
- data.tar.gz: a8077a0ea3ae2a4af4e8a1cfd09e18356c9cd1f920b063450cd649b52421bea3e31fa5b8c3798668fe6e29edc7dd8e1dc737d3ecc486d6982f2c30ba3a2d1805
6
+ metadata.gz: 8be3f54bd57d0287adb8188ef35dfd0ba22c766f7a77498d0ec195975255c272b8ae29ffff92d1218811092138998abe66447da2b7a1f55f8db54496855f47c1
7
+ data.tar.gz: 29f0df2c3e01fcc3354ecfea6846cf517106b731aa81397a93e2e95239a9d247572872bd1a4fd326f2114e8ce73685baa4a24535c622caeba5b0bd39a38c4521
data/README.md CHANGED
@@ -19,11 +19,59 @@ Or install it yourself as:
19
19
  $ gem install coords
20
20
 
21
21
  ## Usage
22
+
23
+ ### Utilities
24
+ ```ruby
25
+ # convert degrees to radians
26
+ Coords.radians(180)
27
+ => 3.141592653589793
28
+
29
+ # convert radians to degrees
30
+ Coords.radians(Math::PI)
31
+ => 180.0
32
+ ```
33
+
34
+ ### Cartesian (2D)
22
35
  ```ruby
23
- point = Coords::Cartesian2d.new(1, 2)
24
- d = point.distance(point2)
25
- polar_point = point.to_polar
36
+ # create a new point
37
+ p = Cartesian2d.new(1, 2)
38
+
39
+ # access point coordinates
40
+ p.x
41
+ => 1
42
+ p.y
43
+ => 2
44
+
45
+ # find distances between points
46
+ p2 = Cartesian2d.new(3, 3)
47
+ p.distance_squared(p2)
48
+ => 5
49
+ p.distance(p2)
50
+ => 2.23606797749979
51
+
52
+ # convert to polar coordinate system
53
+ p.to_polar
54
+ => #<Coords::Polar @radius=2.2360679775, @theta=1.107148717794>
55
+
56
+ # translate coordinates (add values to them)
57
+ p.translate(1, 1)
58
+ => #<Coords::Cartesian2d @x=2, @y=3>
59
+
60
+ # rotate point around origin
61
+ p.rotate(Coords.radians(180))
62
+ => #<Coords::Cartesian2d @x=-1.0, @y=-2.0>
63
+
64
+ # reflect point against origin, line, x-axis or y-axis
65
+ p.reflect('origin')
66
+ => #<Coords::Cartesian2d @x=-1, @y=-2>
67
+ p.reflect('line')
68
+ => #<Coords::Cartesian2d @x=2, @y=1>
69
+ p.reflect('x')
70
+ => #<Coords::Cartesian2d @x=1, @y=-2>
71
+ p.reflect('y')
72
+ => #<Coords::Cartesian2d @x=-1, @y=2>
26
73
  ```
74
+
27
75
  ## Contributing
28
76
 
29
77
  1. Fork it ( https://github.com/ceud/coords/fork )
@@ -48,16 +48,19 @@ module Coords
48
48
  end
49
49
 
50
50
  def rotate!(rZ = 0, rX = 0, rY = 0)
51
+ # rotate clockwise around z axis
51
52
  d = Math.hypot(y, x)
52
53
  theta = Math.atan2(y, x) + rZ
53
54
  @x = d * Math.cos(theta)
54
55
  @y = d * Math.sin(theta)
55
56
 
57
+ # rotate clockwise around x axis
56
58
  d = Math.hypot(y, z)
57
59
  theta = Math.atan2(z, y) + rX
58
60
  @y = d * Math.cos(theta)
59
61
  @z = d * Math.sin(theta)
60
62
 
63
+ # rotate clockwise around y axis
61
64
  d = Math.hypot(x, z)
62
65
  theta = Math.atan2(x, z) + rY
63
66
  @z = d * Math.cos(theta)
data/lib/coords/polar.rb CHANGED
@@ -42,5 +42,33 @@ module Coords
42
42
  radius.round(12) == point.radius.round(12) && theta.round(12) == point.theta.round(12)
43
43
  end
44
44
 
45
+ def translate(radius2, theta2)
46
+ translated_point = Polar.new(radius, theta)
47
+ translated_point.translate!(radius2, theta2)
48
+ translated_point
49
+ end
50
+
51
+ def translate!(radius2, theta2)
52
+ c1 = self.to_cartesian
53
+ c2 = Polar.new(radius2, theta2).to_cartesian
54
+ c3 = c1.translate(c2.x, c2.y)
55
+ p = c3.to_polar
56
+
57
+ @radius = p.radius
58
+ @theta = p.theta
59
+ end
60
+
61
+ def rotate(radians)
62
+ rotated_point = Polar.new(radius, theta)
63
+ rotated_point.rotate!(radians)
64
+ rotated_point
65
+ end
66
+
67
+ def rotate!(radians)
68
+ @theta += radians
69
+ @theta -= (2 * Math::PI) while theta > (2 * Math::PI)
70
+ @theta += (2 * Math::PI) while theta < 0
71
+ end
72
+
45
73
  end
46
74
  end
@@ -1,3 +1,3 @@
1
1
  module Coords
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -50,4 +50,24 @@ describe Coords::Polar do
50
50
  expect(subject != point2).to be false
51
51
  end
52
52
  end
53
+
54
+ describe '#translate' do
55
+ it 'translates values to new point' do
56
+ point2 = subject.translate(2, 1)
57
+ expect(subject.radius).to eq(1)
58
+ expect(subject.theta).to eq(2)
59
+ expect(point2.radius).to eq(2.676043576528)
60
+ expect(point2.theta).to eq(1.319872910435)
61
+ end
62
+ end
63
+
64
+ describe '#rotate' do
65
+ it 'rotates values to new point' do
66
+ point2 = subject.rotate(1)
67
+ expect(subject.radius).to eq(1)
68
+ expect(subject.theta).to eq(2)
69
+ expect(point2.radius).to eq(1)
70
+ expect(point2.theta).to eq(3)
71
+ end
72
+ end
53
73
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coords
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Allan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-15 00:00:00.000000000 Z
11
+ date: 2015-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler