Class: Contrek::Bitmaps::SampleGenerator
- Inherits:
-
Object
- Object
- Contrek::Bitmaps::SampleGenerator
- Defined in:
- lib/contrek/bitmaps/sample_generator.rb
Constant Summary collapse
- DIR4 =
[[1, 0], [-1, 0], [0, 1], [0, -1]]
- DIR8 =
[[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]
Instance Attribute Summary collapse
-
#cols ⇒ Object
readonly
Returns the value of attribute cols.
-
#grid ⇒ Object
readonly
Returns the value of attribute grid.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #generate_blob_islands(count: 4, min_radius: 2, max_radius: 5, noise: 0.85) ⇒ Object
- #generate_islands(count: 6, max_steps: 40, grow_probability: 0.9) ⇒ Object
-
#initialize(rows:, cols:, seed: nil, fill: 0) ⇒ SampleGenerator
constructor
A new instance of SampleGenerator.
- #to_chunk ⇒ Object
Constructor Details
#initialize(rows:, cols:, seed: nil, fill: 0) ⇒ SampleGenerator
Returns a new instance of SampleGenerator.
7 8 9 10 11 12 |
# File 'lib/contrek/bitmaps/sample_generator.rb', line 7 def initialize(rows:, cols:, seed: nil, fill: 0) @rows = rows @cols = cols @grid = Array.new(rows) { Array.new(cols, fill) } srand(seed) if seed end |
Instance Attribute Details
#cols ⇒ Object (readonly)
Returns the value of attribute cols.
6 7 8 |
# File 'lib/contrek/bitmaps/sample_generator.rb', line 6 def cols @cols end |
#grid ⇒ Object (readonly)
Returns the value of attribute grid.
6 7 8 |
# File 'lib/contrek/bitmaps/sample_generator.rb', line 6 def grid @grid end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
6 7 8 |
# File 'lib/contrek/bitmaps/sample_generator.rb', line 6 def rows @rows end |
Instance Method Details
#generate_blob_islands(count: 4, min_radius: 2, max_radius: 5, noise: 0.85) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/contrek/bitmaps/sample_generator.rb', line 29 def generate_blob_islands(count: 4, min_radius: 2, max_radius: 5, noise: 0.85) count.times do cx = rand(@rows) cy = rand(@cols) r = rand(min_radius..max_radius) (cx - r).upto(cx + r) do |x| (cy - r).upto(cy + r) do |y| next unless in_bounds?(x, y) dx = x - cx dy = y - cy @grid[x][y] = 1 if (dx * dx + dy * dy) <= r * r && rand < noise end end end end |
#generate_islands(count: 6, max_steps: 40, grow_probability: 0.9) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/contrek/bitmaps/sample_generator.rb', line 14 def generate_islands(count: 6, max_steps: 40, grow_probability: 0.9) count.times do x = rand(@rows) y = rand(@cols) rand(max_steps / 2..max_steps).times do @grid[x][y] = 1 if rand < grow_probability dx, dy = DIR8.sample nx = x + dx ny = y + dy break unless in_bounds?(nx, ny) x, y = nx, ny end end end |
#to_chunk ⇒ Object
45 46 47 |
# File 'lib/contrek/bitmaps/sample_generator.rb', line 45 def to_chunk @grid.join end |