tiny_fill 1.0.0

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/tiny_fill.rb +46 -0
  3. metadata +40 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 44282db4bd5db3aaf1eb05ae7542b3f370c3eb66fbd4cd5b6a4d434b10c1cb53
4
+ data.tar.gz: 46b4933fc5872d7d1522a5bebe4bbc93fb3464ada38f4ac393714e09f008ed75
5
+ SHA512:
6
+ metadata.gz: 7e43f65e6b020ea212853dd5bceee05093dafcefd549c6fa35ce5f4ebb18bcd8180d672f7b13140b6da59b3407faf65dd952a31b3fd3520a5eaf06e7ba08d4f2
7
+ data.tar.gz: 651d493412514612ab858485e857f5fecd78a5f79df961b33c8dc1cc35d5de7bfa9674d4039df6a7bfbe65b57905664b521b68f8d0b7942ab6d7b37ceab10503
data/lib/tiny_fill.rb ADDED
@@ -0,0 +1,46 @@
1
+ module TinyFill
2
+ def self.fill(map:, start_x:, start_y:, max_search:)
3
+ wid = map.first.length
4
+ hgt = map.length
5
+ return nil unless start_x >= 0
6
+ return nil unless start_y >= 0
7
+ return nil unless start_x < wid
8
+ return nil unless start_y < hgt
9
+ return nil if max_search <= 0
10
+
11
+ return nil if map[start_y][start_x] # starting point is already on a border
12
+ search_queue = [[start_x, start_y]]
13
+ map[start_y][start_x] = true
14
+ cells_checked = 1
15
+
16
+ loop do
17
+ break nil if cells_checked > max_search
18
+ break map if search_queue.length == 0
19
+
20
+ cx, cy = search_queue.shift
21
+
22
+ [
23
+ [cx , cy - 1],
24
+ [cx + 1, cy - 1],
25
+ [cx + 1, cy ],
26
+ [cx + 1, cy + 1],
27
+ [cx , cy + 1],
28
+ [cx - 1, cy + 1],
29
+ [cx - 1, cy ],
30
+ [cx - 1, cy - 1],
31
+ ].each do |next_cell|
32
+ nx, ny = next_cell
33
+
34
+ next if nx < 0
35
+ next if ny < 0
36
+ next if nx >= wid
37
+ next if ny >= hgt
38
+ next if map[ny][nx]
39
+
40
+ map[ny][nx] = true
41
+ cells_checked += 1
42
+ search_queue << [nx, ny]
43
+ end
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,40 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_fill
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Lunt
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: flood fill algo for detecting enclosed areas on a 2d array
13
+ email: jefflunt@gmail.com
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - lib/tiny_fill.rb
19
+ homepage: https://github.com/jefflunt/tiny_fill
20
+ licenses:
21
+ - MIT
22
+ metadata: {}
23
+ rdoc_options: []
24
+ require_paths:
25
+ - lib
26
+ required_ruby_version: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ required_rubygems_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ requirements: []
37
+ rubygems_version: 3.6.9
38
+ specification_version: 4
39
+ summary: flood fill algo for detecting enclosed areas on a 2d array
40
+ test_files: []