ruby-geometry 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: ceb19ba1fb790c21207f0d8b7543b64676e8c4d0
4
- data.tar.gz: c69308a99ef8aee203bdad69478e0b2108f7fea1
3
+ metadata.gz: f47c93644dd21704742df40e87355eb35ebb61a9
4
+ data.tar.gz: 6d9b2f81291fd61de1d3d5cdc110fde153681065
5
5
  SHA512:
6
- metadata.gz: e4c5e0981ef218e99c704aefbf9410e2fe2c62dd1a650a790fd2fd6b4194869c46f77154242a444ff335960853cbc1a86f0225964bdaf884a23922f87966a9e7
7
- data.tar.gz: 326038ae5819fa77e6986a78eb5b04b6eee0a4ff66e89d0b483fd426bb42965a5d700f201552299bcb6a0b96677168f06a0cac36487bcfb11d3ca31aca2c9a8e
6
+ metadata.gz: a9eebbfbd51ceb989571d5506f43e0f50bc37a2fe1788e4d8af093eca4603cf40f255a86677b4b76c20a5949fda8aa958111b9b9a40c9d2ad5548e2362db0d6d
7
+ data.tar.gz: 1bb0cea637ba52b1dd40dbaea8484af7050684bc979d04c49285d5e5004cd341dc23d6613bfed00c88eb2b4c56cbe5c755fe8e174893d7a15efc95b002541cfc
@@ -0,0 +1,10 @@
1
+ Changelog
2
+ =========
3
+
4
+ ### 0.5.0
5
+
6
+ * Added `Line#distance_to(point)`, `Segment#distance_to(point)`
7
+ * Added `Polygon#area`
8
+ * Added `Point.advance_by(vector)`
9
+ * Switched to Ruby 2.0 as a default Ruby version to support
10
+ * Switched from obsolete Test::Unit to Minitest
data/README.md CHANGED
@@ -28,47 +28,51 @@ Defined geometry objects
28
28
  Already implemented algorithms
29
29
  ==============================
30
30
 
31
- * Do segments overlap? { Segments#overlaps? }
31
+ * Area of convex polygon `Polygon#area`
32
32
 
33
- * Do segments lie on one line? { Segment#lies_on_one_line_with? }
33
+ * Distance from point to a line or segment `Line#distance_to(point)`, `Segment#distance_to(point)`
34
34
 
35
- * Do segments intersect? { Segment#intersects_with? }
35
+ * Do segments overlap? `Segments#overlaps?`
36
36
 
37
- * Segments intersection point { Segment#intersection_point_with }
37
+ * Do segments lie on one line? `Segment#lies_on_one_line_with?`
38
38
 
39
- * Does segment contain given point? { Segment#contains_point? }
39
+ * Do segments intersect? `Segment#intersects_with?`
40
40
 
41
- * Are segments parallel? { Segment#parallel_to? }
41
+ * Segments intersection point `Segment#intersection_point_with`
42
42
 
43
- * Are vectors collinear? { Vector#collinear_with? }
43
+ * Does segment contain given point? `Segment#contains_point?`
44
44
 
45
- * Vectors cross product (outer product, vector product) { Vector#cross_product }
45
+ * Are segments parallel? `Segment#parallel_to?`
46
46
 
47
- * Vectors scalar product (inner product, dot product) { Vector#scalar_product }
47
+ * Are vectors collinear? `Vector#collinear_with?`
48
48
 
49
- * Segment length { Segment#length }
49
+ * Vectors cross product (outer product, vector product) `Vector#cross_product`
50
50
 
51
- * Vector modulus { Vector#modulus }
51
+ * Vectors scalar product (inner product, dot product) `Vector#scalar_product`
52
52
 
53
- * Trivial vector arithmetics: summation, subtraction, vector-number multiplication { Vector#+(vector); Vector#-(vector); Vector#*(numeric) }
53
+ * Segment length `Segment#length`
54
54
 
55
- * Euclid distance { Geometry#distance }
55
+ * Vector modulus `Vector#modulus`
56
56
 
57
- * Line slope { Line#slope }
57
+ * Trivial vector arithmetics: summation, subtraction, vector-number multiplication `Vector#+(vector); Vector#-(vector); Vector#*(numeric)`
58
58
 
59
- * Y-intercept of a line { Line#y_intercept }
59
+ * Euclid distance `Geometry#distance`
60
60
 
61
- * X-intercept of a line { Line#x_intercept }
61
+ * Line slope `Line#slope`
62
62
 
63
- * Are lines parallel? { Line#parallel_to? }
63
+ * Y-intercept of a line `Line#y_intercept`
64
64
 
65
- * What x-value do lines intersect at? { Line#intersect_x }
65
+ * X-intercept of a line `Line#x_intercept`
66
66
 
67
- * What is the angle between two lines? { Line#angle_to }
67
+ * Are lines parallel? `Line#parallel_to?`
68
68
 
69
- * Rectangular bounds of polygon { Polygon#bounding_box }
70
-
71
- * Does polygon contain a given point? { Polygon#contains? }
69
+ * What x-value do lines intersect at? `Line#intersect_x`
70
+
71
+ * What is the angle between two lines? `Line#angle_to`
72
+
73
+ * Rectangular bounds of polygon `Polygon#bounding_box`
74
+
75
+ * Does polygon contain a given point? `Polygon#contains?`
72
76
 
73
77
 
74
78
  Coming up
@@ -76,9 +80,7 @@ Coming up
76
80
 
77
81
  * Is polygon self-intersecting?
78
82
 
79
- * Area of polygon
80
-
81
- * Is polygon convex?
83
+ * Is polygon convex?
82
84
 
83
85
  * Do polygons intersect?
84
86
 
@@ -86,7 +88,5 @@ Coming up
86
88
 
87
89
  * Do circles intersect?
88
90
 
89
- * Area of circle
90
-
91
91
 
92
92
  Copyright (c) 2008 Daniel Vartanov, released under the MIT license
@@ -1,18 +1,18 @@
1
1
  require "active_support/core_ext/module/delegation"
2
2
  require "memoist"
3
3
 
4
- require 'point'
5
- require 'segment'
6
- require 'vector'
7
- require 'polygon'
8
- require 'line'
9
- require 'bounding_box'
10
- require 'algorithms/point_in_polygon'
4
+ require 'geometry/point'
5
+ require 'geometry/segment'
6
+ require 'geometry/vector'
7
+ require 'geometry/polygon'
8
+ require 'geometry/line'
9
+ require 'geometry/bounding_box'
10
+ require 'geometry/algorithms/point_in_polygon'
11
11
 
12
12
  module Geometry
13
13
  include Math
14
14
  extend Math
15
-
15
+
16
16
  def distance(point1, point2)
17
17
  hypot point1.x - point2.x, point1.y - point2.y
18
18
  end
@@ -43,7 +43,7 @@ module Geometry
43
43
  end
44
44
 
45
45
  def good_ray?(ray)
46
- edges.none? { |edge| edge.parallel_to?(ray) } && vertices.none? { |vertex| ray.contains_point?(vertex) }
46
+ edges.none? { |edge| !edge.length.zero? && edge.parallel_to?(ray) } && vertices.none? { |vertex| ray.contains_point?(vertex) }
47
47
  end
48
48
 
49
49
  def intersection_count(ray)
@@ -89,5 +89,9 @@ module Geometry
89
89
 
90
90
  (((x2-x1)*(y1-y0))-((x1-x0)*(y2-y1))).abs/Math.sqrt((x2-x1)**2+(y2-y1)**2)
91
91
  end
92
+
93
+ def length
94
+ point1.distance_to(point2)
95
+ end
92
96
  end
93
97
  end
@@ -15,6 +15,10 @@ module Geometry
15
15
  def advance_by(vector)
16
16
  Point x + vector.x, y + vector.y
17
17
  end
18
+
19
+ def distance_to(point)
20
+ Geometry.distance(self, point)
21
+ end
18
22
  end
19
23
  end
20
24
 
File without changes
@@ -71,6 +71,8 @@ module Geometry
71
71
  p1 = point1.to_vector
72
72
  p2 = point2.to_vector
73
73
 
74
+ return Geometry.distance(q, p1) if p1 == p2
75
+
74
76
  u = p2 - p1
75
77
  v = q - p1
76
78
 
File without changes
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "ruby-geometry"
3
- s.version = "0.0.5"
3
+ s.version = "0.0.6"
4
4
  s.author = ["Daniel Vartanov"]
5
5
  s.email = ["dan@vartanov.net"]
6
6
  s.homepage = "http://github.com/DanielVartanov/ruby-geometry"
@@ -0,0 +1,16 @@
1
+ require 'minitest/autorun'
2
+ require 'geometry'
3
+
4
+ class SlopeTest < MiniTest::Unit::TestCase
5
+ include Geometry
6
+
7
+ def test_length
8
+ point1 = Point.new(0, 0)
9
+ point2 = Point.new(3, 4)
10
+ line = Line.new(point1, point2)
11
+ assert_equal 5, point1.distance_to(point2)
12
+ assert_equal 5, line.length
13
+ end
14
+
15
+ end
16
+
@@ -0,0 +1,43 @@
1
+ require 'minitest/autorun'
2
+ require 'geometry'
3
+
4
+ class DistanceToTest < MiniTest::Unit::TestCase
5
+ include Geometry
6
+
7
+ # Distance in whole number
8
+ def test_distance_to_point2
9
+ point1 = Point.new(0, 0)
10
+ point2 = Point.new(3, 4)
11
+ assert_equal 5, point1.distance_to(point2)
12
+ end
13
+
14
+ # Distance in decimal
15
+ # point 2 is right up
16
+ def test_distance_to_point_right_up
17
+ point1 = Point.new(0, 0)
18
+ point2 = Point.new(1, 1)
19
+ assert_equal Math.sqrt(2), point1.distance_to(point2)
20
+ end
21
+
22
+ # Point 2 is left up
23
+ def test_distance_to_point_left_up
24
+ point1 = Point.new(3, 0)
25
+ point2 = Point.new(0, 4)
26
+ assert_equal 5, point1.distance_to(point2)
27
+ end
28
+
29
+ # Point 2 is right below
30
+ def test_distance_to_point_right_below
31
+ point1 = Point.new(0, 4)
32
+ point2 = Point.new(3, 0)
33
+ assert_equal 5, point1.distance_to(point2)
34
+ end
35
+
36
+ # Point 2 is left below
37
+ def test_distance_to_point_left_below
38
+ point1 = Point.new(3, 4)
39
+ point2 = Point.new(0, 0)
40
+ assert_equal 5, point1.distance_to(point2)
41
+ end
42
+
43
+ end
@@ -28,4 +28,10 @@ class DistanceToTest < MiniTest::Unit::TestCase
28
28
  assert_equal 1, segment.distance_to(point)
29
29
  end
30
30
 
31
- end
31
+ def test_distance_to_point_segment
32
+ segment = Segment.new_by_arrays([0, 1], [0, 1])
33
+ point = Point.new(1, 1)
34
+ assert_equal 1, segment.distance_to(point)
35
+ end
36
+
37
+ end
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-geometry
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
  - Daniel Vartanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-15 00:00:00.000000000 Z
11
+ date: 2016-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: memoist
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: minitest
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  description:
@@ -87,22 +87,23 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - .gitignore
91
- - .ruby-version
90
+ - ".gitignore"
91
+ - ".ruby-version"
92
+ - CHANGELOG.md
92
93
  - Gemfile
93
94
  - MIT-LICENSE
94
95
  - README.md
95
96
  - Rakefile
96
97
  - TODO
97
- - lib/algorithms/point_in_polygon.rb
98
- - lib/bounding_box.rb
99
- - lib/comparison_with_precision.rb
100
98
  - lib/geometry.rb
101
- - lib/line.rb
102
- - lib/point.rb
103
- - lib/polygon.rb
104
- - lib/segment.rb
105
- - lib/vector.rb
99
+ - lib/geometry/algorithms/point_in_polygon.rb
100
+ - lib/geometry/bounding_box.rb
101
+ - lib/geometry/comparison_with_precision.rb
102
+ - lib/geometry/line.rb
103
+ - lib/geometry/point.rb
104
+ - lib/geometry/polygon.rb
105
+ - lib/geometry/segment.rb
106
+ - lib/geometry/vector.rb
106
107
  - ruby-geometry.gemspec
107
108
  - test/bounding_box/contains_test.rb
108
109
  - test/geometry/distance_test.rb
@@ -111,12 +112,14 @@ files:
111
112
  - test/line/horizontal_test.rb
112
113
  - test/line/initialize_test.rb
113
114
  - test/line/intersect_x_test.rb
115
+ - test/line/length_test.rb
114
116
  - test/line/parallel_to_test.rb
115
117
  - test/line/slope_test.rb
116
118
  - test/line/vertical_test.rb
117
119
  - test/line/x_intercept_test.rb
118
120
  - test/line/y_intercept_test.rb
119
121
  - test/point/advance_by_test.rb
122
+ - test/point/distance_to_test.rb
120
123
  - test/point/equals_test.rb
121
124
  - test/point/initialize_test.rb
122
125
  - test/polygon/bounding_box_test.rb
@@ -150,17 +153,17 @@ require_paths:
150
153
  - lib
151
154
  required_ruby_version: !ruby/object:Gem::Requirement
152
155
  requirements:
153
- - - '>='
156
+ - - ">="
154
157
  - !ruby/object:Gem::Version
155
158
  version: '0'
156
159
  required_rubygems_version: !ruby/object:Gem::Requirement
157
160
  requirements:
158
- - - '>='
161
+ - - ">="
159
162
  - !ruby/object:Gem::Version
160
163
  version: '0'
161
164
  requirements: []
162
165
  rubyforge_project:
163
- rubygems_version: 2.2.1
166
+ rubygems_version: 2.4.6
164
167
  signing_key:
165
168
  specification_version: 4
166
169
  summary: Implementation of basic 2D geometry algorithms in Ruby
@@ -172,12 +175,14 @@ test_files:
172
175
  - test/line/horizontal_test.rb
173
176
  - test/line/initialize_test.rb
174
177
  - test/line/intersect_x_test.rb
178
+ - test/line/length_test.rb
175
179
  - test/line/parallel_to_test.rb
176
180
  - test/line/slope_test.rb
177
181
  - test/line/vertical_test.rb
178
182
  - test/line/x_intercept_test.rb
179
183
  - test/line/y_intercept_test.rb
180
184
  - test/point/advance_by_test.rb
185
+ - test/point/distance_to_test.rb
181
186
  - test/point/equals_test.rb
182
187
  - test/point/initialize_test.rb
183
188
  - test/polygon/bounding_box_test.rb