trilateration 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 350d570c17ec97de89bffff8e3757d11dc8262fa
4
+ data.tar.gz: 73e954901be4f704167e4be47e2e1470ef58dd56
5
+ SHA512:
6
+ metadata.gz: 11dbff7b5605c81e604ac64e7fd4edc4e39de5bd863e4abfe5a96496e7d3e86081da97b38b8694fb0d5535fc2723adc1f4c81b76cbe55b0616d7592275f1372a
7
+ data.tar.gz: 818e40d6413fd5960b4bd511eea7bcddb95d7b0668552edc56ea93ae663154725571a0e759bf624e4a25324959ad801a1332048a1a7db7b52827eb84e1247cdd
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 patrickread
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,10 @@
1
+ Trilateration calculates the position of an unknown point, using the distance from three known points to it.
2
+
3
+ Example Usage:
4
+
5
+ p1 = Trilateration::Point.new(1,2)
6
+ p2 = Trilateration::Point.new(8,6)
7
+ p3 = Trilateration::Point.new(1,6)
8
+ tri = Trilateration::Calculate.new(p1, p2, p3)
9
+
10
+ output = tri.calculate_from_distances(4.02305854, 4.43677811, 3.87104637)
@@ -0,0 +1,2 @@
1
+ require 'trilateration/calculate'
2
+ require 'trilateration/point'
@@ -0,0 +1,62 @@
1
+ module Trilateration
2
+ class Calculate
3
+ attr_accessor :point1, :point2, :point3, :rel_point1, :rel_point2, :rel_point3
4
+
5
+ # where point1 is our origin, and p2 and p3 are two other
6
+ # sensors in which we know the relative positions
7
+ def initialize p1, p2, p3
8
+ @point1 = p1
9
+ @point2 = p2
10
+ @point3 = p3
11
+
12
+ # adjust all the points so that they are relative to p1
13
+ @rel_point1 = Point.new(0, 0, 0)
14
+ @rel_point2 = get_relative_point(@point2, @point1)
15
+ @rel_point3 = get_relative_point(@point3, @point1)
16
+ end
17
+
18
+ # Trilaterating an X,Y coordinate of an object based on its distance
19
+ # from three sensor points which are at known coordinate positions
20
+ # input: target point relative to the first sensor
21
+ def calculate_from_distances dist1, dist2, dist3
22
+ x = ((dist1 ** 2) - (dist2 ** 2) + (@rel_point2.x ** 2)) / (2 * @rel_point2.x)
23
+
24
+ a = ((dist1 ** 2) - (dist3 ** 2) + (@rel_point3.x ** 2) + (@rel_point3.y ** 2)) / (2 * @rel_point3.y)
25
+ b = (@rel_point3.x * dist1) / @rel_point3.y
26
+
27
+ y = a - b
28
+
29
+ z = ((dist1 ** 2) - (x ** 2) - (y ** 2)) ** 0.5
30
+
31
+ Point.new(x, y, z)
32
+ end
33
+
34
+ # used similarly as calculate_from_distances, except this is a test method
35
+ # input: a target point whose coordinates from @point1 are already known,
36
+ # in order to test the trilateration algorithm quickly
37
+ def calculate_from_test_point target_point
38
+ rel_target_point = get_relative_point(target_point, @point1)
39
+ dist1 = calculate_dist1(rel_target_point)
40
+ dist2 = calculate_dist2(rel_target_point)
41
+ dist3 = calculate_dist3(rel_target_point)
42
+
43
+ calculate_from_distances(dist1, dist2, dist3)
44
+ end
45
+
46
+ def get_relative_point relative_point, origin_point
47
+ Point.new(relative_point.x - origin_point.x, relative_point.y - origin_point.y, relative_point.z - origin_point.z)
48
+ end
49
+
50
+ def calculate_dist1 target_point
51
+ ((target_point.x ** 2) + (target_point.y ** 2) + (target_point.z ** 2)) ** 0.5
52
+ end
53
+
54
+ def calculate_dist2 target_point
55
+ (((target_point.x - @rel_point2.x) ** 2) + (target_point.y ** 2) + (target_point.z ** 2)) ** 0.5
56
+ end
57
+
58
+ def calculate_dist3 target_point
59
+ (((target_point.x - @rel_point3.x) ** 2) + ((target_point.y - @rel_point3.y) ** 2) + (target_point.z ** 2)) ** 0.5
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,24 @@
1
+ module Trilateration
2
+ class Point
3
+ attr_accessor :x, :y, :z
4
+
5
+ # Third dimension is optional
6
+ def initialize pointX, pointY, pointZ=0
7
+ @x = pointX
8
+ @y = pointY
9
+ @z = pointZ
10
+ end
11
+
12
+ def x_rounded
13
+ @x.round(3)
14
+ end
15
+
16
+ def y_rounded
17
+ @y.round(3)
18
+ end
19
+
20
+ def z_rounded
21
+ @z.round(3)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "minitest/autorun"
4
+ require_relative '../lib/trilateration.rb'
5
+ require_relative '../lib/point.rb'
6
+
7
+ class TrilaterationTest < Minitest::Test
8
+
9
+ def test_trilateration
10
+ p1 = Point.new(1,2)
11
+ p2 = Point.new(8,6)
12
+ p3 = Point.new(1,6)
13
+ tri = Trilateration.new(p1, p2, p3)
14
+
15
+ test_output = tri.calculate_from_test_point(Point.new(4.25, 4.15, 1))
16
+ output = tri.calculate_from_distances(4.02305854, 4.43677811, 3.87104637)
17
+
18
+ assert_equal(test_output.x_rounded, output.x_rounded, "X Coordinate is not correct")
19
+ assert_equal(test_output.y_rounded, output.y_rounded, "Y Coordinate is not correct")
20
+
21
+ if (!test_output.z.is_a?(Complex))
22
+ refute_kind_of(Complex, output.z, "Z Coordinate shouldn't be a Complex")
23
+ assert_equal(test_output.z_rounded, output.z_rounded, "Z Coordinate is not correct")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'trilateration'
3
+ s.version = '0.0.1'
4
+ s.date = '2014-05-29'
5
+ s.summary = "Performs a trilateration operation in order to find an unknown point in a Cartesian coordinate system."
6
+ s.description = "Trilateration calculates the position of an unknown point, using the distance from three known points to it."
7
+ s.authors = ["Patrick Read"]
8
+ s.email = 'pread13@gmail.com'
9
+ s.files = %w(LICENSE README.md trilateration.gemspec)
10
+ s.files += Dir.glob('lib/**/*.rb')
11
+ s.files += Dir.glob('spec/**/*')
12
+ s.test_files = Dir.glob('tests/**/*')
13
+ s.homepage = 'https://github.com/patrickread/trilateration'
14
+ s.license = 'MIT'
15
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trilateration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Read
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Trilateration calculates the position of an unknown point, using the
14
+ distance from three known points to it.
15
+ email: pread13@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/trilateration.rb
23
+ - lib/trilateration/calculate.rb
24
+ - lib/trilateration/point.rb
25
+ - tests/test_trilateration.rb
26
+ - trilateration.gemspec
27
+ homepage: https://github.com/patrickread/trilateration
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.2.2
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Performs a trilateration operation in order to find an unknown point in a
51
+ Cartesian coordinate system.
52
+ test_files:
53
+ - tests/test_trilateration.rb