minimal-convex-hull 0.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bad9e380dcea4521b6828cee2104ba8390deb43fcbff13b62248de60a42138c9
4
+ data.tar.gz: 33d51934114a9218a23db4892ccf44a8166838927c86bc61a6440192bf62f7d5
5
+ SHA512:
6
+ metadata.gz: 1098117452cf5dd98b1ceded1bbd1abc53f0aec1626aeeae08a611b7846822233190638bc0ccb42d2effae9ae84c3f2afdd4676df984748cd92812b25107aa59
7
+ data.tar.gz: 898759a553f9cda237f2e8d0faead7857b0666a65007a366d8501c745d3603efe5d31e43fb714be00b41522e1831e63d7abbea455242b1c19f345032d50fdcf5
@@ -0,0 +1,48 @@
1
+ require "point"
2
+
3
+ class GrahemAlrotithm
4
+ private
5
+
6
+ def self.rotate(a, b, c)
7
+ return (b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x)
8
+ end
9
+
10
+ public
11
+
12
+ def self.convex_hull(points)
13
+ n = points.length
14
+ if n < 2
15
+ return points.clone
16
+ end
17
+ p = (0...n).to_a
18
+ for i in 0...n
19
+ if points[p[i]].x < points[p[0]].x
20
+ p[i], p[0] = p[0], p[i]
21
+ end
22
+ end
23
+
24
+ for i in 2...n
25
+ j = i
26
+ while j>1 && (rotate(points[p[0]], points[p[j-1]], points[p[j]])<0)
27
+ p[j], p[j-1] = p[j-1], p[j]
28
+ j -= 1
29
+ end
30
+ end
31
+
32
+ s = [p[0], p[1]]
33
+ for i in 2...n
34
+ while rotate(points[s[-2]], points[s[-1]], points[p[i]]) < 0
35
+ s.pop
36
+ end
37
+ s << p[i]
38
+ end
39
+
40
+ result = []
41
+ for i in 0..s.length-1
42
+ if not result.include? points[s[i]]
43
+ result << points[s[i]]
44
+ end
45
+ end
46
+ return result
47
+ end
48
+ end
@@ -0,0 +1,50 @@
1
+ require "point"
2
+
3
+ class JarvisAlrotithm
4
+ private
5
+
6
+ def self.rotate(a, b, c)
7
+ return (b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x)
8
+ end
9
+
10
+ public
11
+
12
+ def self.convex_hull(points)
13
+ n = points.length
14
+ if n < 2
15
+ return points.clone
16
+ end
17
+ p = (0...n).to_a
18
+ for i in 1...n
19
+ if points[p[i]].x < points[p[0]].x
20
+ p[i], p[0] = p[0], p[i]
21
+ end
22
+ end
23
+
24
+ h = [p[0]]
25
+ p.delete_at(0)
26
+ p.push(h[0])
27
+
28
+ loop do
29
+ right = 0
30
+ for i in 1...p.length
31
+ if rotate(points[h[-1]], points[p[right]], points[p[i]]) < 0
32
+ right = i
33
+ end
34
+ end
35
+ if p[right] == h[0]
36
+ break
37
+ else
38
+ h.push(p[right])
39
+ p.delete_at(right)
40
+ end
41
+ end
42
+ result = []
43
+ for i in 0..h.length-1
44
+ if not result.include? points[h[i]]
45
+ result << points[h[i]]
46
+ end
47
+ end
48
+ return result
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ require "point"
2
+ require "minimal-convex-hull-by-Grahem"
3
+ require "minimal-convex-hull-by-Jarvis"
data/lib/point.rb ADDED
@@ -0,0 +1,31 @@
1
+ class Point
2
+ attr_accessor :x, :y, :eps
3
+
4
+ def initialize(x, y)
5
+ @x = x
6
+ @y = y
7
+ @eps = 0.0001
8
+ end
9
+
10
+ def ==(other)
11
+ (@x - other.x).abs < eps && (@y - other.y).abs < eps
12
+ end
13
+
14
+ def to_s
15
+ "(#{@x},#{@y})"
16
+ end
17
+
18
+ def angle_with_origin
19
+ Math.atan2(@y, @x)
20
+ end
21
+
22
+ def distance_to(other_point)
23
+ Math.hypot(@x - other_point.x, @y - other_point.y)
24
+ end
25
+
26
+ def orientation(p1, p2)
27
+ val = (p2.y - p1.y) * (@x - p2.x) - (p2.x - p1.x) * (@y - p2.y)
28
+ return 0 if val == 0
29
+ val.positive? ? 1 : 2
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minimal-convex-hull
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Mikhail Pokhodeev
8
+ - Konstantin Pinchuk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2024-06-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '13.0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '13.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitests
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 5.15.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 5.15.0
42
+ description: Grahem's and Jarvis's algorithms finding minimal convex hull
43
+ email: mishapohodeev@yandex.ru
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/minimal-convex-hull-by-Grahem.rb
49
+ - lib/minimal-convex-hull-by-Jarvis.rb
50
+ - lib/minimal-convex-hull.rb
51
+ - lib/point.rb
52
+ homepage: https://github.com/MihailPohodeev/Minimal-Convex-Hull_RUBY-GEM/issues?q=is%3Aissue+is%3Aclosed
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.3.15
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: MCH!
75
+ test_files: []